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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=5e5+10,MAXM=5e4+10; int N,Q; std::mt19937 rnd(19260817); namespace Pst { #define ls(p) Tr[p].lc #define rs(p) Tr[p].rc struct PreSegment { int lc,rc,val; }Tr[MAXN<<5]; int Rt[MAXN],Idx=0; inline void pushUp(int p) { Tr[p].val=Tr[ls(p)].val+Tr[rs(p)].val; } int build(int l,int r) { int x=++Idx; if(l==r) return x; int mid=(l+r)>>1; ls(x)=build(l,mid),rs(x)=build(mid+1,r); return x; } int querySum(int p1,int p2,int l,int r,int ql,int qr) { if(ql>qr||Tr[p1].val==Tr[p2].val) return 0; if(ql<=l&&r<=qr) return Tr[p2].val-Tr[p1].val; int mid=(l+r)>>1,res=0; if(ql<=mid) res+=querySum(ls(p1),ls(p2),l,mid,ql,qr); if(mid<qr) res+=querySum(rs(p1),rs(p2),mid+1,r,ql,qr); return res; } int modifyAdd(int p,int l,int r,int x) { int nd=++Idx; Tr[nd]=Tr[p]; if(l==r) return ++Tr[nd].val,nd; int mid=(l+r)>>1; if(x<=mid) ls(nd)=modifyAdd(ls(p),l,mid,x); else rs(nd)=modifyAdd(rs(p),mid+1,r,x); return pushUp(nd),nd; } #undef ls #undef rs }; namespace Tre { struct Treap { int chi[2],siz,val,rd,va1,sum; }Tr[MAXN<<4]; int Idx; struct Fhq_Treap { int Rt,x,y,z; #define ls(p) Tr[p].chi[0] #define rs(p) Tr[p].chi[1] inline int newNode(int x,int y) { int id=++Idx; Tr[id].siz=1,Tr[id].val=x,Tr[id].rd=rnd(); Tr[id].va1=Tr[id].sum=y; return id; } inline void upDate(int p) { Tr[p].siz=Tr[ls(p)].siz+Tr[rs(p)].siz+1; Tr[p].sum=Tr[ls(p)].sum+Tr[rs(p)].sum+Tr[p].va1; } int merge(int u,int v) { if(!u||!v) return u^v; if(Tr[u].rd<Tr[v].rd) { rs(u)=merge(rs(u),v); return upDate(u),u; } else { ls(v)=merge(u,ls(v)); return upDate(v),v; } } void split(int p,int k,int &u,int &v) { if(!p) u=v=0; else { if(Tr[p].val<=k) u=p,split(rs(p),k,rs(p),v); else v=p,split(ls(p),k,u,ls(p)); upDate(p); } return ; } int getSum(int v) { x=y=0; split(Rt,v,x,y); int res=Tr[x].sum; Rt=merge(x,y); return res; } inline void insert(int v,int k) { x=y=z=0; split(Rt,v-1,x,y),split(y,v,y,z); if(!y) y=newNode(v,0); Tr[y].va1+=k,Tr[y].sum+=k; Rt=merge(merge(x,y),z); } }Bst[MAXN]; inline int lowbit(int x){ return x&(-x); } inline int getLeq(int x,int v) { int res=0; for(;x;x-=lowbit(x)) res+=Bst[x].getSum(v); return res; } inline int getLeq(int n,int l,int r,int k) { return Pst::querySum(Pst::Rt[l-1],Pst::Rt[r],1,n,1,k)+(getLeq(r,k)-getLeq(l-1,k)); } inline void erase(int n,int x,int k) { for(;x<=n;x+=lowbit(x)) Bst[x].insert(k,-1); } inline void insert(int n,int x,int k) { for(;x<=n;x+=lowbit(x)) Bst[x].insert(k,1); } }; int p[MAXN]; ll ans; int main() { read(N,Q); Pst::Rt[0]=Pst::build(1,N); for(int i=1;i<=N;++i) { read(p[i]); ans+=std::max(p[i]-i,0)*2-Pst::querySum(Pst::Rt[0],Pst::Rt[i-1],1,N,p[i]+1,N); Pst::Rt[i]=Pst::modifyAdd(Pst::Rt[i-1],1,N,p[i]); } for(int x,y;Q--;) { read(x,y); int a=Tre::getLeq(N,x+1,y-1,p[x]); int b=Tre::getLeq(N,x+1,y-1,p[y]); ans-=(std::max(p[x]-x,0)+std::max(p[y]-y,0))*2-((a+(p[x]>p[y]?1:0))+(y-x-b-1)); Tre::erase(N,x,p[x]),Tre::erase(N,y,p[y]); std::swap(p[x],p[y]); Tre::insert(N,x,p[x]),Tre::insert(N,y,p[y]); ans+=(std::max(p[x]-x,0)+std::max(p[y]-y,0))*2-((b+(p[x]>p[y]?1:0))+(y-x-a-1)); write(ans,'\n'); } return 0; }
|