显然分块,由于颜色也有区间,我们的ans[l,r,k]表示块l和块r颜色1~k的权值和
所以我们块的大小要设为n^(2/3),其它没什么说的,比较水

 var f:array[..,..,..] of int64;
g:array[..,..] of longint;
s:array[..] of longint;
a,be,q:array[..] of longint;
ans,size,t,tot,i,l,r,n,m,te,x,y:longint; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure prework;
var i,j,k:longint;
begin
for i:= to n do
inc(g[be[i],a[i]]);
for i:= to t do
for j:= to m do
g[i,j]:=g[i-,j]+g[i,j];
for i:= to t do
begin
for j:=(i-)*size+ to n do
begin
if j mod size= then
begin
for k:= to m do
f[i,be[j],k]:=f[i,be[j]-,k];
end;
inc(f[i,be[j],a[j]],s[a[j]]*+);
inc(s[a[j]]);
end;
for j:=i to t do
for k:= to m do
inc(f[i,j,k],f[i,j,k-]);
fillchar(s,sizeof(s),);
end;
end; procedure clear;
var i:longint;
begin
for i:= to tot do
s[q[i]]:=;
end; function ask(l,r,x,y:longint):int64;
var i:longint;
begin
tot:=;
ask:=;
if be[l]=be[r] then
begin
for i:=l to r do
if (a[i]>=x) and (a[i]<=y) then
begin
if s[a[i]]= then
begin
inc(tot);
q[tot]:=a[i];
end;
ask:=ask+s[a[i]]*+;
inc(s[a[i]]);
end;
clear;
end
else begin
ask:=f[be[l]+,be[r]-,y]-f[be[l]+,be[r]-,x-];
for i:=l to be[l]*size do
if (a[i]>=x) and (a[i]<=y) then
begin
if s[a[i]]= then
begin
inc(tot);
q[tot]:=a[i];
s[a[i]]:=g[be[r]-,a[i]]-g[be[l],a[i]];
end;
ask:=ask+s[a[i]]*+;
inc(s[a[i]]);
end;
for i:=(be[r]-)*size+ to r do
if (a[i]>=x) and (a[i]<=y) then
begin
if s[a[i]]= then
begin
inc(tot);
q[tot]:=a[i];
s[a[i]]:=g[be[r]-,a[i]]-g[be[l],a[i]];
end;
ask:=ask+s[a[i]]*+;
inc(s[a[i]]);
end;
clear;
end;
end; begin
readln(n,m,te);
size:=;
while size*size/n*size<n do inc(size);
dec(size);
for i:= to n do
begin
read(a[i]);
be[i]:=(i-) div size+;
end;
t:=n div size;
if n mod size<> then inc(t);
prework;
for i:= to te do
begin
readln(l,r,x,y);
l:=l xor ans;
r:=r xor ans;
x:=x xor ans;
y:=y xor ans;
if l>r then swap(l,r);
if x>y then swap(x,y);
ans:=ask(l,r,x,y);
writeln(ans);
end;
end.
04-26 18:32