Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <algorithm>
- #include <string.h>
- using namespace std;
- int n,m;
- int tree[20*100010][3];
- int needsUp[20*100010];
- int qVec[3];
- void init(int root, int l, int r, int idx) {
- if (l==r) {
- tree[root][0]=1;
- return;
- }
- int mid=(l+r)/2;
- if (idx<=mid) init(2*root+1,l,mid,idx);
- else init(2*root+2,mid+1,r,idx);
- for (int i=0; i<3; i++)
- tree[root][i]=tree[2*root+1][i]+tree[2*root+2][i];
- }
- inline void shift(int root, int qtd) {
- int tmp[3];
- for (int i=0; i<3; i++)
- tmp[(i+qtd)%3]=tree[root][i];
- for (int i=0; i<3; i++)
- tree[root][i]=tmp[i];
- }
- inline void propagate(int root, int l, int r) {
- shift(root,needsUp[root]);
- if (l<r) {
- needsUp[2*root+1]+=needsUp[root];
- needsUp[2*root+2]+=needsUp[root];
- }
- needsUp[root]=0;
- }
- void update(int root, int l, int r, int ql, int qr) {
- if (ql<=l && qr>=r) {
- shift(root,1);
- if (l<r) {
- needsUp[2*root+1]++;
- needsUp[2*root+2]++;
- }
- return;
- }
- int mid=(l+r)/2;
- if (ql<=mid) update(2*root+1,l,mid,ql,min(qr,mid));
- if (qr>mid) update(2*root+2,mid+1,r,max(mid+1,ql),qr);
- propagate(2*root+1,l,mid);
- propagate(2*root+2,mid+1,r);
- for (int i=0; i<3; i++)
- tree[root][i]=tree[2*root+1][i]+tree[2*root+2][i];
- }
- void query(int root, int l, int r, int ql, int qr) {
- propagate(root,l,r);
- if (ql<=l && qr>=r) {
- for (int i=0; i<3; i++)
- qVec[i]+=tree[root][i];
- return;
- }
- int mid=(l+r)/2;
- if (ql<=mid) query(2*root+1,l,mid,ql,min(qr,mid));
- if (qr>mid) query(2*root+2,mid+1,r,max(mid+1,ql),qr);
- }
- int main() {
- while (scanf("%d %d", &n,&m)!=EOF) {
- memset(tree,0,sizeof(tree));
- memset(needsUp,0,sizeof(needsUp));
- for (int i=0; i<n; i++)
- init(0,0,n-1,i);
- for (int i=0; i<m; i++) {
- char c; int a,b;
- scanf(" %c %d %d", &c,&a,&b);
- a--, b--;
- if (c=='M')
- update(0,0,n-1,a,b);
- else {
- for (int j=0; j<3; j++)
- qVec[j]=0;
- query(0,0,n-1,a,b);
- printf("%d %d %d\n", qVec[0],
- qVec[1], qVec[2]);
- }
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement