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,就能实现上式的加或减。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<stack>
  7. #include<cstdlib>
  8. #include<queue>
  9. #include<set>
  10. #include<string.h>
  11. #include<vector>
  12. #include<deque>
  13. #include<map>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f3f3f3f3f
  16. #define inf 0x3f3f3f3f
  17. #define eps 1e-4
  18. #define bug printf("*********\n")
  19. #define debug(x) cout<<#x"=["<<x<<"]" <<endl;
  20. typedef long long LL;
  21. typedef long long ll;
  22. const int maxn = 1e6 + 5;
  23. const int mod = 998244353;
  24.  
  25. struct node{
  26. int flag;
  27. LL x,y,id;
  28. LL val;
  29. friend bool operator < (node a,node b) {
  30. if(a.y == b.y) {
  31. if(a.x == b.x) return a.flag < b.flag;
  32. return a.x < b.x;
  33. }
  34. return a.y < b.y;
  35. }
  36. }p[maxn];
  37.  
  38. LL re_val(LL x) {
  39. LL sum = 0;
  40. while(x > 0) {
  41. sum += x % 10;
  42. x /= 10;
  43. }
  44. return sum;
  45. }
  46. LL index(LL y,LL x,LL n) { //求的n * n的矩阵中(x,y)的值是多少
  47. LL mid = (n + 1) / 2;
  48. LL p = max(abs(x - mid), abs(y - mid));
  49. LL ans = n * n - (1 + p) * p * 4;
  50. LL sx = mid + p, sy = mid + p;
  51. if (x == sx && y == sy)
  52. return ans;
  53. else {
  54. if (y == sy || x == sx - 2 * p)
  55. return ans + abs(x - sx) + abs(y - sy);
  56. else
  57. return ans + 8 * p - abs(x - sx) - abs(y - sy);
  58. }
  59. }
  60.  
  61. LL c[maxn],ans[maxn];
  62. void init() {
  63. memset(c,0,sizeof c);
  64. memset(ans,0,sizeof ans);
  65. }
  66. LL lowbit(LL x) {
  67. return x & -x;
  68. }
  69. LL sum(LL i) {
  70. LL res = 0;
  71. while(i) {
  72. res += c[i];
  73. i -= lowbit(i);
  74. }
  75. return res;
  76. }
  77. LL n;
  78.  
  79. void add(LL i,LL t) {
  80. while(i <= maxn) {
  81. c[i] += t;
  82. i += lowbit(i);
  83. }
  84. }
  85. int main() {
  86. int t;
  87. scanf("%d",&t);
  88. while(t--) {
  89. init();
  90. LL m,q;
  91. scanf("%lld %lld %lld",&n,&m,&q);
  92. int cnt = 0;
  93. for(int i = 1;i <= m; i++) {
  94. LL x,y;
  95. scanf("%lld %lld",&x,&y);
  96. p[++cnt] = {0, x, y, -1, re_val(index(x,y,n))};
  97. }
  98. for(int i = 1; i <= q; i++) {
  99. LL x1,y1,x2,y2;
  100. scanf("%lld %lld %lld %lld",&x1,&y1,&x2,&y2);
  101. p[++cnt] = {1, x1 - 1, y1 - 1, i, 1};
  102. p[++cnt] = {1, x1 - 1, y2, i, -1};
  103. p[++cnt] = {1, x2, y1 - 1, i, -1};
  104. p[++cnt] = {1, x2, y2, i, 1};
  105. }
  106. sort(p + 1, p + 1 + cnt);
  107. for(int i = 1; i <= cnt; i++) {
  108. if(p[i].flag == 1) ans[p[i].id] += sum(p[i].x) * p[i].val;
  109. else add(p[i].x,p[i].val);
  110. }
  111. for(int i = 1; i <= q; i++)
  112. printf("%lld\n",ans[i]);
  113. }
  114. }

