The Preliminary Contest for ICPC Asia Nanjing 2019 A. The beautiful values of the palace (扫描线)

2 篇文章 0 订阅
1 篇文章 0 订阅

网络赛真是能把我这种蒟蒻做到自闭。。写了几道属于我这种水平的难题
均转载自一起打比赛的聚聚同学:SDUWH_2U
传送门:https://nanti.jisuanke.com/t/41298

Description

Here is a square matrix of n ∗ n n * n nn, each lattice has its value (nn must be odd), and the center value is n ∗ n n * n nn. 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 123213 123213 123213, the beautiful values of the palace located on it is 1 + 2 + 3 + 2 + 1 + 3 = 121 + 2 + 3 + 2 + 1 + 3 = 12 ) ( 666 − > 18 ) ( 456 − > 15 ) 1+2+3+2+1+3=121+2+3+2+1+3=12) (666 -> 18) (456 ->15) 1+2+3+2+1+3=121+2+3+2+1+3=12)(666>18)(456>15)

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 1 ) (x_1, y_1) (x1,y1), the upper right square ( x 2 , y 2 ) (x_2,y_2) (x2,y2)

Input

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

The next line is three number: n   m   p n~m ~p n m p

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

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

Output

Next, p 1 + p 2 . . . + p T p_1+p_2...+p_T p1+p2...+pT lines: Represent the answer in turn ( n ≤ 1 0 6 ) ( m , p ≤ 1 0 5 ) (n \le 10^6)(m , p \le 10^5) (n106)(m,p105)

Sample Input

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

Sample Output

5
18
23
17

代码:

#include <bits/stdc++.h>
using namespace std;
int m, n, p, tg;

struct point{
    int x,x2,y,y2;
    int id;
    int type; //0代表询问区间左下角点,1代表更新点,2代表询问区间右上角点
}pp[300005];
int quer[100005];

bool cmp1(point &a, point &b)
{
    if(a.y == b.y) {
        if(a.x==b.x) return a.type < b.type;
        return a.x < b.x;
    }
    return a.y < b.y;
}

struct node{
    int l,r,sum;
}tree[4*1000005];

void build(int i,int l,int r){
    tree[i].l=l;
    tree[i].r=r;
    if(l==r){
        tree[i].sum = 0;
        return;
    }
    int mid=(l+r)>>1;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    tree[i].sum = 0;
    return;
}

int query(int id, int l, int r)
{
    if (tree[id].l == l && tree[id].r == r) return tree[id].sum;
    int mid = (tree[id].l + tree[id].r) >> 1;
    if (r <= mid) return query(id*2, l, r);
    if (l > mid) return query(id*2+1, l, r);
    return query(id*2, l, mid) + query(id*2+1, mid + 1, r);
}



int getVal(long long x, long long y)
{
    x = x - n / 2 - 1;
    y = y - n / 2 - 1;
    long long tmp = max(abs(x), abs(y));
    long long num = x >= y ? (long long)n * n - tmp * tmp * 4 - tmp * 2 - x - y : (long long)n * n - tmp * tmp * 4 + tmp * 2 + x + y;
    int res = 0;
    do { res += num % 10; num /= 10; } while (num);
    return res;
}



void update(int id, int l, int r, int v)
{
    if (tree[id].l == l && tree[id].r == r)
    {
        tree[id].sum += (r - l + 1) * v;
        return;
    }
    int mid = (tree[id].l + tree[id].r) >> 1;
    if (r <= mid) update(id*2, l, r, v);
    else if (l > mid) update(id*2+1, l, r, v);
    else
    {
        update(id*2, l, mid, v);
        update(id*2+1, mid + 1, r, v);
    }
    tree[id].sum = tree[id*2].sum + tree[id*2+1].sum;
}


int main()
{
    int T;
    cin >> T;
    for(int g=1;g<=T;g++)
    {
        scanf("%d %d %d",&n,&m,&p);
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&pp[i].x,&pp[i].y);
            pp[i].type = 1;
        }
        int x1,y1,x2,y2;
        for(int i=0;i<p;i++)
        {
            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
            pp[m+i*2].type = 0;
            pp[m+i*2+1].type = 2;
            pp[m+i*2].x = pp[m+i*2+1].x2 = x1;
            pp[m+i*2].y = pp[m+i*2+1].y2 = y1;
            pp[m+i*2+1].x = pp[m+i*2].x2 = x2;
            pp[m+i*2+1].y = pp[m+i*2].y2 = y2;
            pp[m+i*2].id = pp[m+i*2+1].id = i;
        }
        sort(pp,pp+p*2+m,cmp1);
        build(1,1,n);
        for(int i=0;i<2*p+m;i++)
        {
            if(pp[i].type==1){
                update(1, pp[i].x, pp[i].x,getVal(pp[i].x, pp[i].y));
            }
            else if(pp[i].type==0){
                quer[pp[i].id] = query(1, pp[i].x, pp[i].x2);
            }
            else if(pp[i].type==2){
                quer[pp[i].id] = query(1, pp[i].x2, pp[i].x) - quer[pp[i].id];
            }
        }
        for(int i=0;i<p;i++)
            printf("%d\n",quer[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值