hdu 4578 Transformation 2013ACM-ICPC杭州赛区全国邀请赛

来源:互联网 发布:淘宝9秒视频在线制作 编辑:程序博客网 时间:2024/04/29 10:27

TLE 代码(错在询问操作不必更新到叶节点):

#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<queue>#include<stack>#include<set>#include<map>#define CLEAR(a) memset((a),0,sizeof((a)))using namespace std;typedef long long LL;const double pi = acos(-1.0);const int maxn=1e5+10;const int inf=99999999;const double eps=1e-3;const int mod=10007;struct Node{    int l,r,mid;    LL v,add,mul;} SegTree[maxn*4];void build(int rt,int l,int r){    SegTree[rt].l=l;SegTree[rt].r=r;    SegTree[rt].mid=(l+r)>>1;    SegTree[rt].add=SegTree[rt].v=0;    SegTree[rt].mul=1;    if (l<r)    {        build(rt<<1,l,SegTree[rt].mid);        build(rt<<1|1,SegTree[rt].mid+1,r);    }}void add(int rt,int x,int y,int num){    int &l=SegTree[rt].l,&r=SegTree[rt].r,&mid=SegTree[rt].mid;    if (x>y) return;    //cout<<"+ "<<rt<<' '<<l<<' '<<r<<' '<<mid<<"  "<<x<<' '<<y<<' '<<num<<endl;    if (x<=SegTree[rt].l&&SegTree[rt].r<=y)    {        SegTree[rt].add+=num;        SegTree[rt].add%=mod;        return;    }    int tmp=SegTree[rt].add;    if (SegTree[rt].add)    {        SegTree[rt].add=0;        add(rt,SegTree[rt].l,x-1,tmp);        add(rt,y+1,SegTree[rt].r,tmp);    }    if (x<=mid) add(rt<<1,x,min(mid,y),tmp+num);    if (y>mid) add(rt<<1|1,max(mid+1,x),y,tmp+num);}void mul(int rt,int x,int y,int num){    int &l=SegTree[rt].l,&r=SegTree[rt].r,&mid=SegTree[rt].mid;    if (x>y) return;    if (x<=SegTree[rt].l&&SegTree[rt].r<=y)    {        SegTree[rt].mul*=num;SegTree[rt].mul%=mod;        SegTree[rt].add*=num;SegTree[rt].add%=mod;        return;    }    int tmp=SegTree[rt].mul;    if (SegTree[rt].mul!=1)    {        SegTree[rt].mul=1;        add(rt,SegTree[rt].l,x-1,SegTree[rt].add*tmp);        mul(rt,SegTree[rt].l,x-1,tmp);        add(rt,x,y,SegTree[rt].add*tmp*num);        add(rt,y+1,SegTree[rt].r,SegTree[rt].add*tmp);        mul(rt,y+1,SegTree[rt].r,tmp);    }    if (x<=mid) mul(rt<<1,x,min(mid,y),tmp*num);    if (y>mid) mul(rt<<1|1,max(mid+1,x),y,tmp*num);}void cover(int rt,int x,int y,int num){    int &l=SegTree[rt].l,&r=SegTree[rt].r,&mid=SegTree[rt].mid;    if (x>y) return;    //cout<<"c "<<rt<<' '<<l<<' '<<r<<' '<<mid<<"  "<<x<<' '<<y<<' '<<num<<endl;    if (x<=SegTree[rt].l&&SegTree[rt].r<=y)    {        SegTree[rt].v=num;        SegTree[rt].add=0;        SegTree[rt].mul=1;        return;    }    if (SegTree[rt].add)    {        int tmp=SegTree[rt].add;        SegTree[rt].add=0;        add(rt,SegTree[rt].l,x-1,tmp);        add(rt,y+1,SegTree[rt].r,tmp);    }    if (SegTree[rt].mul!=1)    {        int tmp=SegTree[rt].mul;        SegTree[rt].mul=1;        mul(rt,SegTree[rt].l,x-1,tmp);        mul(rt,y+1,SegTree[rt].r,tmp);    }    if (x<=mid) cover(rt<<1,x,min(mid,y),num);    if (y>mid) cover(rt<<1|1,max(mid+1,x),y,num);}LL query(int rt,int x,int y,int p){    int &l=SegTree[rt].l,&r=SegTree[rt].r,&mid=SegTree[rt].mid;    //cout<<"q "<<rt<<' '<<l<<' '<<r<<' '<<mid<<"  "<<SegTree[rt].v<<' '<<SegTree[rt].add<<' '<<SegTree[rt].mul<<endl; //<<"  "<<x<<' '<<y<<' '<<endl;    if (SegTree[rt].l==SegTree[rt].r)    //if (SegTree[rt].v)    {        SegTree[rt].v*=SegTree[rt].mul;SegTree[rt].v%=mod;        SegTree[rt].v+=SegTree[rt].add;SegTree[rt].v%=mod;        SegTree[rt].add=0;        SegTree[rt].mul=1;        switch(p)        {            case 1:return SegTree[rt].v;            case 2:return SegTree[rt].v*SegTree[rt].v%mod;            case 3:return SegTree[rt].v*SegTree[rt].v*SegTree[rt].v%mod;        }    }    if (SegTree[rt].v)    {        add(rt<<1,l,mid,SegTree[rt].v*SegTree[rt].mul+SegTree[rt].add);        add(rt<<1|1,mid+1,r,SegTree[rt].v*SegTree[rt].mul+SegTree[rt].add);        SegTree[rt].v=0;    }    if (SegTree[rt].add)    {        //puts("update");        add(rt<<1,l,mid,SegTree[rt].add);        add(rt<<1|1,mid+1,r,SegTree[rt].add);        SegTree[rt].add=0;    }    if (SegTree[rt].mul!=1)    {        mul(rt<<1,l,mid,SegTree[rt].mul);        mul(rt<<1|1,mid+1,r,SegTree[rt].mul);        SegTree[rt].mul=1;    }    return (((x<=mid)?query(rt<<1,x,min(mid,y),p):0)+((y>mid)?query(rt<<1|1,max(mid+1,x),y,p):0))%mod;}LL fnd(int rt,int x){    if(SegTree[rt].l==SegTree[rt].r) return SegTree[rt].v;    else if (x<=SegTree[rt].mid) return fnd(rt<<1,x);    else return fnd(rt<<1|1,x);}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        if (!n&&!m) break;        build(1,1,n);        for(int i=1;i<=m;i++)        {            int op,x,y,c;            scanf("%d%d%d%d",&op,&x,&y,&c);            switch(op)            {                case 1:                    add(1,x,y,c);//query(1,x,y,1);                    break;                case 2:                    mul(1,x,y,c);//query(1,x,y,1);                    break;                case 3:                    cover(1,x,y,c);//query(1,x,y,1);                    break;                case 4:                    printf("%I64d\n",query(1,x,y,c));                    break;            }            //for(int i=1;i<=n;i++) printf("%lld ",fnd(1,i));printf("\n");        }    }    return 0;}



