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

思路:

题目的意思是给你一个n*n的矩阵  但是n很大 我们显然不能用前缀和数组来维护 但是考虑到点只有1e6个 所以我们可以用树状数组来维护y轴 那么这个其实是经典的二维偏序问题 最后我们要解决的其实就是螺旋矩阵的映射问题 对于(i,j)O(1)求出对应的权值:

一种思路是求圈数k 然后k圈的第一个权值:F(k)=F(k-1)+4*(n-1-2*(k-1))

我们可以推出递推式 F(k)=1+4*(k-1)*(n-k+1)

最后下右和左上分别讨论一下即可

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+7;
struct node{
ll x,y,v,id;
friend bool operator < (node a,node b){
if(a.x!=b.x) return a.x<b.x;
if(a.y!=b.y) return a.y<b.y;
return a.id<b.id;
}
}p[N];
ll getv(ll x,ll y,ll n){
ll t=min(x,min(y,min(n-y+1,n-x+1)));
ll res=1+4*(t-1)*(n-t+1);
ll ans;
if(x>=y) ans=res+2*(n-t+1)-x-y;
else ans=res+2*(n-2*(t-1)-1)+x+y-2*t;
ll xx=0;
while(ans){
xx+=(ans%10);
ans/=10;
}
return xx;
}
ll C[N];
ll lowbit(ll x){
return x&(-x);
}
void add(ll x,ll v){
while(x<=N){
C[x]+=v;
x+=lowbit(x);
}
}
ll ask(ll x){
ll ans=0;
while(x){
ans+=C[x];
x-=lowbit(x);
}
return ans;
}
ll ans[N];
int main(){
int t; scanf("%d",&t);
while(t--){
memset(C,0,sizeof(C));
memset(ans,0,sizeof(ans));
ll n,m,pp; scanf("%lld%lld%lld",&n,&m,&pp);
for(int i=0;i<m;i++){
ll x,y; scanf("%lld%lld",&x,&y);
p[i].x=x; p[i].y=y; p[i].v=getv(x,y,n); p[i].id=0;
}
int cnt=m-1;
for(int i=1;i<=pp;i++){
ll x1,y1,x2,y2;
scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
++cnt;
p[cnt].x=x1-1; p[cnt].y=y1-1; p[cnt].v=1; p[cnt].id=i;
++cnt;
p[cnt].x=x2; p[cnt].y=y2; p[cnt].v=1; p[cnt].id=i;
++cnt;
p[cnt].x=x1-1; p[cnt].y=y2; p[cnt].v=0; p[cnt].id=i;
++cnt;
p[cnt].x=x2; p[cnt].y=y1-1; p[cnt].v=0; p[cnt].id=i;
}
sort(p,p+cnt+1);
for(int i=0;i<=cnt;i++){
if(p[i].id){
if(p[i].v==1)
ans[p[i].id]+=ask(p[i].y);
else ans[p[i].id]-=ask(p[i].y);
}else{
add(p[i].y,p[i].v);
}
}
for(int i=1;i<=pp;i++){
printf("%lld\n",ans[i]);
}
}
}

The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)的更多相关文章

  1. The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

    (施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...

  2. [The Preliminary Contest for ICPC Asia Nanjing 2019] A-The beautiful values of the palace(二维偏序+思维)

    >传送门< 前言 这题比赛的时候觉得能做,硬是怼了一个半小时,最后还是放弃了.开始想到用二维前缀和,结果$n\leq 10^{6}$时间和空间上都爆了,没有办法.赛后看题解用树状数组,一看 ...

  3. 计蒜客 The Preliminary Contest for ICPC Asia Nanjing 2019

    F    Greedy Sequence You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each  ...

  4. The Preliminary Contest for ICPC Asia Nanjing 2019

    传送门 A. The beautiful values of the palace 题意: 给出一个\(n*n\)的矩阵,并满足\(n\)为奇数,矩阵中的数从右上角开始往下,类似于蛇形填数那样来填充. ...

  5. The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail

    题目链接:https://nanti.jisuanke.com/t/41305 题目说的很明白...只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了... #include < ...

  6. The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  7. 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019

    题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...

  8. B.super_log(The Preliminary Contest for ICPC Asia Nanjing 2019)

    同:https://www.cnblogs.com/--HPY-7m/p/11444923.html #define IOS ios_base::sync_with_stdio(0); cin.tie ...

  9. H.Holy Grail ( floyd )(The Preliminary Contest for ICPC Asia Nanjing 2019)

    题意: 给出一个有向图,再给出6条原来不存在的路径,让你在这6条路径上添加一个最小的数,使图不存在负环. 思路: 直接6遍 floyd 输出就行了. #include <bits/stdc++. ...

随机推荐

  1. IE浏览器F12无法使用

    原文链接http://zhhll.icu/2020/04/07/windows/IE%E6%B5%8F%E8%A7%88%E5%99%A8F12%E6%97%A0%E6%B3%95%E4%BD%BF% ...

  2. 任意文件下载漏洞的接口URL构造分析与讨论

    文件下载接口的URL构造分析与讨论 某学院的文件下载接口 http://www.****.edu.cn/item/filedown.asp?id=76749&Ext=rar&fname ...

  3. 细说 js 的7种继承方式

    在这之前,先搞清楚下面这个问题: function Father(){} Father.prototype.name = 'father'; Father.prototype.children = [ ...

  4. Nginx Consul nginx-upsync-module

    nginx consul nginx-upsync-module 依赖包: yum -y install libpcre3 libpcre3-dev ruby zlib1g-dev patch 下载n ...

  5. puppetlabs地址

    https://yum.puppetlabs.com/el/6Server/products/i386/ rpm -Uvh  http://yum.puppetlabs.com/el/6Server/ ...

  6. 【ORA】 ORA-01031:权限不足的问题

    今天创建一个用户,赋予dba权限,在plsql中选择sysdba登录,但是报错 ORA-01031 在网上找了好久最后的解决办法是 不仅仅要有dba权限 还要有这个权限: grant all priv ...

  7. 单片机—Arduino UNO-R3—学习笔记002

    led控制 本篇主要介绍Arduino数字引脚及相关函数,通过数字I/O输出控制板载LED灯亮灭状态(数字引脚13). 数字信号是以0.1表示的电平不连续变化的信号,也就是以二进制的形式表示的信号. ...

  8. 强制删除 Terminating 状态的pod

    [root@k8s-master coredns]# kubectl get podNAME                     READY   STATUS        RESTARTS   ...

  9. mysql 1449 : The user specified as a definer ('usertest'@'%') does not exist 解决方法 (grant 授予权限)

    从服务器上迁移数据库到本地localhost 执行  函数  时报错, mysql 1449 : The user specified as a definer ('usertest'@'%') do ...

  10. navicat premium 11.0.17 破解版

    下载地址: 链接:https://pan.baidu.com/s/1zBoKRAaQZb2p2weelJpKMQ       提取码:b8dd 一款功能强大的数据库管理工具Navicat Premiu ...