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(思维,树状数组的更多相关文章

  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. 2018 CCPC 秦皇岛 I (状压DP)

    题意: 首先t组数据  (t<=5),一个n代表有n件东西,每个东西可以代表两个物品,商品或者袋子,每个都有个值,如果这个要代表袋子的话,当前就代表是容量,而且必须把其他几件不是袋子的物品放一些 ...

  2. .NET COM+级别的事务Transaction实现

    参考: https://docs.microsoft.com/zh-cn/dotnet/api/system.enterpriseservices.contextutil?view=netframew ...

  3. 关于Java协变性的思考

    简而言之,如果A IS-A B,那么A[] IS-A B[]. 举例:现在有类型Person.Employee和Student.Employee 是一个(IS-A) Person,Student是一个 ...

  4. qbzt day2 晚上(竟然还有晚上)

    内容提要 搜索 拓展欧几里得 逆元 先是搜索 A* 有几个数组 g 当前点到根节点的深度 h 当前点到终点理想的最优情况需要走几步 f  f=g+h A*就是把所有的f从小到大排序 启发式搜索相较于其 ...

  5. SpringBoot整合jsp技术

    1.修改pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  6. mooc-IDEA 快速通过mapper定位到XML

    intellij 快速通过mapper定位到XML 方法一:在线安装Mybatis插件 安装完毕,重启IDEA生效! =>Mapper文件效果 点击向下键头即可定位到XML文件中的具体SQL = ...

  7. oracle--增删改、表备份、集合运算

    truncate 插入数据学习及数据的备份 单表查询语句(select) 1.插入数据(insert) 1.语法规范 insert into 表名(字段1,字段2,字段3,....)values('值 ...

  8. CentOS7 修复boot目录

    这里为了达到实验目的,首先删除boot目录下所有内容 重启后发现系统进不去了,这正是我们想要的 进入系统救援模式,以重新引导系统 进入救援模式后,输入以下命令进行修复boot目录 重启后,能正常引导系 ...

  9. POJ-2352.Stats(树状数组简单应用)

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 58255   Accepted: 24860 Descripti ...

  10. webpack打包html里的img图片

    对待css里的图片, 因为已经通过引入css文件到js,打包了,可以正常通过module.rules.test检测到,然后正常打包. 但是对于html里的图片, 这个需要安装一个插件html-with ...