AC 4882ms:

#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<queue>#include<stack>#include<set>#include<map>#define CLEAR(a) memset((a),0,sizeof((a)))using namespace std;typedef long long LL;const double pi = acos(-1.0);const int maxn = 1e5 + 10;const int inf = 99999999;const double eps = 1e-3;const int mod = 10007;struct Node{    int l, r, mid;    LL v, add, mul;    bool up;} SegTree[maxn * 4];void build(int rt, int l, int r){    SegTree[rt].l = l;    SegTree[rt].r = r;    SegTree[rt].mid = (l + r) >> 1;    SegTree[rt].add = SegTree[rt].v = 0;    SegTree[rt].mul = 1;    if (l < r)    {        SegTree[rt].up = 0;        build(rt << 1, l, SegTree[rt].mid);        build(rt << 1 | 1, SegTree[rt].mid + 1, r);    }    else    {        SegTree[rt].up = 1;    }}void pushdown(int rt){    int mid = SegTree[rt].mid;    if (SegTree[rt].up)    {        SegTree[rt << 1].add = SegTree[rt << 1 | 1].add = 0;        SegTree[rt << 1].mul = SegTree[rt << 1 | 1].mul = 1;        SegTree[rt << 1].v = SegTree[rt << 1 | 1].v = SegTree[rt].v;        SegTree[rt << 1].up = SegTree[rt << 1 | 1].up = 1;        SegTree[rt].up = 0;    }    else    {        if (SegTree[rt].add)        {            if (SegTree[rt << 1].up)            {                SegTree[rt << 1].v += SegTree[rt].add;                SegTree[rt << 1].v %= mod;            }            else            {                pushdown(rt << 1);                SegTree[rt << 1].add += SegTree[rt].add;                SegTree[rt << 1].add %= mod;            }            if (SegTree[rt << 1 | 1].up)            {                SegTree[rt << 1 | 1].v += SegTree[rt].add;                SegTree[rt << 1 | 1].v %= mod;            }            else            {                pushdown(rt << 1 | 1);                SegTree[rt << 1 | 1].add += SegTree[rt].add;                SegTree[rt << 1 | 1].add %= mod;            }            SegTree[rt].add = 0;        }        if (SegTree[rt].mul != 1)        {            if (SegTree[rt << 1].up)            {                SegTree[rt << 1].v *= SegTree[rt].mul;                SegTree[rt << 1].v %= mod;            }            else            {                pushdown(rt << 1);                SegTree[rt << 1].mul *= SegTree[rt].mul;                SegTree[rt << 1].mul %= mod;            }            if (SegTree[rt << 1 | 1].up)            {                SegTree[rt << 1 | 1].v *= SegTree[rt].mul;                SegTree[rt << 1 | 1].v %= mod;            }            else            {                pushdown(rt << 1 | 1);                SegTree[rt << 1 | 1].mul *= SegTree[rt].mul;                SegTree[rt << 1 | 1].mul %= mod;            }            SegTree[rt].mul = 1;        }    }}void update(int rt, int x, int y, LL num,int op){    int& l = SegTree[rt].l, &r = SegTree[rt].r, &mid = SegTree[rt].mid;    //cout<<"+ "<<rt<<' '<<l<<' '<<r<<' '<<mid<<"  "<<x<<' '<<y<<' '<<num<<endl;    if (x <= SegTree[rt].l && SegTree[rt].r <= y)    {        if (op==3)        {            SegTree[rt].add=0;            SegTree[rt].mul=1;            SegTree[rt].v=num;            SegTree[rt].up=1;        }        else        {            if (SegTree[rt].up)            {                if (op==1)                {                    SegTree[rt].v+=num;SegTree[rt].v%=mod;                }                else                {                    SegTree[rt].v*=num;SegTree[rt].v%=mod;                }            }            else            {                pushdown(rt);                if (op==1)                {                    SegTree[rt].add+=num;SegTree[rt].add%=mod;                }                else                {                    SegTree[rt].mul*=num;SegTree[rt].mul%=mod;                }            }        }        return;    }    pushdown(rt);    if (x <= mid)        update(rt << 1, x, min(mid, y), num,op);    if (y > mid)        update(rt << 1 | 1, max(mid + 1, x), y, num,op);}LL query(int rt, int x, int y, int p){    int& l = SegTree[rt].l, &r = SegTree[rt].r, &mid = SegTree[rt].mid;    //cout<<"q "<<rt<<' '<<l<<' '<<r<<' '<<mid<<"  "<<SegTree[rt].v<<' '<<SegTree[rt].add<<' '<<SegTree[rt].mul<<endl; //<<"  "<<x<<' '<<y<<' '<<endl;    if (x <= SegTree[rt].l && SegTree[rt].r <= y&&SegTree[rt].up)    {        LL tmp=LL(r-l+1)%mod;        for(int i=1;i<=p;i++)        {            tmp*=SegTree[rt].v;tmp%=mod;        }        return tmp;    }    pushdown(rt);    return (((x <= mid) ? query(rt << 1, x, min(mid, y), p) : 0LL) + ((y > mid) ? query(rt << 1 | 1, max(mid + 1, x), y, p) : 0LL)) % mod;}LL fnd(int rt, int x){    if (SegTree[rt].l == SegTree[rt].r)    {        return SegTree[rt].v;    }    else if (x <= SegTree[rt].mid)    {        return fnd(rt << 1, x);    }    else    {        return fnd(rt << 1 | 1, x);    }}int main(){    int n, m;    while (~scanf("%d%d", &n, &m))    {        if (!n && !m)        {            break;        }        build(1, 1, n);        for (int i = 1; i <= m; i++)        {            int op, x, y;            LL c;            scanf("%d%d%d%I64d", &op, &x, &y, &c);            switch (op)            {                case 1:                case 2:                case 3:                    update(1, x, y, c,op); //query(1,x,y,1);                    break;                case 4:                    printf("%I64d\n", query(1, x, y, c));                    break;            }            //for(int i=1;i<=n;i++) printf("%lld ",fnd(1,i));printf("\n");        }    }    return 0;}


0 0
原创粉丝点击