Guest User

Untitled

a guest
Jul 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define mod 10056
  4. #define pb push_back
  5. #define X first
  6. #define Y second
  7. using namespace std;
  8.  
  9. typedef pair<int,int> P;
  10.  
  11. int n,q;
  12. int step[200005];
  13. int smt1[2][800005];
  14. int smt2[2][800005];
  15.  
  16. void build1(int idx,int l,int r){
  17. if(l==r){
  18. if(step[l]==1) {
  19. smt1[0][idx]=l;
  20. smt1[1][idx]=l;
  21. }
  22. else{
  23. smt1[0][idx]=n+1;
  24. smt1[1][idx]=0;
  25. }
  26. return;
  27. }
  28. int m=(l+r)/2;
  29. build1(idx<<1,l,m);
  30. build1(idx<<1|1,m+1,r);
  31. smt1[0][idx]=min(smt1[0][idx<<1],smt1[0][idx<<1|1]);
  32. smt1[1][idx]=max(smt1[1][idx<<1],smt1[1][idx<<1|1]);
  33. }
  34.  
  35. void build2(int idx,int l,int r){
  36. if(l==r){
  37. if(step[l]==2) {
  38. smt2[0][idx]=l;
  39. smt2[1][idx]=l;
  40. }
  41. else{
  42. smt2[0][idx]=n+1;
  43. smt2[1][idx]=0;
  44. }
  45. return;
  46. }
  47. int m=(l+r)/2;
  48. build2(idx<<1,l,m);
  49. build2(idx<<1|1,m+1,r);
  50. smt2[0][idx]=min(smt2[0][idx<<1],smt2[0][idx<<1|1]);
  51. smt2[1][idx]=max(smt2[1][idx<<1],smt2[1][idx<<1|1]);
  52. }
  53.  
  54. P query1(int idx,int l,int r,int a,int b){
  55. if(r<a||l>b) return P(n+1,0);
  56. if(l>=a&&r<=b) return P(smt1[0][idx],smt1[1][idx]);
  57. int m=(l+r)/2;
  58. P p1=query1(idx<<1,l,m,a,b);
  59. P p2=query1(idx<<1|1,m+1,r,a,b);
  60. return P(min(p1.X,p2.X),max(p1.Y,p2.Y));
  61. }
  62.  
  63. P query2(int idx,int l,int r,int a,int b){
  64. if(r<a||l>b) return P(n+1,0);
  65. if(l>=a&&r<=b) return P(smt2[0][idx],smt2[1][idx]);
  66. int m=(l+r)/2;
  67. P p1=query2(idx<<1,l,m,a,b);
  68. P p2=query2(idx<<1|1,m+1,r,a,b);
  69. return P(min(p1.X,p2.X),max(p1.Y,p2.Y));
  70. }
  71.  
  72. void change1(int idx,int l,int r,int a){//呼叫這個之前要先修改陣列 step
  73. if(l>a||r<a) return;
  74. if(l==r&&l==a){
  75. if(step[l]==1) {
  76. smt1[0][idx]=l;
  77. smt1[1][idx]=l;
  78. }
  79. else{
  80. smt1[0][idx]=n+1;
  81. smt1[1][idx]=0;
  82. }
  83. return;
  84. }
  85. int m=(l+r)/2;
  86. change1(idx<<1,l,m,a);
  87. change1(idx<<1|1,m+1,r,a);
  88. smt1[0][idx]=min(smt1[0][idx<<1],smt1[0][idx<<1|1]);
  89. smt1[1][idx]=max(smt1[1][idx<<1],smt1[1][idx<<1|1]);
  90. }
  91.  
  92. void change2(int idx,int l,int r,int a){//呼叫這個之前要先修改陣列 step
  93. if(l>a||r<a) return;
  94. if(l==r&&l==a){
  95. if(step[l]==2) {
  96. smt2[0][idx]=l;
  97. smt2[1][idx]=l;
  98. }
  99. else{
  100. smt2[0][idx]=n+1;
  101. smt2[1][idx]=0;
  102. }
  103. return;
  104. }
  105. int m=(l+r)/2;
  106. change2(idx<<1,l,m,a);
  107. change2(idx<<1|1,m+1,r,a);
  108. smt2[0][idx]=min(smt2[0][idx<<1],smt2[0][idx<<1|1]);
  109. smt2[1][idx]=max(smt2[1][idx<<1],smt2[1][idx<<1|1]);
  110. }
  111.  
  112. set<int> s;
  113. int cnt[200005];
  114.  
  115. void cancel(int x){
  116. cnt[x]--;
  117. if(cnt[x]==0) s.erase(x);
  118. }
  119.  
  120. void add(int x){
  121. cnt[x]++;
  122. s.insert(x);
  123. }
  124.  
  125. int main(){
  126. while(scanf("%d%d",&n,&q)!=EOF){
  127. for(int i=1;i<=n;i++){
  128. if(i&1) step[i]=1;
  129. else step[i]=2;
  130. }
  131. build1(1,1,n);
  132. build2(1,1,n);
  133.  
  134. memset(cnt,0,sizeof(cnt));
  135. cnt[1]=n;
  136. s.clear();
  137. s.insert(1);
  138.  
  139. while(q--){
  140. int x;
  141. scanf("%d",&x);
  142.  
  143. int OL,OR;
  144. if(step[x]==1) OL=(x==1)? 1:query2(1,1,n,1,x-1).Y+1;
  145. else OL=(x==1)? 1:query1(1,1,n,1,x-1).Y+1;
  146. if(step[x]==1) OR=(x==n)? n:query2(1,1,n,x+1,n).X-1;
  147. else OR=(x==n)? n:query1(1,1,n,x+1,n).X-1;
  148.  
  149. step[x]=(step[x]==1)? 2:1;
  150. change1(1,1,n,x);
  151. change2(1,1,n,x);
  152.  
  153. int NL,NR;
  154. if(step[x]==1) NL=(x==1)? 1:query2(1,1,n,1,x-1).Y+1;
  155. else NL=(x==1)? 1:query1(1,1,n,1,x-1).Y+1;
  156. if(step[x]==1) NR=(x==n)? n:query2(1,1,n,x+1,n).X-1;
  157. else NR=(x==n)? n:query1(1,1,n,x+1,n).X-1;
  158.  
  159. //printf("%d %d %d %d\n",OL,OR,NL,NR);
  160.  
  161. if(OL>=NL&&OR<=NR){
  162. cancel(OL-NL);
  163. cancel(NR-OR);
  164. cancel(OR-OL+1);
  165. add(NR-NL+1);
  166. }
  167. else if(OL<=NL&&OR>=NR){
  168. cancel(OR-OL+1);
  169. add(NL-OL);
  170. add(OR-NR);
  171. add(NR-NL+1);
  172. }
  173. else if(OL<=NL&&OR<=NR){
  174. cancel(OR-OL+1);
  175. cancel(NR-OR);
  176. add(NL-OL);
  177. add(NR-NL+1);
  178. }
  179. else if(OL>=NL&&OR>=NR){
  180. cancel(OL-NL);
  181. cancel(OR-OL+1);
  182. add(NR-NL+1);
  183. add(OR-NR);
  184. }
  185. set<int>::iterator iter=s.end();
  186. iter--;
  187. printf("%d\n",*iter);
  188.  
  189. }
  190.  
  191. }
  192.  
  193. return 0;
  194. }
Add Comment
Please, Sign In to add comment