The beautiful values of the palace

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_39132605/article/details/100515255

https://nanti.jisuanke.com/t/41298

Spiral matrix calculation: the outermost layer can be determined to find the law; and which is narrow at the coordinates, n is about the same reduction is calculated; n-side is to longlongs;

Discrete two-dimensional difference: discrete two-dimensional coordinates calculated as the sum of the original prefix and; 

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <stack>
//#include <tr1/unordered_map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define prl(i) printf("%I64d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define inf 1e18
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

template<typename T>inline void read(T &x)
{
    x=0;
    static int p;p=1;
    static char c;c=getchar();
    while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<1)+(x<<3)+(c-48);c=getchar();}
    x*=p;
}

//-----------------------------------------------

const int maxn=1e6+9;
const int mod=1e9+7;

int n,m,q;
ll get_val(ll x)
{
    ll ans=0;
    while(x)
    {
        ans+=x%10;
        x/=10;
    }
    return ans;
}

ll get_id(int x,int y)
{
    ll ans=0;

    int ceng=min(min(x,y),min(n-x+1,n-y+1));

    if(ceng==1)
    {
        if(x==1) ans=2*(n-1)+y;
        else if(x==n) ans=n-y+1;
        else if(y==1) ans=2*n-x;
        else if(y==n) ans=3*(n-1)+x;
        return ans;
    }

//    int cha=n;
//    for(int i=0;i<ceng-1;i++)
//    {
//        ans+=(cha-1)*4;
//        cha-=2;
//    }

    ans=4*(ll)n*((ll)ceng-1)-4*(((ll)ceng-1)+((ll)ceng-1)*((ll)ceng-2));

    x-=ceng;
    x++;
    y-=ceng;
    y++;

    ll nn=(ll)n-((ll)ceng-1)*2;

    if(x==1) ans+=2*(nn-1)+y;
    else if(x==nn) ans+=nn-y+1;
    else if(y==1) ans+=2*nn-x;
    else if(y==nn) ans+=3*(nn-1)+x;

    return ans;

}

struct point
{
    int x,y;
    bool f;
}p[maxn];

int cnt;

struct Q
{
    int x1,y1,x2,y2;
}que[maxn*4];

int cnty;
int yy[maxn];

bool cmp(point a,point b)
{
    if(a.x==b.x)
    {
        if(a.y==b.y)
            return a.f<b.f;
        else return a.y<b.y;
    }
    return a.x<b.x;
}

int c[maxn];

void add(int x,int val)
{
    while(x<maxn)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}

int query(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
map<pair<int,int>,int >mp;
int main()
{
    FAST_IO;
    //freopen("input.txt","r",stdin);

//    n=7;
//    int x,y;
//
//    for(int i=n;i>=1;i--)
//    {
//        for(int j=1;j<=n;j++) cout<<get_id(j,i)<<" ";
//        cout<<endl;
//    }



    int T;
    cin>>T;
    while(T--)
    {
        mp.clear();
        cnty=0;
        cnt=0;
        mem(c,0);

        cin>>n>>m>>q;

        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            p[++cnt]={x,y,0};
        }

        for(int i=0;i<q;i++)
        {
            int x1,y1,x2,y2;
            cin>>x1>>y1>>x2>>y2;
            que[i]={x1,y1,x2,y2};
            p[++cnt]={x1-1,y1-1,1};
            p[++cnt]={x1-1,y2,1};
            p[++cnt]={x2,y1-1,1};
            p[++cnt]={x2,y2,1};
        }

        for(int i=1;i<=cnt;i++) yy[cnty++]=p[i].y;
        sort(yy,yy+cnty);
        int len=unique(yy,yy+cnty)-yy;

        sort(p+1,p+1+cnt,cmp);

        for(int i=1;i<=cnt;i++)
        {
            int pos=lower_bound(yy,yy+len,p[i].y)-yy+1;
            //cout<<pos<<" ";
            if(p[i].f==0) add(pos,get_val(get_id(p[i].x,p[i].y)));
            else if(p[i].f==1) mp[{p[i].x,p[i].y}]=query(pos);
        }

        for(int i=0;i<q;i++)
        {
            int x1=que[i].x1;
            int y1=que[i].y1;
            int x2=que[i].x2;
            int y2=que[i].y2;

            ll ans=mp[{x2,y2}]+mp[{x1-1,y1-1}]-mp[{x1-1,y2}]-mp[{x2,y1-1}];
            cout<<ans<<endl;
        }

    }

    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_39132605/article/details/100515255