SPOJ 7299 Multiples of 3

来源:互联网 发布:网络诈骗的形式包括 编辑:程序博客网 时间:2024/05/16 00:52

Description

There are N numbers a[0],a[1]..a[N - 1]. Initally all are 0. You have to perform two types of operations :

1) Increase the numbers between indices A and B (inclusive) by 1. This is represented by the command "0 A B"
2) Answer how many numbers between indices A and B (inclusive) are divisible by 3. This is represented by the command "1 A B".

Input :
The first line contains two integers, N and Q. Each of the next Q lines are either of the form "0 A B" or "1 A B" as mentioned above.

Output :
Output 1 line for each of the queries of the form "1 A B" containing the required answer for the corresponding query.

Sample Input :
4 7
1 0 3
0 1 2
0 1 3
1 0 0
0 0 3
1 3 3
1 0 3

Sample Output :
4
1
0
2

Constraints :

1 <= N <= 100000
1 <= Q <= 100000
0 <= A <= B <= N - 1

题意:有一个长为N(N<=10^5)的数组,初始时数组每个元素的值都为0,接下来有Q(Q<=10^5)次操作,每次操作有2中类型:
        0 A B 将[A,B]区间内的所有数字+1
        1 A B 输出[A,B]区间内所有能够被3整除的数组元素的个数
考察内容:
        线段树的基本应用
        区间维护
        Lazy标记的使用
每次修改时,如果某个节点表示的区间[x,y]被加了1,那么我们要对这个区间的3个sum属性进行修改,同时在这个区间打上lazy标记,表示这个区间已经被修改了。
当需要访问这个节点下面的节点的时候,一定会先经过这个点,这时候下传lazy标记,来保证子节点的信息是正确的。
#include <cstdio>const int maxn = 100010;int sum[maxn<<2][3],add[maxn<<2];void pushup(int cur){    int ls = cur<<1,rs = cur<<1|1;    sum[cur][0] = sum[ls][0]+sum[rs][0];    sum[cur][1] = sum[ls][1]+sum[rs][1];    sum[cur][2] = sum[ls][2]+sum[rs][2];}void pushdown(int cur){    int ls = cur<<1,rs = cur<<1|1,tmp;    if(add[cur] != 0){        add[ls] = (add[ls]+add[cur])%3;        add[rs] = (add[rs]+add[cur])%3;        if(add[cur] == 1){            tmp = sum[ls][2];            sum[ls][2] = sum[ls][1];            sum[ls][1] = sum[ls][0];            sum[ls][0] = tmp;        }else if(add[cur] == 2){            tmp = sum[ls][0];            sum[ls][0] = sum[ls][1];            sum[ls][1] = sum[ls][2];            sum[ls][2] = tmp;        }        if(add[cur] == 1){            tmp = sum[rs][2];            sum[rs][2] = sum[rs][1];            sum[rs][1] = sum[rs][0];            sum[rs][0] = tmp;        }else if(add[cur] == 2){            tmp = sum[rs][0];            sum[rs][0] = sum[rs][1];            sum[rs][1] = sum[rs][2];            sum[rs][2] = tmp;        }        add[cur] = 0;    }}void bulid(int cur,int x,int y){    int mid = (x+y)>>1,ls = cur<<1,rs = cur<<1|1;    if(x == y){        sum[cur][0] = 1;        return;    }    bulid(ls,x,mid);    bulid(rs,mid+1,y);    pushup(cur);}void update(int cur,int x,int y,int s,int t){    int mid = (x+y)>>1,ls = cur<<1,rs = cur<<1|1;    if(x >= s && y <= t){        int tmp = sum[cur][2];        sum[cur][2] = sum[cur][1];        sum[cur][1] = sum[cur][0];        sum[cur][0] = tmp;        add[cur] = (add[cur]+1)%3;        return;    }    pushdown(cur);    if(mid >= s)    update(ls,x,mid,s,t);    if(mid+1 <= t)  update(rs,mid+1,y,s,t);    pushup(cur);}void query(int cur,int x,int y,int s,int t,int &ans){    int mid = (x+y)>>1,ls = cur<<1,rs = cur<<1|1;    if(x >= s && y <= t){        ans += sum[cur][0];        return;    }    pushdown(cur);    if(mid >= s)    query(ls,x,mid,s,t,ans);    if(mid+1 <= t)  query(rs,mid+1,y,s,t,ans);}int main(){    int n,q,op,a,b;    scanf("%d%d",&n,&q);    bulid(1,0,n-1);    while(q--){        scanf("%d%d%d",&op,&a,&b);        if(op == 0) update(1,0,n-1,a,b);        else{            int ans = 0;            query(1,0,n-1,a,b,ans);            printf("%d\n",ans);        }    }    return 0;}



0 0
原创粉丝点击