Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- */
- //#pragma comment(linker, "/STACK:16777216")
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <complex>
- #include <math.h>
- #include <set>
- #include <vector>
- #include <map>
- #include <queue>
- #include <stdio.h>
- #include <stack>
- #include <algorithm>
- #include <list>
- #include <ctime>
- #include <memory.h>
- #include <ctime>
- #define y0 sdkfaslhagaklsldk
- #define y1 aasdfasdfasdf
- #define yn askfhwqriuperikldjk
- #define j1 assdgsdgasghsf
- #define tm sdfjahlfasfh
- #define lr asgasgash
- #define eps 1e-9
- //#define M_PI 3.141592653589793
- #define bs 1000000007
- #define bsize 256
- #define right adsgasgadsg
- #define free adsgasdg
- #define MAG 10000
- using namespace std;
- string st;
- long long tests,n,m,l,r,ps1,ps2;
- long long val1,val2,val3;
- long long t[1<<20],mx[1<<20],p[1<<20];
- long long pww[2000];
- void build(long long v,long tl,long tr)
- {
- if (tl==tr)
- {
- t[v]=1;
- mx[v]=1;
- p[v]=0;
- return ;
- }
- long tm=tl+tr;
- tm/=2;
- build(v*2,tl,tm);
- build(v*2+1,tm+1,tr);
- t[v]=t[v*2]+t[v*2+1];
- mx[v]=1;
- p[v]=0;
- return;
- }
- void dopush(long long v,long long tl,long long tr)
- {
- while (p[v])
- {
- if (p[v]>100)while (true);
- long tm=tl+tr;
- tm/=2;
- if (tl<tr)
- {p[v*2]+=p[v];p[v*2+1]+=p[v];t[v*2]*=pww[p[v]];t[v*2+1]*=pww[p[v]];mx[v*2]*=pww[p[v]];mx[v*2+1]*=pww[p[v]];}//dopush(v*2,tl,tm);}//dopush(v*2+1,tm+1,tr);}
- p[v]=0;
- }
- }
- long long get_ps(long long v,long long tl,long long tr,long long x)
- {
- dopush(v,tl,tr);
- if (tl==tr)
- return tl;
- long long tm=tl+tr;
- tm/=2;
- if (t[v*2]<x)
- {
- x-=t[v*2];
- return get_ps(v*2+1,tm+1,tr,x);
- }
- else
- {
- return get_ps(v*2,tl,tm,x);
- }
- }
- void add(long long v,long long tl,long long tr,long long ps,long long val)
- {
- dopush(v,tl,tr);
- if (tl==tr)
- {
- t[v]+=val;
- mx[v]+=val;
- return ;
- }
- long long tm=tl+tr;
- tm/=2;
- if (ps<=tm)
- add(v*2,tl,tm,ps,val);
- else
- add(v*2+1,tm+1,tr,ps,val);
- t[v]=t[v*2]+t[v*2+1];
- mx[v]=max(mx[v*2],mx[v*2+1]);
- }
- long long get_sum(long long v,long long tl,long long tr,long long l,long long r)
- {
- dopush(v,tl,tr);
- if (l>r)
- return 0;
- if (tl==l&&tr==r)
- return t[v];
- long long tm=tl+tr;
- tm/=2;
- long long v1,v2;
- v1=get_sum(v*2,tl,tm,l,min(r,tm));
- v2=get_sum(v*2+1,tm+1,tr,max(l,tm+1),r);
- t[v]=t[v*2]+t[v*2+1];
- mx[v]=max(mx[v*2],mx[v*2+1]);
- return v1+v2;
- }
- long long get_max(long long v,long long tl,long long tr,long long l,long long r)
- {
- dopush(v,tl,tr);
- if (l>r)
- return -1;
- if (tl==l&&tr==r)
- return mx[v];
- long long tm=tl+tr;
- tm/=2;
- long long r1,r2;
- r1=get_max(v*2,tl,tm,l,min(r,tm));
- r2=get_max(v*2+1,tm+1,tr,max(l,tm+1),r);
- return max(r1,r2);
- }
- void mult(long long v,long long tl,long long tr,long long l,long long r)
- {
- dopush(v,tl,tr);
- if (l>r)return;
- if (tl==l&&tr==r)
- {
- p[v]=1;
- mx[v]*=2;
- t[v]*=2;
- dopush(v,tl,tr);
- return ;
- }
- long long tm=tl+tr;
- tm/=2;
- mult(v*2,tl,tm,l,min(r,tm));
- mult(v*2+1,tm+1,tr,max(l,tm+1),r);
- t[v]=t[v*2]+t[v*2+1];
- mx[v]=max(mx[v*2],mx[v*2+1]);
- }
- long ar[1<<21];
- long N;
- map<long, long> M;
- long mxx;
- long naive_get(long l,long r)
- {
- M.clear();
- mxx=0;
- for (int i=l;i<=r;i++)
- {
- M[ar[i]]++;
- if (M[ar[i]]>mxx)
- mxx=M[ar[i]];
- }
- return mxx;
- }
- void naive_upd(long l,long r)
- {
- vector<long> vec;
- for (int i=1;i<l;i++)
- vec.push_back(ar[i]);
- for (int i=l;i<=r;i++)
- vec.push_back(ar[i]),
- vec.push_back(ar[i]);
- for (int i=r+1;i<=N;i++)
- vec.push_back(ar[i]);
- N=vec.size();
- for (int i=1;i<=N;i++)
- ar[i]=vec[i-1];
- }
- int main(){
- //freopen("hiking.in","r",stdin);
- //freopen("hiking.out","w",stdout);
- //freopen("C:/input.txt","r",stdin);
- //freopen("C:/output.txt","w",stdout);
- ios_base::sync_with_stdio(0);
- //cin.tie(0);
- pww[1]=2;
- for (int i=2;i<=100;i++)
- pww[i]=pww[i-1]*2;
- cin>>tests;
- for (;tests;--tests)
- {
- cin>>n>>m;
- N=n;
- for (int i=1;i<=n;i++)
- ar[i]=i;
- build(1,1,n);
- int cc=0;
- for (;m;--m)
- {
- ++cc;
- cin>>st;
- if (st=="D")
- {
- cin>>l>>r;
- ps1=get_ps(1,1,n,l);
- ps2=get_ps(1,1,n,r);
- if (ps1==ps2)
- {
- add(1,1,n,ps1,r-l+1);
- }
- else
- {
- val1=get_sum(1,1,n,1,ps1);
- val2=get_sum(1,1,n,1,ps2-1);
- add(1,1,n,ps1,val1-l+1);
- add(1,1,n,ps2,r-val2);
- mult(1,1,n,ps1+1,ps2-1);
- }
- }
- else
- {
- cin>>l>>r;
- ps1=get_ps(1,1,n,l);
- ps2=get_ps(1,1,n,r);
- if (ps1==ps2)
- {
- cout<<r-l+1<<endl;
- continue;
- }
- val1=get_sum(1,1,n,1,ps1);
- val1=val1-l+1;
- val2=get_sum(1,1,n,1,ps2-1);
- val2=r-val2;
- val3=get_max(1,1,n,ps1+1,ps2-1);
- cout<<max(val1,max(val2,val3))<<endl;
- }
- }
- }
- cin.get();cin.get();
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement