View Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 #define N 100010 6 #define lson l,mid,p<<1 7 #define rson mid+1,r,p<<1|1 8 #define demid int mid=(l+r)>>1 9 10 int segt[N<<2][3],col[N<<2]; 11 12 void cal(int p){ 13 int tmp=segt[p][0]; 14 segt[p][0]=segt[p][2]; 15 segt[p][2]=segt[p][1]; 16 segt[p][1]=tmp; 17 } 18 19 void pushup(int p){ 20 for(int i=0;i<3;i++) segt[p][i]=segt[p<<1][i]+segt[p<<1|1][i]; 21 } 22 23 void pushdown(int p){ 24 if(col[p]){ 25 col[p]%=3; 26 col[p<<1]+=col[p]; 27 col[p<<1|1]+=col[p]; 28 for(int i=0;i<col[p];i++) cal(p<<1),cal(p<<1|1); 29 col[p]=0; 30 } 31 } 32 33 void build(int l,int r,int p){ 34 if(l==r){ 35 segt[p][0]=1; 36 return; 37 } 38 demid; 39 build(lson); 40 build(rson); 41 pushup(p); 42 } 43 44 void update(int L,int R,int l,int r,int p){ 45 //printf("%d %d %d %d\n",l,r,p,segt[p][0]); 46 if(L<=l && r<=R){ 47 col[p]++; 48 cal(p); 49 return; 50 } 51 demid; 52 pushdown(p); 53 if(L<=mid) update(L,R,lson); 54 if(mid<R) update(L,R,rson); 55 pushup(p); 56 } 57 58 int query(int L,int R,int l,int r,int p){ 59 if(L<=l && r<=R) return segt[p][0]; 60 demid; 61 pushdown(p); 62 int res=0; 63 if(L<=mid) res+=query(L,R,lson); 64 if(mid<R) res+=query(L,R,rson); 65 return res; 66 } 67 68 int main(){ 69 int t,cas=1; 70 cin>>t; 71 while(t--){ 72 memset(segt,0,sizeof(segt)); 73 memset(col,0,sizeof(col)); 74 int n,m; 75 cin>>n>>m; 76 build(1,n,1); 77 printf("Case %d:\n",cas++); 78 while(m--){ 79 int key,i,j; 80 scanf("%d%d%d",&key,&i,&j); 81 i++,j++; 82 if(key) printf("%d\n",query(i,j,1,n,1)); 83 else update(i,j,1,n,1); 84 } 85 } 86 return 0; 87 } 88 /* 89 555 90 4 3 91 0 0 2 92 0 1 3 93 1 0 3 94 */
题目如下:
You have an array with n elements which is indexed from 0 to n - 1. Initially all elements are zero. Now you have to deal with two types of operations
1. Increase the numbers between indices i and j (inclusive) by 1. This is represented by the command '0 i j'.
2. Answer how many numbers between indices i and j (inclusive) are divisible by 3. This is represented by the command '1 i j'.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form '0 i j' or '1 i j' where i, j are integers and 0 ≤ i ≤ j < n.
Output
For each case, print the case number first. Then for each query in the form '1 i j', print the desired result.
Sample Input |
Output for Sample Input |
|
1 10 9 0 0 9 0 3 7 0 1 4 1 1 7 0 2 2 1 2 4 1 8 8 0 5 8 1 6 9 |
Case 1: 2 3 0 2 |
题意:两个操作,0 i j,代表i到j的值全部+1,1 i j输出i到j之间有多少个数%3==0;
线段树,区间更新,区间查询,维护3个值,%3==0,%3==1,%3==2的数的个数。
简单题,记得区域赛挺常出的

浙公网安备 33010602011771号