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为开头的序列 ...
随机推荐
- Java测试笔记(ATM)
本次Java测试主要是做一个与ATM相似的系统,用文本文件来作为用户数据库,实现存款.取款.转账.修改密码.查询余额的功能.在做这次测试之前老师并没有讲解与Java相关的知识,所以这就需要我们自学Ja ...
- 《Vue前端开发手册》
序言 为了统一前端的技术栈问题,技术开发二部规定开发技术必须以Vue为主. 为了更好的规范公司的前端框架,现以我前端架构师为主,编写以下开发规范,如有不当的地方,欢迎批评教育并慢慢改善该开发文档,谢谢 ...
- css简单实现带箭头的边框
原文地址 https://tianshengjie.cn/artic... css简单实现带箭头的边框 普通边框 <style> .border { width: 100px; heigh ...
- vue之router-link
<router-link> 组件支持用户在具有路由功能的应用中(点击)导航. 1.to:表示目标路由的链接.当被点击后,内部会立刻把 to 的值传到 router.push(),所以这个 ...
- influxDB 1.3 中文文档
influxDB是一个旨在处理高并发写入和查询负载的时序数据库,它是TICK框架的第二部分,influxdb用于任何包含大量时序数据应用的后台存储,包括Devops监控.应用指标数据.物联网传感器数据 ...
- (转)C#_WinForm接收命令行参数
本文转载自:http://blog.csdn.net/lysc_forever/article/details/38356007 首先,我要仔细的声明下,本文讲的是接受命令行参数,让程序启动.而不是启 ...
- ScheduledThreadPoolExecutor 源码分析
ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor 是能够在给定的延时之后.或周期性执行被提交任务的线程池 创建实例 /** * 线程池关闭 ...
- Eclipse Java工程转为Web工程步骤
找到工程的.project文件,在<natures>标签中增加以下两行配置:<nature>org.eclipse.wst.common.modulecore.ModuleCo ...
- 四种方法给Vmware虚拟机清理瘦身
随着VMware虚拟机使用时间的增长,其所占用的空间也越来越大,本文来说说怎么给VMware虚拟机占用的空间进行瘦身. **方法一:VMware自带的清理磁盘 **这个方法是VMware自带,具有普适 ...
- git总览
git客户端官网:https://git-scm.com/ 下载对应版本安装 服务器安装git 安装依赖:yum install -y curl-devel expat-devel gettext-d ...