没什么好说的,树套树应该随便搞
我在128MB空间下大胆的写了主席树
当然要把原树和修改树分开来建
没有然后了
1 type node=record 2 l,r,s:longint; 3 end; 4 5 var tree:array[0..10000010] of node; 6 c:array[0..100010] of int64; 7 h,ph,a,b,d1,d2:array[0..100010] of longint; 8 dd1,dd2:array[0..100,0..100] of longint; 9 d,t1,t2,i,x,y,n,m,t:longint; 10 ans:int64; 11 12 function lowbit(x:longint):longint; 13 begin 14 exit(x and (-x)); 15 end; 16 17 procedure add(x:longint); 18 begin 19 while x<=n do 20 begin 21 c[x]:=c[x]+1; 22 x:=x+lowbit(x); 23 end; 24 end; 25 26 function get(x:longint):int64; 27 begin 28 get:=0; 29 while x>0 do 30 begin 31 get:=get+c[x]; 32 x:=x-lowbit(x); 33 end; 34 end; 35 36 procedure update(i:longint); 37 begin 38 tree[i].s:=tree[tree[i].l].s+tree[tree[i].r].s; 39 end; 40 41 function build(l,r:longint):longint; 42 var m,q:longint; 43 begin 44 inc(t); 45 if l=r then 46 exit(t) 47 else begin 48 m:=(l+r) shr 1; 49 q:=t; 50 tree[q].l:=build(l,m); 51 tree[q].r:=build(m+1,r); 52 exit(q); 53 end; 54 end; 55 56 function insert(l,r,last,x,y:longint):longint; 57 var m,q:longint; 58 begin 59 inc(t); 60 if l=r then 61 begin 62 tree[t].s:=tree[last].s+y; 63 exit(t); 64 end 65 else begin 66 m:=(l+r) shr 1; 67 q:=t; 68 if x<=m then 69 begin 70 tree[q].r:=tree[last].r; 71 last:=tree[last].l; 72 tree[q].l:=insert(l,m,last,x,y); 73 end 74 else begin 75 tree[q].l:=tree[last].l; 76 last:=tree[last].r; 77 tree[q].r:=insert(m+1,r,last,x,y); 78 end; 79 update(q); 80 exit(q); 81 end; 82 end; 83 84 function getans(l,r,x,y:longint):longint; 85 var m,s,i:longint; 86 begin 87 if (x<=l) and (y>=r) then 88 begin 89 s:=0; 90 for i:=1 to t2 do 91 s:=s+tree[d2[i]].s; 92 for i:=1 to t1 do 93 s:=s-tree[d1[i]].s; 94 exit(s); 95 end 96 else begin 97 m:=(l+r) shr 1; 98 s:=0; 99 if (x<=m) then 100 begin 101 inc(d); 102 for i:=1 to t1 do 103 begin 104 dd1[d,i]:=d1[i]; 105 d1[i]:=tree[d1[i]].l; 106 end; 107 for i:=1 to t2 do 108 begin 109 dd2[d,i]:=d2[i]; 110 d2[i]:=tree[d2[i]].l; 111 end; 112 s:=s+getans(l,m,x,y); 113 for i:=1 to t1 do 114 d1[i]:=dd1[d,i]; 115 for i:=1 to t2 do 116 d2[i]:=dd2[d,i]; 117 dec(d); 118 end; 119 if y>m then 120 begin 121 for i:=1 to t1 do 122 d1[i]:=tree[d1[i]].r; 123 for i:=1 to t2 do 124 d2[i]:=tree[d2[i]].r; 125 s:=s+getans(m+1,r,x,y); 126 end; 127 exit(s); 128 end; 129 end; 130 131 function ask(l,r,x,y:longint):longint; 132 var i:longint; 133 begin 134 if r=0 then exit(0); 135 if l=n+1 then exit(0); 136 if x=n+1 then exit(0); 137 if y=0 then exit(0); 138 dec(l); 139 t1:=1; 140 t2:=1; 141 d1[1]:=ph[l]; 142 d2[1]:=ph[r]; 143 i:=l; 144 while i>0 do 145 begin 146 if h[i]<>0 then 147 begin 148 inc(t1); 149 d1[t1]:=h[i]; 150 end; 151 i:=i-lowbit(i); 152 end; 153 i:=r; 154 while i>0 do 155 begin 156 if h[i]<>0 then 157 begin 158 inc(t2); 159 d2[t2]:=h[i]; 160 end; 161 i:=i-lowbit(i); 162 end; 163 d:=0; 164 exit(getans(1,n,x,y)); 165 end; 166 167 procedure del(x,y:longint); 168 begin 169 while x<=n do 170 begin 171 h[x]:=insert(1,n,h[x],y,-1); 172 x:=x+lowbit(x); 173 end; 174 end; 175 176 begin 177 readln(n,m); 178 for i:=1 to n do 179 begin 180 readln(a[i]); 181 b[a[i]]:=i; 182 end; 183 for i:=n downto 1 do 184 begin 185 ans:=ans+get(a[i]-1); 186 add(a[i]); 187 end; 188 h[0]:=build(1,n); 189 ph[0]:=h[0]; 190 for i:=1 to n do 191 ph[i]:=insert(1,n,ph[i-1],a[i],1); 192 for i:=1 to m do 193 begin 194 readln(x); 195 y:=b[x]; 196 writeln(ans); 197 ans:=ans-ask(1,y-1,x+1,n)-ask(y+1,n,1,x-1); 198 del(y,x); 199 end; 200 end. 201 202