【BZOJ2874】训练士兵(主席树)

时间:2021-09-25 22:13:10

题意:有一个N*M的矩阵,给出一些形如(x1,y1,x2,y2,s)的操作,代表(x1,y1)到(x2,y2)都被加上了s这个数

现在有一些强制在线的询问,询问(x1,y1)到(x2,y2)的和

对于100%的数据 n,m<=10^8,k<=40000,q<=100000;

思路:将操作(x1,y1,x2,y2,s)差分成

(x1,y1)+s

(x1,y2+1)-s

(x2+1,y1)-s

(x2+1,y2+1)+s

四个点

询问(x,y)等价于

\[\sum_{i=1}^x  \sum_{j=1}^y  \ a[i,j]*(x+1)*(y+1)-a[i,j]*i*(y+1)-a[i,j]*j*(x+1)+a[i,j]*i*j \]

证明如下:

【BZOJ2874】训练士兵(主席树)

可以看出只需维护a[i,j],a[i,j]*i,a[i,j]*j,a[i,j]*i*j四个值的二维前缀和

从左到右,从上到下排序并离散化,对每一行建立一棵主席树表示左上角的前缀和,

询问时在主席树中查找一段从头开始的前缀和即可

 var t:array[..,..]of int64;
root:array[..]of int64;
c:array[..,..]of int64;
cx,cy:array[..]of int64;
n,m,i,j,u,k,cnt,u1,u2,k1,q1,tmp,x,y:longint;
ans,x1,y1,x2,y2:int64; procedure swap(var x,y:int64);
var t:int64;
begin
t:=x; x:=y; y:=t;
end; procedure qsortx(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=cx[(l+r)>>];
repeat
while mid>cx[i] do inc(i);
while mid<cx[j] do dec(j);
if i<=j then
begin
swap(cx[i],cx[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsortx(l,j);
if i<r then qsortx(i,r);
end; procedure qsorty(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=cy[(l+r)>>];
repeat
while mid>cy[i] do inc(i);
while mid<cy[j] do dec(j);
if i<=j then
begin
swap(cy[i],cy[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsorty(l,j);
if i<r then qsorty(i,r);
end; function hashx(x:longint):longint;
var l,r,mid,last:longint;
begin
l:=; r:=u1; last:=;
while l<=r do
begin
mid:=(l+r)>>;
if x>=cx[mid] then begin last:=mid; l:=mid+; end
else r:=mid-;
end;
exit(last);
end; function hashy(x:longint):longint;
var l,r,mid,last:longint;
begin
l:=; r:=u2; last:=;
while l<=r do
begin
mid:=(l+r)>>;
if x>=cy[mid] then begin last:=mid; l:=mid+; end
else r:=mid-;
end;
exit(last);
end; procedure qsort(l,r:longint);
var i,j,mid1,mid2:longint;
begin
i:=l; j:=r; mid1:=c[(l+r)>>,]; mid2:=c[(l+r)>>,];
repeat
while (mid1>c[i,])or((mid1=c[i,])and(mid2>c[i,])) do inc(i);
while (mid1<c[j,])or((mid1=c[j,])and(mid2<c[j,])) do dec(j);
if i<=j then
begin
swap(c[i,],c[j,]);
swap(c[i,],c[j,]);
swap(c[i,],c[j,]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; function query(p,l,r,x,y:longint):int64;
var mid:longint;
begin
if x= then exit();
if p= then exit();
if x>=r then exit(t[p,y]);
mid:=(l+r)>>;
if x<=mid then exit(query(t[p,],l,mid,x,y))
else exit(t[t[p,],y]+query(t[p,],mid+,r,x,y));
end; function ask(x,y:longint):int64;
var x1,y1:longint;
begin
x1:=hashx(x); y1:=hashy(y);
ask:=;
ask:=ask+query(root[x1],,u2,y1,)*(x+)*(y+);
ask:=ask-query(root[x1],,u2,y1,)*(y+);
ask:=ask-query(root[x1],,u2,y1,)*(x+);
ask:=ask+query(root[x1],,u2,y1,);
end; procedure update(var p:int64;l,r,x:longint;v:int64;x1,y1:longint);
var mid:longint;
begin
inc(cnt); t[cnt]:=t[p];
p:=cnt;
t[cnt,]:=t[cnt,]+v;
t[cnt,]:=t[cnt,]+v*x1;
t[cnt,]:=t[cnt,]+v*y1;
t[cnt,]:=t[cnt,]+v*x1*y1;
if l=r then exit;
mid:=(l+r)>>;
if x<=mid then update(t[p,],l,mid,x,v,x1,y1)
else update(t[p,],mid+,r,x,v,x1,y1);
end; begin
assign(input,'bzoj2874.in'); reset(input);
assign(output,'bzoj2874.out'); rewrite(output);
readln(n,m,k1,q1);
k:=;
for i:= to k1 do
begin
read(x1); read(x2); read(y1); read(y2); read(tmp);
inc(k); c[k,]:=x1; c[k,]:=y1; c[k,]:=tmp;
inc(k); c[k,]:=x1; c[k,]:=y2+; c[k,]:=-tmp;
inc(k); c[k,]:=x2+; c[k,]:=y1; c[k,]:=-tmp;
inc(k); c[k,]:=x2+; c[k,]:=y2+; c[k,]:=tmp;
inc(u); cx[u]:=x1; cy[u]:=y1;
inc(u); cx[u]:=x2+; cy[u]:=y2+;
end;
qsortx(,u); qsorty(,u);
u1:=;
for i:= to u do
if cx[i]<>cx[u1] then begin inc(u1); cx[u1]:=cx[i]; end;
u2:=;
for i:= to u do
if cy[i]<>cy[u2] then begin inc(u2); cy[u2]:=cy[i]; end; qsort(,k);
j:=;
for i:= to u1 do
begin
root[i]:=root[i-];
while (j<=k)and(hashx(c[j,])=i) do
begin
update(root[i],,u2,hashy(c[j,]),c[j,],c[j,],c[j,]);
inc(j);
end;
end;
for i:= to q1 do
begin
readln(x,y);
x1:=ans mod n+; x2:=(ans+x) mod n+; if x1>x2 then swap(x1,x2);
y1:=ans mod m+; y2:=(ans+y) mod m+; if y1>y2 then swap(y1,y2);
ans:=ask(x2,y2)-ask(x1-,y2)-ask(x2,y1-)+ask(x1-,y1-);
writeln(ans);
end;
close(input);
close(output);
end.