Advertisement
Guest User

Untitled

a guest
Feb 8th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /*
  2. */
  3.  
  4. //#pragma comment(linker, "/STACK:16777216")
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <complex>
  9. #include <math.h>
  10. #include <set>
  11. #include <vector>
  12. #include <map>
  13. #include <queue>
  14. #include <stdio.h>
  15. #include <stack>
  16. #include <algorithm>
  17. #include <list>
  18. #include <ctime>
  19. #include <memory.h>
  20. #include <ctime>
  21.  
  22. #define y0 sdkfaslhagaklsldk
  23. #define y1 aasdfasdfasdf
  24. #define yn askfhwqriuperikldjk
  25. #define j1 assdgsdgasghsf
  26. #define tm sdfjahlfasfh
  27. #define lr asgasgash
  28.  
  29. #define eps 1e-9
  30. //#define M_PI 3.141592653589793
  31. #define bs 1000000007
  32. #define bsize 256
  33. #define right adsgasgadsg
  34. #define free adsgasdg
  35. #define MAG 10000
  36.  
  37. using namespace std;
  38.  
  39. string st;
  40. long long tests,n,m,l,r,ps1,ps2;
  41. long long val1,val2,val3;
  42. long long t[1<<20],mx[1<<20],p[1<<20];
  43. long long pww[2000];
  44.  
  45. void build(long long v,long tl,long tr)
  46. {
  47. if (tl==tr)
  48. {
  49. t[v]=1;
  50. mx[v]=1;
  51. p[v]=0;
  52. return ;
  53. }
  54. long tm=tl+tr;
  55. tm/=2;
  56. build(v*2,tl,tm);
  57. build(v*2+1,tm+1,tr);
  58. t[v]=t[v*2]+t[v*2+1];
  59. mx[v]=1;
  60. p[v]=0;
  61. return;
  62. }
  63.  
  64. void dopush(long long v,long long tl,long long tr)
  65. {
  66. while (p[v])
  67. {
  68. if (p[v]>100)while (true);
  69. long tm=tl+tr;
  70. tm/=2;
  71. if (tl<tr)
  72. {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);}
  73.  
  74. p[v]=0;
  75. }
  76. }
  77.  
  78. long long get_ps(long long v,long long tl,long long tr,long long x)
  79. {
  80. dopush(v,tl,tr);
  81.  
  82. if (tl==tr)
  83. return tl;
  84. long long tm=tl+tr;
  85. tm/=2;
  86.  
  87. if (t[v*2]<x)
  88. {
  89. x-=t[v*2];
  90. return get_ps(v*2+1,tm+1,tr,x);
  91. }
  92. else
  93. {
  94. return get_ps(v*2,tl,tm,x);
  95. }
  96. }
  97.  
  98. void add(long long v,long long tl,long long tr,long long ps,long long val)
  99. {
  100. dopush(v,tl,tr);
  101. if (tl==tr)
  102. {
  103. t[v]+=val;
  104. mx[v]+=val;
  105. return ;
  106. }
  107. long long tm=tl+tr;
  108. tm/=2;
  109. if (ps<=tm)
  110. add(v*2,tl,tm,ps,val);
  111. else
  112. add(v*2+1,tm+1,tr,ps,val);
  113. t[v]=t[v*2]+t[v*2+1];
  114. mx[v]=max(mx[v*2],mx[v*2+1]);
  115. }
  116.  
  117. long long get_sum(long long v,long long tl,long long tr,long long l,long long r)
  118. {
  119. dopush(v,tl,tr);
  120. if (l>r)
  121. return 0;
  122. if (tl==l&&tr==r)
  123. return t[v];
  124. long long tm=tl+tr;
  125. tm/=2;
  126. long long v1,v2;
  127. v1=get_sum(v*2,tl,tm,l,min(r,tm));
  128. v2=get_sum(v*2+1,tm+1,tr,max(l,tm+1),r);
  129. t[v]=t[v*2]+t[v*2+1];
  130. mx[v]=max(mx[v*2],mx[v*2+1]);
  131. return v1+v2;
  132. }
  133.  
  134. long long get_max(long long v,long long tl,long long tr,long long l,long long r)
  135. {
  136. dopush(v,tl,tr);
  137. if (l>r)
  138. return -1;
  139. if (tl==l&&tr==r)
  140. return mx[v];
  141. long long tm=tl+tr;
  142. tm/=2;
  143. long long r1,r2;
  144. r1=get_max(v*2,tl,tm,l,min(r,tm));
  145. r2=get_max(v*2+1,tm+1,tr,max(l,tm+1),r);
  146. return max(r1,r2);
  147. }
  148.  
  149. void mult(long long v,long long tl,long long tr,long long l,long long r)
  150. {
  151. dopush(v,tl,tr);
  152. if (l>r)return;
  153. if (tl==l&&tr==r)
  154. {
  155. p[v]=1;
  156. mx[v]*=2;
  157. t[v]*=2;
  158. dopush(v,tl,tr);
  159. return ;
  160. }
  161. long long tm=tl+tr;
  162. tm/=2;
  163. mult(v*2,tl,tm,l,min(r,tm));
  164. mult(v*2+1,tm+1,tr,max(l,tm+1),r);
  165. t[v]=t[v*2]+t[v*2+1];
  166. mx[v]=max(mx[v*2],mx[v*2+1]);
  167. }
  168.  
  169. long ar[1<<21];
  170. long N;
  171. map<long, long> M;
  172. long mxx;
  173.  
  174. long naive_get(long l,long r)
  175. {
  176. M.clear();
  177. mxx=0;
  178. for (int i=l;i<=r;i++)
  179. {
  180. M[ar[i]]++;
  181. if (M[ar[i]]>mxx)
  182. mxx=M[ar[i]];
  183. }
  184. return mxx;
  185. }
  186.  
  187. void naive_upd(long l,long r)
  188. {
  189. vector<long> vec;
  190. for (int i=1;i<l;i++)
  191. vec.push_back(ar[i]);
  192. for (int i=l;i<=r;i++)
  193. vec.push_back(ar[i]),
  194. vec.push_back(ar[i]);
  195. for (int i=r+1;i<=N;i++)
  196. vec.push_back(ar[i]);
  197. N=vec.size();
  198. for (int i=1;i<=N;i++)
  199. ar[i]=vec[i-1];
  200. }
  201.  
  202. int main(){
  203. //freopen("hiking.in","r",stdin);
  204. //freopen("hiking.out","w",stdout);
  205. //freopen("C:/input.txt","r",stdin);
  206. //freopen("C:/output.txt","w",stdout);
  207. ios_base::sync_with_stdio(0);
  208. //cin.tie(0);
  209.  
  210. pww[1]=2;
  211. for (int i=2;i<=100;i++)
  212. pww[i]=pww[i-1]*2;
  213.  
  214. cin>>tests;
  215. for (;tests;--tests)
  216. {
  217. cin>>n>>m;
  218. N=n;
  219. for (int i=1;i<=n;i++)
  220. ar[i]=i;
  221.  
  222. build(1,1,n);
  223. int cc=0;
  224. for (;m;--m)
  225. {
  226. ++cc;
  227. cin>>st;
  228. if (st=="D")
  229. {
  230. cin>>l>>r;
  231. ps1=get_ps(1,1,n,l);
  232. ps2=get_ps(1,1,n,r);
  233. if (ps1==ps2)
  234. {
  235. add(1,1,n,ps1,r-l+1);
  236. }
  237. else
  238. {
  239. val1=get_sum(1,1,n,1,ps1);
  240. val2=get_sum(1,1,n,1,ps2-1);
  241. add(1,1,n,ps1,val1-l+1);
  242. add(1,1,n,ps2,r-val2);
  243. mult(1,1,n,ps1+1,ps2-1);
  244. }
  245. }
  246. else
  247. {
  248. cin>>l>>r;
  249. ps1=get_ps(1,1,n,l);
  250. ps2=get_ps(1,1,n,r);
  251. if (ps1==ps2)
  252. {
  253. cout<<r-l+1<<endl;
  254. continue;
  255. }
  256. val1=get_sum(1,1,n,1,ps1);
  257. val1=val1-l+1;
  258. val2=get_sum(1,1,n,1,ps2-1);
  259. val2=r-val2;
  260. val3=get_max(1,1,n,ps1+1,ps2-1);
  261. cout<<max(val1,max(val2,val3))<<endl;
  262. }
  263. }
  264. }
  265.  
  266. cin.get();cin.get();
  267. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement