提交时间:2024-02-27 13:46:32
运行 ID: 26923
#include <bits/stdc++.h> using namespace std; #define lson (i<<1) #define rson (i<<1|1) const int maxn=310; int n,m,q,type; int a[maxn][maxn]; struct node { struct ST { int l,r; vector<int> vec; }t[4*maxn]; void build(int i,int l,int r,int x,int y) { t[i].l=l,t[i].r=r; if(l==r) { for(int j=x;j<=y;j++) t[i].vec.push_back(a[j][l]); sort(t[i].vec.begin(),t[i].vec.end()); return ; } int mid=(l+r)>>1; build(lson,l,mid,x,y); build(rson,mid+1,r,x,y); int pos1=0,pos2=0,sz1=t[lson].vec.size(),sz2=t[rson].vec.size(); while(pos1!=sz1&&pos2!=sz2) { if(t[lson].vec[pos1]<t[rson].vec[pos2]) t[i].vec.push_back(t[lson].vec[pos1]),pos1++; else t[i].vec.push_back(t[rson].vec[pos2]),pos2++; } while(pos1!=sz1) t[i].vec.push_back(t[lson].vec[pos1]),pos1++; while(pos2!=sz2) t[i].vec.push_back(t[rson].vec[pos2]),pos2++; } int query(int i,int l,int r,int x,int y) { if(l<=t[i].l&&t[i].r<=r) return upper_bound(t[i].vec.begin(),t[i].vec.end(),y)-lower_bound(t[i].vec.begin(),t[i].vec.end(),x); int e=0; if(l<=t[lson].r) e+=query(lson,l,r,x,y); if(r>=t[rson].l) e+=query(rson,l,r,x,y); return e; } }c[maxn]; int query(int x,int l,int r,int y,int z) { int ans=0; while(x) { ans+=c[x].query(1,l,r,y,z); x-=(x&(-x)); } return ans; } int ask(int x1,int y1,int x2,int y2,int s,int t) { return query(x2,y1,y2,s,t)-query(x1-1,y1,y2,s,t); } int main() { scanf("%d%d%d%d",&n,&m,&q,&type); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); for(int i=1;i<=n;i++) { int lpos=i-(i&(-i))+1; c[i].build(1,1,m,lpos,i); } int x1,y1,x2,y2,s,t,lstans=0; while(q--) { scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&s,&t); if(type==1) x1^=lstans,x2^=lstans,y1^=lstans,y2^=lstans,s^=lstans,t^=lstans; x1=(x1-1+n)%n+1,x2=(x2-1+n)%n+1,y1=(y1-1+m)%m+1,y2=(y2-1+m)%m+1; if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); if(s>t) swap(s,t); printf("%d\n",(lstans=ask(x1,y1,x2,y2,s,t))); } return 0; }