[网页工具] C++压行

前言

将C++的代码压进最少的行数,增加不可读性,抄代码必备。

工具

效果演示

效果演示1(正常运作)
使用前

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll parseLl(string v)
{
    ll egg=0;
    for(int i=int(-4)+(int)v.length();i<(int)v.length();i++)
    {
        if(i>=0) egg=egg*10+(v[i]-'0');
    }
    return egg;
}
stack<ll> numstack;
stack<bool> symstack;  //TRUE=*, FALSE=+
vector<string> tokens;
int main()
{
    ios::sync_with_stdio(false);
    string exp;
    getline(cin,exp);
    /* Parse tokens */
    {
        int l=0;
        bool c=1;  //TRUE=num, FALSE=sym
        for(unsigned i=1;i<exp.length();i++)
        {
            if(isdigit(exp[i])^c)
            {
                tokens.push_back(exp.substr(l,i-l));
                c=isdigit(exp[i]);
                l=i;
            }
        }
        tokens.push_back(exp.substr(l,exp.length()-l));
    }
    for(unsigned i=0;i<tokens.size();i++)
    {
        //cout<<"Current token is "<<tokens[i]<<endl;
        bool isnum=isdigit(tokens[i][0]);
        if(isnum)
        {
            numstack.push(parseLl(tokens[i]));
            //cout<<"  Pushed as a number:"<<numstack.top()<<endl;
        }
        else
        {
            while(!symstack.empty() && symstack.top()>=(tokens[i]=="*"))
            {
                ll a=numstack.top();
                numstack.pop();
                ll b=numstack.top();
                bool s=symstack.top();
                numstack.pop();symstack.pop();
                if(s) numstack.push((a*b)%10000);
                else numstack.push((a+b)%10000);
                //cout<<"  Solved:"<<a<<" "<<s<<" "<<b<<endl;
            }
            symstack.push(tokens[i]=="*");
            //cout<<"  Pushed as a symbol:"<<symstack.top()<<endl;
        }
    }
    while(!symstack.empty())
    {
        ll a=numstack.top();
        numstack.pop();
        ll b=numstack.top();
        bool s=symstack.top();
        numstack.pop();symstack.pop();
        if(s) numstack.push((a*b)%10000);
        else numstack.push((a+b)%10000);
        //cout<<"- Last calculated:"<<a<<" "<<s<<" "<<b<<endl;
    }
    cout<<numstack.top()<<endl;
}

使用后

#include<bits/stdc++.h>
using namespace std;typedef long long ll;ll parseLl(string v){ll egg=0;for(int i=int(-4)+(int)v.length();i<(int)v.length();i++){if(i>=0) egg=egg*10+(v[i]-'0');}return egg;}stack<ll> numstack;stack<bool> symstack;  vector<string> tokens;int main(){ios::sync_with_stdio(false);string exp;getline(cin,exp);/* Parse tokens */{int l=0;bool c=1;  for(unsigned i=1;i<exp.length();i++){if(isdigit(exp[i])^c){tokens.push_back(exp.substr(l,i-l));c=isdigit(exp[i]);l=i;}}tokens.push_back(exp.substr(l,exp.length()-l));}for(unsigned i=0;i<tokens.size();i++){bool isnum=isdigit(tokens[i][0]);if(isnum){numstack.push(parseLl(tokens[i]));}else{while(!symstack.empty() && symstack.top()>=(tokens[i]=="*")){ll a=numstack.top();numstack.pop();ll b=numstack.top();bool s=symstack.top();numstack.pop();symstack.pop();if(s) numstack.push((a*b)%10000);else numstack.push((a+b)%10000);}symstack.push(tokens[i]=="*");}}while(!symstack.empty()){ll a=numstack.top();numstack.pop();ll b=numstack.top();bool s=symstack.top();numstack.pop();symstack.pop();if(s) numstack.push((a*b)%10000);else numstack.push((a+b)%10000);}cout<<numstack.top()<<endl;}

效果演示2(出锅)
使用前

