# bzoj3744

  1 const maxn=50000;
2
3 var s:array[0..300,0..300] of longint;
4     f,g:array[0..300,0..maxn] of longint;
5     co,v,a,be,b,c,rank,h:array[0..maxn] of longint;
6     time,i,size,n,m,j,k,ans,x,y,t,tot,p:longint;
7
8 function lowbit(x:longint):longint;
9   begin
10     exit(x and (-x));
11   end;
12
13 function min(a,b:longint):longint;
14   begin
15     if a>b then exit(b) else exit(a);
16   end;
17
18 procedure swap(var a,b:longint);
19   var c:longint;
20   begin
21     c:=a;
22     a:=b;
23     b:=c;
24   end;
25
27   begin
28     while x<=p do
29     begin
30       if v[x]<>time then
31       begin
32         c[x]:=0;
33         v[x]:=time;
34       end;
35       inc(c[x]);
36       x:=x+lowbit(x);
37     end;
38   end;
39
41   begin
43     while x>0 do
44     begin
45       if v[x]<>time then  //为了不用fillchar加了一个时间戳
46       begin
47         c[x]:=0;
48         v[x]:=time;
49       end;
51       x:=x-lowbit(x);
52     end;
53   end;
54
55 procedure sort(l,r: longint);
56   var i,j,x,y: longint;
57   begin
58     i:=l;
59     j:=r;
60     x:=b[(l+r) shr 1];
61     repeat
62       while b[i]<x do inc(i);
63       while x<b[j] do dec(j);
64       if not(i>j) then
65       begin
66         swap(b[i],b[j]);
67         swap(h[i],h[j]);
68         inc(i);
69         j:=j-1;
70       end;
71     until i>j;
72     if l<j then sort(l,j);
73     if i<r then sort(i,r);
74   end;
75
76 function getans(l,r:longint):longint;
77   var i,j,x:longint;
78   begin
79     getans:=0;
80     t:=0;
81     if be[r]=be[l] then
82     begin
83       for i:=l to r do
84       begin
87         inc(t);
88       end;
89     end
90     else begin
91       x:=min(be[r]*size,n);
92       if ((be[l]-1)*size+1=l) and (x=r) then exit(s[be[l],be[r]])  //正好是端点可以直接计算
93       else if ((be[l]-1)*size+1=l) then
94       begin
95         getans:=getans+s[be[l],be[r]-1];
96         for i:=(be[r]-1)*size+1 to r do
97         begin
98           x:=rank[i];
99           getans:=getans+f[be[r]-1,x]-f[be[l]-1,x];
102           inc(t);
103         end;
104         exit;
105       end
106       else if (x=r) then
107       begin
108         getans:=getans+s[be[l]+1,be[r]];
109         for i:=l to be[l]*size do
110         begin
111           x:=rank[i];
112           getans:=getans+g[be[r],x]-g[be[l],x];
115           inc(t);
116         end;
117         exit;
118       end;
119       getans:=getans+s[be[l]+1,be[r]-1];
120       for i:=l to be[l]*size do
121       begin
122         x:=rank[i];
123         getans:=getans+g[be[r]-1,x]-g[be[l],x];
126         inc(t);
127       end;
128       for i:=(be[r]-1)*size+1 to r do
129       begin
130         x:=rank[i];
131         getans:=getans+f[be[r]-1,x]-f[be[l],x];
134         inc(t);
135       end;
136     end;
137   end;
138
139 begin
141   size:=trunc(sqrt(n)/1.1);
142   for i:=1 to n do
143   begin
145     b[i]:=a[i];
146     h[i]:=i;
147     be[i]:=(i-1) div size+1;
148   end;
149   tot:=i div size;
150   if i mod size<>0 then inc(tot);
151   sort(1,n);
152   p:=1;
153   rank[h[1]]:=1;
154   for i:=2 to n do
155   begin
156     if b[i]<>b[i-1] then inc(p);
157     rank[h[i]]:=p;
158   end;
159   for i:=1 to tot do //预处理f[i,j],g[i,j]
160   begin
161     x:=min(i*size,n);
162     for j:=(i-1)*size+1 to x do
163       inc(co[rank[j]]);
164     f[i,p]:=0;
165     for j:=p-1 downto 1 do
166       f[i,j]:=f[i,j+1]+co[j+1];
167     g[i,1]:=0;
168     for j:=2 to p do
169       g[i,j]:=g[i,j-1]+co[j-1];
170   end;
171   for i:=1 to tot do  //预处理i~j块内的逆序对数
172   begin
173     t:=0;
174     inc(time);
175     for j:=(i-1)*size+1 to n do
176     begin
178       inc(s[i,be[j]],x);
180       inc(t);
181     end;
182     for j:=i+1 to tot do
183       s[i,j]:=s[i,j]+s[i,j-1];
184   end;
185   ans:=0;
187   for i:=1 to m do
188   begin
189     inc(time);
197 end.