Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define n 100000
- using namespace std;
- typedef long long int in;
- in a[n+1],c[n+1],t[(4*n)+4]; // c- checker array - c[i]=1 if 'i' got 3 set bits
- void update(in x, in s, in e, in l, in r)
- {
- in tem;
- if(s==e) {
- a[l]=r;
- tem=__builtin_popcountll(r);
- if(tem==3)
- t[x]=c[l]=1;
- else {
- if(c[l]==1)
- c[l]=t[x]=0;
- }
- return;
- }
- in mid=(s+e)/2;
- in p=(2*x);
- in q=p+1;
- if(s<=l && l<=mid)
- update(p,s,mid,l,r);
- else
- update(q,mid+1,e,l,r);
- if(c[mid]==c[mid+1] && c[mid]==1)
- t[x]=t[p]+t[q];
- else
- t[x]=max(t[p],t[q]);
- return;
- }
- in query(in x, in s, in e, in l, in r) //completely fine
- {
- if(s>r || e<l)
- return 0;
- if(l<=s && e<=r)
- return t[x];
- in mid,p,q,lt,rt; //lt-left, rt-right
- mid =(s+e)/2;
- p=(2*x); q=p+1;
- lt=query(p,s,mid,l,r);
- rt=query(q,mid+1,e,l,r);
- if(c[mid]==c[mid+1] && c[mid]==1)
- return lt+rt;
- else
- return max(lt,rt);
- }
- int main()
- {
- in m,q,x,i,t1,l,r;
- scanf("%lld %lld", &m, &q);
- assert(1<=m && m<=100000);
- assert(1<=q && q<=100000);
- //no build function reqd since initial value zero
- while(q--) {
- scanf("%lld %lld %lld", &x, &l, &r);
- assert(1<=x && x<=2);
- if(x==1) {
- assert(1<=l && l<=m);
- assert(1<=r && r<=62); //2 power 63 exceeds long long range
- t1=(a[l-1]^((1LL)<<(r-1)));
- update(1,0,m-1,l-1,t1);
- }
- else {
- assert(1<=r && r<=m);
- assert(1<=l && l<=r);
- t1=query(1,0,m-1,l-1,r-1);
- cout<<t1<<"\n";
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment