提交时间:2024-02-27 16:21:36

运行 ID: 26941

#include<bits/stdc++.h> //#define int long long //#define endl "\n" using namespace std; short pre[310][310][310]; short rkd[90300][310],rku[90300][310]; int ALL[100030]; int LEN,len[310]; int a[310][310]; int b[310][310]; int n,m,q,tp; inline int rkdn(int x){ return upper_bound(ALL+1,ALL+LEN+1,x)-ALL-1; } inline int rkup(int x){ return lower_bound(ALL+1,ALL+LEN+1,x)-ALL; } signed main(){ //freopen("bath.in","r",stdin); //freopen("bath.out","w",stdout); ios::sync_with_stdio(0); cin>>n>>m>>q>>tp; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) cin>>a[i][j],b[i][j]=a[i][j],ALL[++LEN]=a[i][j]; sort(b[i]+1,b[i]+m+1); len[i]=unique(b[i]+1,b[i]+m+1)-b[i]-1; for(int j=1;j<=m;j++){ a[i][j]=lower_bound(b[i]+1,b[i]+len[i]+1,a[i][j])-b[i]; memcpy(pre[j][i],pre[j-1][i],sizeof(pre[i][j])); pre[j][i][a[i][j]]++; } for(int j=1;j<=m;j++) for(int k=1;k<=m+1;k++) pre[j][i][k]+=pre[j][i][k-1]; b[i][0]=-1; b[i][len[i]+1]=2e9; } sort(ALL+1,ALL+LEN+1); LEN=unique(ALL+1,ALL+LEN+1)-ALL-1; for(int i=1;i<=n;i++){ int it=0; for(int j=1;j<=LEN;j++){ while(b[i][it]<ALL[j]) it++; rku[j][i]=it; } it=0; for(int j=1;j<=LEN;j++){ while(b[i][it]<=ALL[j]) it++; rkd[j][i]=it-1; } } int lst=0; while(q--){ int xl,xr,yl,yr,wl,wr; cin>>xl>>yl>>xr>>yr>>wl>>wr; if(tp){ xl^=lst; xl=(xl-1+n)%n+1; xr^=lst; xr=(xr-1+n)%n+1; yl^=lst; yl=(yl-1+m)%m+1; yr^=lst; yr=(yr-1+m)%m+1; wl^=lst; wr^=lst; } if(xl>xr)swap(xl,xr); if(yl>yr)swap(yl,yr); if(wl>wr)swap(wl,wr); wl=rkup(wl); wr=rkdn(wr); int res=0; for(int i=xl;i<=xr;i++){ int L=rku[wl][i]; int R=rkd[wr][i]; res+=pre[yr][i][R]-pre[yl-1][i][R]; if(L)res+=pre[yl-1][i][L-1]-pre[yr][i][L-1]; } cout<<(lst=res)<<endl; } cout.flush(); return 0; } /* */