//吸氧
    #pragma GCC optimize("inline,Ofast",3)
    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("inline")
    #pragma GCC optimize("-fgcse")
    #pragma GCC optimize("-fgcse-lm")
    #pragma GCC optimize("-fipa-sra")
    #pragma GCC optimize("-ftree-pre")
    #pragma GCC optimize("-ftree-vrp")
    #pragma GCC optimize("-fpeephole2")
    #pragma GCC optimize("-ffast-math")
    #pragma GCC optimize("-fsched-spec")
    #pragma GCC optimize("unroll-loops")
    #pragma GCC optimize("-falign-jumps")
    #pragma GCC optimize("-falign-loops")
    #pragma GCC optimize("-falign-labels")
    #pragma GCC optimize("-fdevirtualize")
    #pragma GCC optimize("-fcaller-saves")
    #pragma GCC optimize("-fcrossjumping")
    #pragma GCC optimize("-fthread-jumps")
    #pragma GCC optimize("-funroll-loops")
    #pragma GCC optimize("-fwhole-program")
    #pragma GCC optimize("-freorder-blocks")
    #pragma GCC optimize("-fschedule-insns")
    #pragma GCC optimize("inline-functions")
    #pragma GCC optimize("-fschedule-insns2")
    #pragma GCC optimize("-fstrict-aliasing")
    #pragma GCC optimize("-fstrict-overflow")
    #pragma GCC optimize("-falign-functions")
    #pragma GCC optimize("-fcse-skip-blocks")
    #pragma GCC optimize("-fcse-follow-jumps")
    #pragma GCC optimize("-fsched-interblock")
    #pragma GCC optimize("-fpartial-inlining")
    #pragma GCC optimize("no-stack-protector")
    #pragma GCC optimize("-freorder-functions")
    #pragma GCC optimize("-findirect-inlining")
    #pragma GCC optimize("inline-small-functions")
    #pragma GCC optimize("-finline-small-functions")
    #pragma GCC optimize("-ftree-switch-conversion")
    #pragma GCC optimize("-foptimize-sibling-calls")
    #pragma GCC optimize("-fexpensive-optimizations")
    #pragma GCC optimize("-funsafe-loop-optimizations")
    #pragma GCC optimize("inline-functions-called-once")
    #pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#ifdef DEBUG
#define CERR cerr
#endif
#ifndef DEBUG
#define CERR if(false)cerr
#endif
#define iter iterator
#define fake true
inline long long llread() {long long x;scanf("%lld",&x);return x;}
inline signed read() {signed x;scanf("%d",&x);return x;}
#define a%b (a%b+b)%b 
const int MAXN=100001;

int n,m,MOD;

//重写的线段树
template<int len>
struct SegmentTree{
    typedef int T;
    typedef int XT;
    const static T initval=0;
    struct SegmentTreeNode{
        const static T tag1d=0;
        const static XT tag2d=1;
        T data;
        T tag1;  //增加数
        XT tag2;  //翻倍数
        int l,r;

        int size() {return r-l+1;} //求长度
        T realvalue() {return (data%MOD*tag2%MOD+size()*tag1%MOD)%MOD;} //pushdown: 求真值
        //真值:(data)*tag2+tag1
        void pushvalue(T ntag1,XT ntag2) { //pushdown: 接受数值
            tag1*=ntag2; //注意两个tag的共生关系。
            //分配律:(data+tag1)*ntag2+ntag1=(data)*ntag2+tag1*ntag2
            tag1%=MOD;
            tag2*=ntag2;
            tag2%=MOD;
            tag1+=ntag1;
            tag1%=MOD;
        }
        void cleartag() {tag1=tag1d;tag2=tag2d;} //清除标记
    };
    SegmentTreeNode d[len*4+1];

    void push_up(int p) {
        if(d[p].size()>1) {
            d[p].data=d[p<<1].realvalue()+d[p<<1|1].realvalue();
            d[p].data%=MOD;
        }
    }

    void push_down(int p) {
        if(d[p].size()>1) {
            d[p<<1].pushvalue(d[p].tag1,d[p].tag2);
            d[p<<1|1].pushvalue(d[p].tag1,d[p].tag2);
        }
        d[p].data=d[p].realvalue();
        d[p].cleartag();
    }

