The meaning of problems
To a \ (n \ times n \) spiral matrix, which gives the \ (m \) values are points on each point of the digital and digital to give \ (Q \) a query, each Inquiry from \ ((x1, y1) \ ) to \ ((x2, y2) \ ) sub-matrix and.
analysis
The method for official solution to a problem of \ (O (1) \) Release point \ ((x, y) \ ) values of these \ (m \) th tap \ (X \) sorted sequentially by \ (Y \) Chairman build tree, find the query corresponding \ (x1 \) and (x2 \) \ version of history, the query \ (y1 \) to \ (y2 \) weights and on the line, \ ((query ( Y1, y2,1, n-, RT [L], RT [R & lt])) \) ;
Code
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e6+10;
int T,n,m,q;
ll tr[maxn*30];
int ls[maxn*30],rs[maxn*30],rt[maxn],tot;
struct ppo{
int x,y;
ll c;
bool operator<(const ppo &r)const{
return x<r.x;
}
}a[maxn];
ll fun(ll i,ll j,ll n){
ll ans;
j=n-j+1;i=n-i+1;
ll minn=min(i,min(j,min(n-i+1,n-j+1)));
if(i<=j) ans=minn*(4*(n-1)-4*minn)+10*minn-4*n-3+i+j;
else ans=minn*(4*n-4*minn)+2*minn+1-i-j;
ll sum = 0;
while(ans){
sum+=ans%10;
ans/=10;
}
return sum;
}
void bd(int l,int r,int &p){
tr[++tot]=tr[p],ls[tot]=ls[p],rs[tot]=rs[p],p=tot;
if(l==r) return;
int mid=l+r>>1;
bd(l,mid,ls[p]);bd(mid+1,r,rs[p]);
}
void up(int k,int l,int r,int x,int &p){
tr[++tot]=tr[p]+x,ls[tot]=ls[p],rs[tot]=rs[p],p=tot;
if(l==r) return;
int mid=l+r>>1;
if(k<=mid) up(k,l,mid,x,ls[p]);
else up(k,mid+1,r,x,rs[p]);
}
ll qy(int dl,int dr,int l,int r,int a,int b){
if(l>=dl&&r<=dr){
return tr[b]-tr[a];
}int mid=l+r>>1;ll ret=0;
if(dl<=mid) ret+=qy(dl,dr,l,mid,ls[a],ls[b]);
if(dr>mid) ret+=qy(dl,dr,mid+1,r,rs[a],rs[b]);
return ret;
}
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
scanf("%d",&T);
while(T--){
tot=0;
scanf("%d%d%d",&n,&m,&q);
for(int i=1,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
a[i]=ppo{x,y,fun(x,y,n)};
}
sort(a+1,a+m+1);
bd(1,n,rt[0]);
for(int i=1;i<=m;i++){
rt[i]=rt[i-1];
up(a[i].y,1,n,a[i].c,rt[i]);
}
while(q--){
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int l=lower_bound(a+1,a+m+1,ppo{x1,0,0})-a-1;
int r=upper_bound(a+1,a+m+1,ppo{x2,0,0})-a-1;
printf("%lld\n",qy(y1,y2,1,n,rt[l],rt[r]));
}
}
return 0;
}