The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)
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(树状数组+思维)的更多相关文章
- The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解
(施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...
- [The Preliminary Contest for ICPC Asia Nanjing 2019] A-The beautiful values of the palace(二维偏序+思维)
>传送门< 前言 这题比赛的时候觉得能做,硬是怼了一个半小时,最后还是放弃了.开始想到用二维前缀和,结果$n\leq 10^{6}$时间和空间上都爆了,没有办法.赛后看题解用树状数组,一看 ...
- 计蒜客 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 ...
- The Preliminary Contest for ICPC Asia Nanjing 2019
传送门 A. The beautiful values of the palace 题意: 给出一个\(n*n\)的矩阵,并满足\(n\)为奇数,矩阵中的数从右上角开始往下,类似于蛇形填数那样来填充. ...
- The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail
题目链接:https://nanti.jisuanke.com/t/41305 题目说的很明白...只需要反向跑spfa然后输入-dis,然后添-dis的一条边就好了... #include < ...
- 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 ...
- 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019
题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...
- 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 ...
- H.Holy Grail ( floyd )(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意: 给出一个有向图,再给出6条原来不存在的路径,让你在这6条路径上添加一个最小的数,使图不存在负环. 思路: 直接6遍 floyd 输出就行了. #include <bits/stdc++. ...
随机推荐
- hadoop完全分布式
虚拟机克隆 a. vim /etc/udev/rules.d/70-persistent-net.rules 更改网卡名 b. vim /etc/sysconfig/network-scrip ...
- 这么优雅的Java ORM没见过吧!
Java的ORM框架有很多,但由于Java语言的限制大部分都不够优雅也不够简单,所以作者只能另辟蹊径造轮子了.照旧先看示例代码了解个大概,然后再解释实现原理. 一.ORM示例 1. Insert ...
- C++题目东华
1. 定义一个点类Point,其有两个double型的私有数据成员x和y.此外还包含以下公有成员函数: (1)构造函数,给点初始化: (2)setPoint函数,设置点坐标值: (3)distance ...
- tf.argmax(vector,axis)函数的使用
1.返回值 vector为向量,返回行或列的最大值的索引号: vector为矩阵,返回值是向量,返回每行或每列的最大值的索引号. 2.参数 vector为向量或者矩阵 axis = 0 或1 0:返回 ...
- 基于 MPI/OpenMP 混合编程的大规模多体(N-Body)问题仿真实验
完整代码: #include <iostream> #include <ctime> #include <mpi.h> #include <omp.h> ...
- maven生命周期与插件
目录 Maven生命周期 clean default site 命令与对应周期 插件与绑定 插件目标 插件绑定 内置绑定 自定义绑定 插件配置 本文主要是针对<maven实战>书中关键知识 ...
- 通过show profile分析sql语句
set profling=1; select count(*) from xuehao; show profiles; show profile for query 1; mysql> set ...
- CodeMonke少儿编程第1章 step与turn
第1章 step与turn 目标 了解游戏舞台的各组成部分 掌握step和turn指令的用法 说起计算机,对于不了解它的人来说,也许会感到有些神秘,其实不然,它不过是能够接收指令并且按照指令执行的一种 ...
- python中IF语句容易犯的错误CASE
python中没有switch case类似的语句,但是下面的IF语句却与之类似,却又不同: A = B = C = D = E = 1 if A == 1: B=2 elif B ==2: C= ...
- Java编程开发之浅析Java引用机制
对于一个Java的对象而言,存储主要分为两种,一种是内存堆(Heap),内存堆是无序的,主要用来存放创建的Java对象:一种是内存栈(Stack),主要用来存放Java引用,然后在管理过程使用Java ...