南京网络赛a(离线 树状数组)

Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center value is n * nn∗n. Its spiral decline along the center of the square matrix (the way of spiral decline is shown in the following figure:)

 

 

The grid in the lower left corner is (1,1) and the grid in the upper right corner is (n , n)

Now I can choose mm squares to build palaces, The beauty of each palace is equal to the digital sum of the value of the land which it is located. Such as (the land value is 123213123213,the beautiful values of the palace located on it is 1+2+3+2+1+3=121+2+3+2+1+3=12) (666666 -> 1818) (456456 ->1515)

Next, we ask pp times to the sum of the beautiful values of the palace in the matrix where the lower left grid(x_1,y_1x1​,y1​), the upper right square (x_2,y_2x2​,y2​).

Input

The first line has only one number TT .Representing TT-group of test data (T\le 5)(T≤5)

The next line is three number: n \ m \ pn m p

The mm lines follow, each line contains two integers the square of the palace (x, y )(x,y)

The pp lines follow, each line contains four integers : the lower left grid (x_1,y_1)(x1​,y1​) the upper right square (x_2,y_2)(x2​,y2​)

Output

Next, p_1+p_2...+p_Tp1​+p2​...+pT​ lines: Represent the answer in turn(n \le 10^6)(m , p \le 10^5)(n≤106)(m,p≤105)

样例输入复制

1
3 4 4
1 1
2 2
3 3
2 3
1 1 1 1
2 2 3 3
1 1 3 3
1 2 2 3

样例输出复制

5
18
23
17

给一个图

每次查询一个矩形的内的部分和

对查询和点按x排序后

离线边更新边查找即可

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
P pi[200005];
int n,m,p;
struct node
{
    int x,ly,ry;
    int ans;
    int id;
}qur[200005];
int c[200005];
int cmp1(node p,node q)
{
    return p.x<q.x;
}
int cmp2(node p,node q)
{
    return p.id<q.id;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int v)
{
    while(x<=n)
    {
        c[x]+=v;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int res=0;
    while(x>0)
    {
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
int get_num(P cao)
{
    int x=n+1-cao.second;
    int y=cao.first;
    long long tmp=0;
    if(x<=y&&x<n-y+1)//shang
    {   //cout<<"1"<<"_";
        tmp=(long long)4*x*(n+1)-8LL*x*(x+1)/2;
        tmp-=n-x-y;
    }
    if(x>y&&x<=n-y+1)
    {
        //cout<<"2"<<"_";
        tmp=(long long)4*y*(n+1)-8LL*y*(y+1)/2;
        tmp-=n+1-2*y;
        tmp-=x-y-1;
    }
    if(x>=y&&x>n-y+1)
    {
        //cout<<"3"<<"_";
        tmp=(long long)4*(n+1-x)*(n+1)-8LL*(n+1-x)*((n+1-x)+1)/2;
        tmp-=2*n+2-4*(n+1-x);
        tmp-=x-n+y-2;
    }
    if(x<y&&x>=n-y+1)
    {
        //cout<<"4"<<"_";
        tmp=(long long)4*(n+1-y)*(n+1)-8LL*(n+1-y)*((n+1-y)+1)/2;
        tmp-=3*n+3-6*(n+1-y);
        tmp-=y-x-1;
    }
    if(x==n/2+1&&y==n/2+1)
    {
       tmp=(long long )n*n;
    }
    int ans=0;
    while(tmp!=0)
    {
        ans+=tmp%10;
        tmp/=10;
    }
    return ans;
}
main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&p);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&pi[i].first,&pi[i].second);
            //cout<<get_num(pi[i])<<endl;
        }
        sort(pi+1,pi+1+m);
        for(int i=1;i<=p;i++)
        {
            scanf("%d%d%d%d",&qur[i*2-1].x,&qur[i*2-1].ly,&qur[i*2].x,&qur[i*2].ry);
            qur[i*2-1].x--;
            qur[i*2-1].ly--;
            qur[i*2-1].ry=qur[i*2].ry;
            qur[i*2].ly=qur[i*2-1].ly;
            qur[i*2-1].id=i*2-1;
            qur[i*2].id=i*2;
        }
        //cout<<"#";
        memset(c,0,sizeof(c));
        sort(qur+1,qur+1+p*2,cmp1);
        int pos=1;
        for(int i=1;i<=p*2;i++)
        {
            while(pi[pos].first<=qur[i].x&&pos<=m)
            {
                //cout<<pi[pos].second<<get_num(pi[pos])<<endl;
                update(pi[pos].second,get_num(pi[pos]));
                pos++;
            }
            qur[i].ans=sum(qur[i].ry)-sum(qur[i].ly);
            //cout<<qur[i].id<<" "<<qur[i].ans<<endl;
        }
        sort(qur+1,qur+1+2*p,cmp2);
        for(int i=1;i<=p;i++)
        {
            //printf("%d %d\n",qur[i*2].ans,qur[i*2-1].ans);
            printf("%d\n",qur[i*2].ans-qur[i*2-1].ans);
        }
    }
    return 0;
}

 

上一篇:我今天才知道:学习编程和学习编程语言是两码事


下一篇:CF1A Theatre Square