2019南京网赛 The beautiful values of the palace(思维,树状数组的更多相关文章

  1. 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 ...

  2. 2019icpc南京网络赛 A The beautiful values of the palace(离线+树状数组)

    题意: (假设所有的点对应的值已经求出)给你一个1e6*1e6的矩阵,有m<=1e5个点有值,其余都为0 q<=1e5个询问,求子矩阵的权值和 思路: 根据二维差分,对于询问左下角(x1, ...

  3. 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...

  4. 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...

  5. 2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 题目大意:给定一个含有n个数的序列,还有一个m,对于每个i(1<=i<=n)求出最少 ...

  6. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

  7. 2019牛客暑期多校训练营(第七场)F-Energy stones(思维+树状数组)

    >传送门< 题意:有n块能量石,每秒钟会增加Li的能量,但是一旦增长到了Ci它就不会增长了,它初始的能量为Ei. 现在有若干个时刻ti,会选择下标在[Si,Ti]的能量石吸取它们的能量,这 ...

  8. 2019牛客暑期多校训练营(第七场)E-Find the median(思维+树状数组+离散化+二分)

    >传送门< 题意:给n个操作,每次和 (1e9范围内)即往数组里面插所有 的所有数,求每次操作后的中位数思路:区间离散化然后二分答案,因为小于中位数的数字恰好有个,这显然具有单调性.那么问 ...

  9. 牛客网多校第5场 H subseq 【树状数组+离散化】

    题目:戳这里 学习博客:戳这里 题意:给n个数为a1~an,找到字典序第k小的序列,输出该序列所有数所在位置. 解题思路:先把所有序列预处理出来,方法是设一个数组为dp,dp[i]表示以i为开头的序列 ...

随机推荐

  1. Java测试笔记(ATM)

    本次Java测试主要是做一个与ATM相似的系统,用文本文件来作为用户数据库,实现存款.取款.转账.修改密码.查询余额的功能.在做这次测试之前老师并没有讲解与Java相关的知识,所以这就需要我们自学Ja ...

  2. 《Vue前端开发手册》

    序言 为了统一前端的技术栈问题,技术开发二部规定开发技术必须以Vue为主. 为了更好的规范公司的前端框架,现以我前端架构师为主,编写以下开发规范,如有不当的地方,欢迎批评教育并慢慢改善该开发文档,谢谢 ...

  3. css简单实现带箭头的边框

    原文地址 https://tianshengjie.cn/artic... css简单实现带箭头的边框 普通边框 <style> .border { width: 100px; heigh ...

  4. vue之router-link

    <router-link> 组件支持用户在具有路由功能的应用中(点击)导航.  1.to:表示目标路由的链接.当被点击后,内部会立刻把 to 的值传到 router.push(),所以这个 ...

  5. influxDB 1.3 中文文档

    influxDB是一个旨在处理高并发写入和查询负载的时序数据库,它是TICK框架的第二部分,influxdb用于任何包含大量时序数据应用的后台存储,包括Devops监控.应用指标数据.物联网传感器数据 ...

  6. (转)C#_WinForm接收命令行参数

    本文转载自:http://blog.csdn.net/lysc_forever/article/details/38356007 首先,我要仔细的声明下,本文讲的是接受命令行参数,让程序启动.而不是启 ...

  7. ScheduledThreadPoolExecutor 源码分析

    ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor 是能够在给定的延时之后.或周期性执行被提交任务的线程池 创建实例 /** * 线程池关闭 ...

  8. Eclipse Java工程转为Web工程步骤

    找到工程的.project文件,在<natures>标签中增加以下两行配置:<nature>org.eclipse.wst.common.modulecore.ModuleCo ...

  9. 四种方法给Vmware虚拟机清理瘦身

    随着VMware虚拟机使用时间的增长,其所占用的空间也越来越大,本文来说说怎么给VMware虚拟机占用的空间进行瘦身. **方法一:VMware自带的清理磁盘 **这个方法是VMware自带,具有普适 ...

  10. git总览

    git客户端官网:https://git-scm.com/ 下载对应版本安装 服务器安装git 安装依赖:yum install -y curl-devel expat-devel gettext-d ...