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. Mac上“您没有权限来打开应用程序”(Big Sur)

    最近电脑更新了Macos的最新11版大苏尔 Big Sur.很快问题就出现了:安装某个软件的时候Key Gen打不开,提示您没有权限来打开应用程序,类似这样:https://zhuanlan.zhih ...

  2. linux系统修改Swap分区【转】

    在装完Linux系统之后自己去修改Swap分区的大小(两种方法) 在安装完Linux系统后,swap分区太小怎么办,怎么可以扩大Swap分区呢?有两个办法,一个是从新建立swap分区,一个是增加swa ...

  3. 剑指offer 面试题10.1:青蛙跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 编程思想 对于本题,前提只有 一次 1阶或者2阶的跳法.a.如果两种跳 ...

  4. Java向指定Excel写入读取数据

    今天在开发中遇到用户列表导入导出的功能实现,这里了解到使用POI函数库可以完成此任务!特此记录一下 POI Apache POI是Apache软件基金会开放的源码函数库,POI提供API给Java程序 ...

  5. springAOP的概述及使用

    Spring AOP SpringAOP是Spring中非常重要的功能模块之一,该模块提供了面向切面编程,在事务处理,日志记录,安全控制等操作中广泛使用. SpringAOP的基本概念 AOP的概念 ...

  6. pandas高级操作

    pandas高级操作 import numpy as np import pandas as pd from pandas import DataFrame,Series 替换操作 替换操作可以同步作 ...

  7. NFS存储迁移至GlusterFS

    NFS存储迁移至GlusterFS 前提条件 为防止脑裂,建议使用最低3台节点制作3复制集的存储卷: 在进行存储迁移前,GluseterFS存储节点需先成为k8s集群中的node节点: 存储切换时请勿 ...

  8. CPU飙高,系统性能问题如何排查?

    CPU飙高,系统性能问题如何排查? 原创 雍雍 阿里技术 2020-09-29 https://mp.weixin.qq.com/s/fzLcAkYwKhj-9hgoVkTzaw  

  9. malloc函数 链表 运行时才知道内存 动态内存

    https://baike.baidu.com/item/malloc函数 malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void ...

  10. Mark基本语法

    Markdown语法 1. 标题 样式的标题在行的开头使用1-6个#,对应于标题级别1-6.例如: 2.引用 在引用中再嵌套一个引用(在用">"的段落中使用"> ...