2019南京网赛 The beautiful values of the palace(思维,树状数组
https://nanti.jisuanke.com/t/41298
题意:给一个n * n的螺旋矩阵,n保证是奇数,取一些点使其、获得价值,价值为数位和,然后再给q次查询,求矩阵中的价值总和
思路:首先这题由点的位置求权值是一个思维点,可以先求出点位于哪一层螺旋中,然后将该层螺旋的起点数值获取,推出所求点数值。离散化地将每个点加入数组,用0和1标记是价值点还是询问点,四个询问点属于一个询问,然后将点按y,x,flag地顺序排序,ans = map[x2][y2]− map[x2][y1−1]− map[x1−1][y2]+ map[x1−1][y1−1],将询问点的权值设为-1或1,就能实现上式的加或减。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<stack>
#include<cstdlib>
#include<queue>
#include<set>
#include<string.h>
#include<vector>
#include<deque>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define eps 1e-4
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl;
typedef long long LL;
typedef long long ll;
const int maxn = 1e6 + 5;
const int mod = 998244353; struct node{
int flag;
LL x,y,id;
LL val;
friend bool operator < (node a,node b) {
if(a.y == b.y) {
if(a.x == b.x) return a.flag < b.flag;
return a.x < b.x;
}
return a.y < b.y;
}
}p[maxn]; LL re_val(LL x) {
LL sum = 0;
while(x > 0) {
sum += x % 10;
x /= 10;
}
return sum;
}
LL index(LL y,LL x,LL n) { //求的n * n的矩阵中(x,y)的值是多少
LL mid = (n + 1) / 2;
LL p = max(abs(x - mid), abs(y - mid));
LL ans = n * n - (1 + p) * p * 4;
LL sx = mid + p, sy = mid + p;
if (x == sx && y == sy)
return ans;
else {
if (y == sy || x == sx - 2 * p)
return ans + abs(x - sx) + abs(y - sy);
else
return ans + 8 * p - abs(x - sx) - abs(y - sy);
}
} LL c[maxn],ans[maxn];
void init() {
memset(c,0,sizeof c);
memset(ans,0,sizeof ans);
}
LL lowbit(LL x) {
return x & -x;
}
LL sum(LL i) {
LL res = 0;
while(i) {
res += c[i];
i -= lowbit(i);
}
return res;
}
LL n; void add(LL i,LL t) {
while(i <= maxn) {
c[i] += t;
i += lowbit(i);
}
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
init();
LL m,q;
scanf("%lld %lld %lld",&n,&m,&q);
int cnt = 0;
for(int i = 1;i <= m; i++) {
LL x,y;
scanf("%lld %lld",&x,&y);
p[++cnt] = {0, x, y, -1, re_val(index(x,y,n))};
}
for(int i = 1; i <= q; i++) {
LL x1,y1,x2,y2;
scanf("%lld %lld %lld %lld",&x1,&y1,&x2,&y2);
p[++cnt] = {1, x1 - 1, y1 - 1, i, 1};
p[++cnt] = {1, x1 - 1, y2, i, -1};
p[++cnt] = {1, x2, y1 - 1, i, -1};
p[++cnt] = {1, x2, y2, i, 1};
}
sort(p + 1, p + 1 + cnt);
for(int i = 1; i <= cnt; i++) {
if(p[i].flag == 1) ans[p[i].id] += sum(p[i].x) * p[i].val;
else add(p[i].x,p[i].val);
}
for(int i = 1; i <= q; i++)
printf("%lld\n",ans[i]);
}
}
2019南京网赛 The beautiful values of the palace(思维,树状数组的更多相关文章
- 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 val ...
- 2019icpc南京网络赛 A The beautiful values of the palace(离线+树状数组)
题意: (假设所有的点对应的值已经求出)给你一个1e6*1e6的矩阵,有m<=1e5个点有值,其余都为0 q<=1e5个询问,求子矩阵的权值和 思路: 根据二维差分,对于询问左下角(x1, ...
- 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...
- 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...
- 2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 题目大意:给定一个含有n个数的序列,还有一个m,对于每个i(1<=i<=n)求出最少 ...
- 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟
Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...
- 2019牛客暑期多校训练营(第七场)F-Energy stones(思维+树状数组)
>传送门< 题意:有n块能量石,每秒钟会增加Li的能量,但是一旦增长到了Ci它就不会增长了,它初始的能量为Ei. 现在有若干个时刻ti,会选择下标在[Si,Ti]的能量石吸取它们的能量,这 ...
- 2019牛客暑期多校训练营(第七场)E-Find the median(思维+树状数组+离散化+二分)
>传送门< 题意:给n个操作,每次和 (1e9范围内)即往数组里面插所有 的所有数,求每次操作后的中位数思路:区间离散化然后二分答案,因为小于中位数的数字恰好有个,这显然具有单调性.那么问 ...
- 牛客网多校第5场 H subseq 【树状数组+离散化】
题目:戳这里 学习博客:戳这里 题意:给n个数为a1~an,找到字典序第k小的序列,输出该序列所有数所在位置. 解题思路:先把所有序列预处理出来,方法是设一个数组为dp,dp[i]表示以i为开头的序列 ...
随机推荐
- 你还没搞懂this?
一.前言 this关键字是JavaScript中最复杂的机制之一.它是一个很特别的关键字,被自动定义在所有函数的作用域中.对于那些没有投入时间学习this机制的JavaScript开发者来说,this ...
- Spring Data JPA开发中遇到的问题1:org.hibernate.hql.internal.ast.QuerySyntaxException: DispatchShift is not mapped
org.hibernate.hql.internal.ast.QuerySyntaxException: T_D_SHIFT_DISPATCH is not mapped 错误原因: 没有映射到表,经 ...
- docker学习与应用
概要 众所周知,Docker是当前非常火的虚拟化容器技术,对于它的优势以及使用场景网上的资料非常多,这里我推荐大家去这个网站去详细学习docker. 我这里就写一下作为一名后端开发程序员,自己学习do ...
- C++中让人忽视的左值和右值
前言 为了了解C++11的新特性右值引用,不得不重新认识一下左右值.学习之初,最快的理解,莫过于望文生义了,右值那就是赋值号右边的值,左值就是赋值号左边的值.在中学的数学的学习中,我们理解的是,左值等 ...
- 2017埙箫简谱清单分享(附音频Demo)
前言 习箫五载,略有所获,皆在坚持. 本博文记录旨在记录练习过程中所录制的Demo以供自省.自娱.自乐,同时记录.分享简谱与箫友(目前为简谱,日后学会线谱后会添加相应谱子分类). 简谱 &&a ...
- [Udemy] ES 7 and Elastic Stack - part 3
Section 7: Analyzing Log Data with the Elastic Stack
- 关于Vue的理解以及与React框架的对比
1.Vue的理解 概念: Vue是一套用于构建用户界面的渐进式框架: Vue的核心库只关注视图层: 是一个数据驱动的MVVM框架: 特性: 确实轻量:体积比较小: 数据绑定简单.方便: 有一些简单的内 ...
- 20191105 《Spring5高级编程》笔记-第16章
第16章 Web应用程序 16.3 MVC和Spring MVC介绍 16.3.3 Spring MVC WebApplicationContext层次结构 在Spring MVC中,Dispatch ...
- STL 配接器(adapters)
定义 配接器(adapters):将一个class的接口,转换为另一个class的接口,使得原来不能一起使用相互兼容的classes,可以一起协同工作. 配接器是一种设计模式. STL中提供的各种配接 ...
- Java 动态代理及AOP实现机制
AOP实现机制http://www.iteye.com/topic/1116696 AOP: (Aspect Oriented Programming) 面向切面编程AOP包括切面(aspect).通 ...