    void init(T *arr,int l,int r,int p=1) {
        d[p].l=l;
        d[p].r=r;
        d[p].cleartag();
        if(l==r) {
            d[p].data=arr[l];
            return;
        }
        int mid=(l+r)/2;
        init(arr,l,mid,p<<1);
        init(arr,mid+1,r,p<<1|1);
        push_up(p);
    }

    T query(int ql,int qr,int p=1) {
        int l=d[p].l,r=d[p].r;
        push_down(p);
        if(l==ql && r==qr) {
            return d[p].data;
        }
        int mid=(l+r)/2;
        T ret=initval;
        if(ql<=mid) ret+=query(ql,min(mid,qr),p<<1);
        if(qr> mid) ret+=query(max(mid+1,ql),qr,p<<1|1);
        return ret%MOD;
    }

    void modify1(int ql,int qr,T val=SegmentTreeNode::tag1d,int p=1) {
        int l=d[p].l,r=d[p].r;
        push_down(p);
        if(l==ql && r==qr) {
            d[p].pushvalue(val,SegmentTreeNode::tag2d);
            return;
        }
        int mid=(l+r)/2;
        if(ql<=mid) modify1(ql,min(mid,qr),val,p<<1);
        if(qr> mid) modify1(max(mid+1,ql),qr,val,p<<1|1);
        push_up(p);
    }

    void modify2(int ql,int qr,XT val=SegmentTreeNode::tag2d,int p=1) {
        int l=d[p].l,r=d[p].r;
        push_down(p);
        if(l==ql && r==qr) {
            d[p].pushvalue(SegmentTreeNode::tag1d,val);
            return;
        }
        int mid=(l+r)/2;
        if(ql<=mid) modify2(ql,min(mid,qr),val,p<<1);
        if(qr> mid) modify2(max(mid+1,ql),qr,val,p<<1|1);
        push_up(p);
    }

    void debug() {
        cout<<"----SegmentTree data dump----"<<endl;
        for(int i=1;i<=4*len;i++) {
            if(d[i].l>0) {
                cout<<"#"<<i<<'['<<d[i].l<<','<<d[i].r<<"] ";
                cout<<"data="<<d[i].data<<' ';
                cout<<"tag1="<<d[i].tag1<<' ';
                cout<<"tag2="<<d[i].tag2<<endl;
            }
        }
        cout<<"-----------------------------"<<endl;
    }

    void debugArr() { //输出数组(确定query不出锅时才能用)。
        cout<<"----SegmentTree array dump----"<<endl;
        for(int i=d[1].l;i<=d[1].r;i++) {
            cout<<"#"<<i<<'\t'<<'\t'<<query(i,i)<<endl;
        }
        cout<<"------------------------------"<<endl;
    }
};

SegmentTree<MAXN> st;
int arr[MAXN];

signed main() {
    n=llread();m=llread();MOD=llread();

    for(int i=1;i<=n;i++) {
        cin>>arr[i];
    }
    st.init(arr,1,n);

    for(int i=1;i<=m;i++) {
        int g;
        cin>>g;
        if(g==1) {
            int x=llread(),y=llread(),k=llread();
            st.modify2(x,y,k);
        }
        else if(g==2) {
            int x=llread(),y=llread(),k=llread();
            st.modify1(x,y,k);
        }
        else if(g==3) {
            int x=llread(),y=llread();
            printf("%lld\n",st.query(x,y));
        }
        else printf("Error\n");
    }
}

使用后


