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]);
}
}

最新文章

  1. Web Map Gis 开发系列索引
  2. iOS UIAlertController跟AlertView用法一样 &amp;&amp; otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法
  3. TCP 常用总结
  4. 浅谈FloatingActionButton(悬浮按钮)
  5. 如何仿写thinkphp的C方法?
  6. COJ 2105 submatrix
  7. servlet的filter的使用
  8. iOS开发分析&amp;quot;秘密&amp;quot;App内容页面的效果(两)
  9. Android便携式热点的开启状态检测和SSID的获取
  10. Dynamics CRM 2015Online Update1 new feature之 插件跟踪日志
  11. Jira与Confluence集成、授权信息查看和问题汇总
  12. Java中方法定义和调用的学习
  13. 浅谈编码Base64、Hex、UTF-8、Unicode、GBK等
  14. 理解 PHP 依赖注入
  15. Mask RCNN 简单使用
  16. 本地tp项目上传服务器报runtime/cache错误
  17. Qt_简介
  18. TCP 流量控制、拥塞控制
  19. 第4章&#160;Selenium2-java WebDriver API (一)
  20. MVP架构学习

热门文章

  1. USACO2018 DEC (Gold) (dp,容斥+哈希,最短路)
  2. Spring Cloud教程(十)自定义引导配置属性源
  3. E. Compress Words
  4. BigDecimal保留两位小数,或指定精确的位数
  5. CG-CTF | I wanna play CTF
  6. java.lang.OutOfMemoryError:GC overhead limit exceeded解决方
  7. sql分别用日期、月、年 分组 group by 分组,datepart函数
  8. leetcode-mid-array-334 Increasing Triplet Subsequence-NO
  9. H5 刮图-刮一刮
  10. python使用__new__创建一个单例模式(单例对象)