1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include<iostream> #include<cstdio> #include<cstdlib> #define ll long long using namespace std; namespace Ehnaev{ inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();} while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();} return ret*f; } inline void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);} else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);} while(len>=0) putchar(buf[len--]); } }using Ehnaev::read;using Ehnaev::write; inline void writes(ll x) {write(x);putchar(32);}
const ll N=1e5;
ll bufch[2][N+5],bufval[N+5],bufsiz[N+5],bufrnd[N+5],bufrev[N+5]; ll *nowch[2]={bufch[0],bufch[1]},*nowval=bufval,*nowsiz=bufsiz ,*nowrnd=bufrnd,*nowrev=bufrev;
struct Fhq_Treap{ ll *ch[2],*val,*siz,*rnd,*rev;ll rt,sz; inline void Init(ll x) { x+=3;ch[0]=nowch[0];ch[1]=nowch[1];val=nowval;siz=nowsiz; rnd=nowrnd;rev=nowrev; nowch[0]+=x;nowch[1]+=x;nowval+=x;nowsiz+=x;nowrnd+=x;nowrev+=x; } inline void Pushup(ll x) {siz[x]=siz[ch[0][x]]+siz[ch[1][x]]+1;} inline void Pushdown(ll x) { if(rev[x]) { swap(ch[0][x],ch[1][x]);rev[x]=0; if(ch[0][x]) rev[ch[0][x]]^=1;if(ch[1][x]) rev[ch[1][x]]^=1; } } inline void Split_(ll p,ll k,ll &x,ll &y) { if(!p) {x=y=0;return;}Pushdown(p); if(siz[ch[0][p]]<k) {x=p;Split_(ch[1][x],k-siz[ch[0][p]]-1,ch[1][x],y);} else {y=p;Split_(ch[0][y],k,x,ch[0][y]);}Pushup(p); } inline ll Merge(ll x,ll y) { if(!x||!y) return x|y; if(rnd[x]<rnd[y]) { Pushdown(y);ch[0][y]=Merge(x,ch[0][y]);Pushup(y);return y; } else { Pushdown(x);ch[1][x]=Merge(ch[1][x],y);Pushup(x);return x; } } inline void Ins(ll k) { val[++sz]=k;siz[sz]++;rnd[sz]=rand(); if(!rt) {rt=sz;return;}rt=Merge(rt,sz); } inline void Rev(ll l,ll r) { ll x,y,z;Split_(rt,l-1,x,y);Split_(y,r-l+1,y,z); rev[y]^=1;rt=Merge(x,Merge(y,z)); } inline void Print(ll p) { if(!p) return; Pushdown(p);Print(ch[0][p]);writes(val[p]);Print(ch[1][p]); } }t;
int main() { srand(73939133);ll n,m;n=read();m=read();t.Init(n); for(ll i=1;i<=n;i++) {t.Ins(i);}
while(m--) {ll l,r;l=read();r=read();t.Rev(l,r);}
t.Print(t.rt);
return 0; }
|