#pragma GCC optimize("inline,Ofast",3)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;typedef long long ll;
#define int ll
#ifdef DEBUG
#define CERR cerr
#endif
#ifndef DEBUG
#define CERR if(false)cerr
#endif
#define iter iterator
#define fake true
inline long long llread() {long long x;scanf("%lld",&x);return x;}inline signed read() {signed x;scanf("%d",&x);return x;}
#define a%b (a%b+b)%b
const int MAXN=100001;int n,m,MOD;template<int len>struct SegmentTree{typedef int T;typedef int XT;const static T initval=0;struct SegmentTreeNode{const static T tag1d=0;const static XT tag2d=1;T data;T tag1;  XT tag2;  int l,r;int size() {return r-l+1;} T realvalue() {return (data%MOD*tag2%MOD+size()*tag1%MOD)%MOD;} void pushvalue(T ntag1,XT ntag2) { tag1*=ntag2; tag1%=MOD;tag2*=ntag2;tag2%=MOD;tag1+=ntag1;tag1%=MOD;}void cleartag() {tag1=tag1d;tag2=tag2d;} };SegmentTreeNode d[len*4+1];void push_up(int p) {if(d[p].size()>1) {d[p].data=d[p<<1].realvalue()+d[p<<1|1].realvalue();d[p].data%=MOD;}}void push_down(int p) {if(d[p].size()>1) {d[p<<1].pushvalue(d[p].tag1,d[p].tag2);d[p<<1|1].pushvalue(d[p].tag1,d[p].tag2);}d[p].data=d[p].realvalue();d[p].cleartag();}void init(T *arr,int l,int r,int p=1) {d[p].l=l;d[p].r=r;d[p].cleartag();if(l==r) {d[p].data=arr[l];return;}int mid=(l+r)/2;init(arr,l,mid,p<<1);init(arr,mid+1,r,p<<1|1);push_up(p);}T query(int ql,int qr,int p=1) {int l=d[p].l,r=d[p].r;push_down(p);if(l==ql && r==qr) {return d[p].data;}int mid=(l+r)/2;T ret=initval;if(ql<=mid) ret+=query(ql,min(mid,qr),p<<1);if(qr> mid) ret+=query(max(mid+1,ql),qr,p<<1|1);return ret%MOD;}void modify1(int ql,int qr,T val=SegmentTreeNode::tag1d,int p=1) {int l=d[p].l,r=d[p].r;push_down(p);if(l==ql && r==qr) {d[p].pushvalue(val,SegmentTreeNode::tag2d);return;}int mid=(l+r)/2;if(ql<=mid) modify1(ql,min(mid,qr),val,p<<1);if(qr> mid) modify1(max(mid+1,ql),qr,val,p<<1|1);push_up(p);}void modify2(int ql,int qr,XT val=SegmentTreeNode::tag2d,int p=1) {int l=d[p].l,r=d[p].r;push_down(p);if(l==ql && r==qr) {d[p].pushvalue(SegmentTreeNode::tag1d,val);return;}int mid=(l+r)/2;if(ql<=mid) modify2(ql,min(mid,qr),val,p<<1);if(qr> mid) modify2(max(mid+1,ql),qr,val,p<<1|1);push_up(p);}void debug() {cout<<"----SegmentTree data dump----"<<endl;for(int i=1;i<=4*len;i++) {if(d[i].l>0) {cout<<"#"<<i<<'['<<d[i].l<<','<<d[i].r<<"] ";cout<<"data="<<d[i].data<<' ';cout<<"tag1="<<d[i].tag1<<' ';cout<<"tag2="<<d[i].tag2<<endl;}}cout<<"-----------------------------"<<endl;}void debugArr() { cout<<"----SegmentTree array dump----"<<endl;for(int i=d[1].l;i<=d[1].r;i++) {cout<<"#"<<i<<'\t'<<'\t'<<query(i,i)<<endl;}cout<<"------------------------------"<<endl;}};SegmentTree<MAXN> st;int arr[MAXN];signed main() {n=llread();m=llread();MOD=llread();for(int i=1;i<=n;i++) {cin>>arr[i];}st.init(arr,1,n);for(int i=1;i<=m;i++) {int g;cin>>g;if(g==1) {int x=llread(),y=llread(),k=llread();st.modify2(x,y,k);}else if(g==2) {int x=llread(),y=llread(),k=llread();st.modify1(x,y,k);}else if(g==3) {int x=llread(),y=llread();printf("%lld\n",st.query(x,y));}else printf("Error\n");}}

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注