Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define mod 10056
- #define pb push_back
- #define X first
- #define Y second
- using namespace std;
- typedef pair<int,int> P;
- int n,q;
- int step[200005];
- int smt1[2][800005];
- int smt2[2][800005];
- void build1(int idx,int l,int r){
- if(l==r){
- if(step[l]==1) {
- smt1[0][idx]=l;
- smt1[1][idx]=l;
- }
- else{
- smt1[0][idx]=n+1;
- smt1[1][idx]=0;
- }
- return;
- }
- int m=(l+r)/2;
- build1(idx<<1,l,m);
- build1(idx<<1|1,m+1,r);
- smt1[0][idx]=min(smt1[0][idx<<1],smt1[0][idx<<1|1]);
- smt1[1][idx]=max(smt1[1][idx<<1],smt1[1][idx<<1|1]);
- }
- void build2(int idx,int l,int r){
- if(l==r){
- if(step[l]==2) {
- smt2[0][idx]=l;
- smt2[1][idx]=l;
- }
- else{
- smt2[0][idx]=n+1;
- smt2[1][idx]=0;
- }
- return;
- }
- int m=(l+r)/2;
- build2(idx<<1,l,m);
- build2(idx<<1|1,m+1,r);
- smt2[0][idx]=min(smt2[0][idx<<1],smt2[0][idx<<1|1]);
- smt2[1][idx]=max(smt2[1][idx<<1],smt2[1][idx<<1|1]);
- }
- P query1(int idx,int l,int r,int a,int b){
- if(r<a||l>b) return P(n+1,0);
- if(l>=a&&r<=b) return P(smt1[0][idx],smt1[1][idx]);
- int m=(l+r)/2;
- P p1=query1(idx<<1,l,m,a,b);
- P p2=query1(idx<<1|1,m+1,r,a,b);
- return P(min(p1.X,p2.X),max(p1.Y,p2.Y));
- }
- P query2(int idx,int l,int r,int a,int b){
- if(r<a||l>b) return P(n+1,0);
- if(l>=a&&r<=b) return P(smt2[0][idx],smt2[1][idx]);
- int m=(l+r)/2;
- P p1=query2(idx<<1,l,m,a,b);
- P p2=query2(idx<<1|1,m+1,r,a,b);
- return P(min(p1.X,p2.X),max(p1.Y,p2.Y));
- }
- void change1(int idx,int l,int r,int a){//呼叫這個之前要先修改陣列 step
- if(l>a||r<a) return;
- if(l==r&&l==a){
- if(step[l]==1) {
- smt1[0][idx]=l;
- smt1[1][idx]=l;
- }
- else{
- smt1[0][idx]=n+1;
- smt1[1][idx]=0;
- }
- return;
- }
- int m=(l+r)/2;
- change1(idx<<1,l,m,a);
- change1(idx<<1|1,m+1,r,a);
- smt1[0][idx]=min(smt1[0][idx<<1],smt1[0][idx<<1|1]);
- smt1[1][idx]=max(smt1[1][idx<<1],smt1[1][idx<<1|1]);
- }
- void change2(int idx,int l,int r,int a){//呼叫這個之前要先修改陣列 step
- if(l>a||r<a) return;
- if(l==r&&l==a){
- if(step[l]==2) {
- smt2[0][idx]=l;
- smt2[1][idx]=l;
- }
- else{
- smt2[0][idx]=n+1;
- smt2[1][idx]=0;
- }
- return;
- }
- int m=(l+r)/2;
- change2(idx<<1,l,m,a);
- change2(idx<<1|1,m+1,r,a);
- smt2[0][idx]=min(smt2[0][idx<<1],smt2[0][idx<<1|1]);
- smt2[1][idx]=max(smt2[1][idx<<1],smt2[1][idx<<1|1]);
- }
- set<int> s;
- int cnt[200005];
- void cancel(int x){
- cnt[x]--;
- if(cnt[x]==0) s.erase(x);
- }
- void add(int x){
- cnt[x]++;
- s.insert(x);
- }
- int main(){
- while(scanf("%d%d",&n,&q)!=EOF){
- for(int i=1;i<=n;i++){
- if(i&1) step[i]=1;
- else step[i]=2;
- }
- build1(1,1,n);
- build2(1,1,n);
- memset(cnt,0,sizeof(cnt));
- cnt[1]=n;
- s.clear();
- s.insert(1);
- while(q--){
- int x;
- scanf("%d",&x);
- int OL,OR;
- if(step[x]==1) OL=(x==1)? 1:query2(1,1,n,1,x-1).Y+1;
- else OL=(x==1)? 1:query1(1,1,n,1,x-1).Y+1;
- if(step[x]==1) OR=(x==n)? n:query2(1,1,n,x+1,n).X-1;
- else OR=(x==n)? n:query1(1,1,n,x+1,n).X-1;
- step[x]=(step[x]==1)? 2:1;
- change1(1,1,n,x);
- change2(1,1,n,x);
- int NL,NR;
- if(step[x]==1) NL=(x==1)? 1:query2(1,1,n,1,x-1).Y+1;
- else NL=(x==1)? 1:query1(1,1,n,1,x-1).Y+1;
- if(step[x]==1) NR=(x==n)? n:query2(1,1,n,x+1,n).X-1;
- else NR=(x==n)? n:query1(1,1,n,x+1,n).X-1;
- //printf("%d %d %d %d\n",OL,OR,NL,NR);
- if(OL>=NL&&OR<=NR){
- cancel(OL-NL);
- cancel(NR-OR);
- cancel(OR-OL+1);
- add(NR-NL+1);
- }
- else if(OL<=NL&&OR>=NR){
- cancel(OR-OL+1);
- add(NL-OL);
- add(OR-NR);
- add(NR-NL+1);
- }
- else if(OL<=NL&&OR<=NR){
- cancel(OR-OL+1);
- cancel(NR-OR);
- add(NL-OL);
- add(NR-NL+1);
- }
- else if(OL>=NL&&OR>=NR){
- cancel(OL-NL);
- cancel(OR-OL+1);
- add(NR-NL+1);
- add(OR-NR);
- }
- set<int>::iterator iter=s.end();
- iter--;
- printf("%d\n",*iter);
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment