如果询问 \(x_1, y_1, x_2, y_2\),

那么询问

\((x_2, y_2)\),

\((x_2, y_1 - 1)\),

\((x_1 - 1, y_2)\)

\((x_1 - 1, y_1 - 1\)),

这些点到原点(不一定是 \((0, 0)\),有可能有负数)的和。

设其结果分别为 \(a, b, c, d\),那么最后结果为 \(a - b - c + d\)(二维前缀和原理)。

问题成功转化。


设结构体

struct Node {
int x, y; // 位置
int z; // 值
};

为基本信息。

我们在此基础上加一个 \(type\) 和 \(res\),

如果 \(type\) 为 \(1\) 就表示要询问 \((x, y)\) 的二维前缀和,结果保存在 \(res\) 中。

如果 \(type\) 为 \(0\) 表示 \((x, y)\) 为一个基站,其功率为 \(z\)。


对于 \(type_i\) 为 \(1\) 的部分,

使用CDQ分治统计:

\(type_j < type_i\) (即 \(type_j\) 为 \(0\))

\(x_j \leq x_i\)

\(y_j \leq y_i\)

的各个位置的和即可。

注意开long long

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map> #define int long long using namespace std; const int N = 500010; struct Node {
int x, y, z;
int type;
int res;
}a[N], tmp[N]; bool cmp(const Node a, const Node b) {
if (a.x != b.x) return a.x < b.x;
if (a.y != b.y) return a.y < b.y;
return a.type < b.type;
} int n, m; void cdq(int l, int r) {
if (l == r) return;
int mid = (l + r) / 2;
cdq(l, mid);
cdq(mid + 1, r); int sum = 0; int p = l, q = mid + 1, tot = l;
while (p <= mid && q <= r) {
if (a[p].y <= a[q].y) {
if (!a[p].type) sum += a[p].z;
tmp[tot++] = a[p++];
}
else {
if (a[q].type) a[q].res += sum;
tmp[tot++] = a[q++];
}
}
while (p <= mid) {
if (!a[p].type) sum += a[p].z;
tmp[tot++] = a[p++];
}
while (q <= r) {
if (a[q].type) a[q].res += sum;
tmp[tot++] = a[q++];
}
for (int i = l; i <= r; i++) a[i] = tmp[i];
} struct Query {
int x1, y1;
int x2, y2;
}query[N]; unordered_map<int, unordered_map<int, int> > res_a; signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y >> a[i].z;
a[i].type = 0;
a[i].res = 0;
}
int tot = n;
for (int i = 1; i <= m; i++) {
cin >> query[i].x1 >> query[i].y1 >> query[i].x2 >> query[i].y2;
a[++tot] = {query[i].x1 - 1, query[i].y1 - 1, 0, 1, 0};
a[++tot] = {query[i].x2, query[i].y2, 0, 1, 0};
a[++tot] = {query[i].x2, query[i].y1 - 1, 0, 1, 0};
a[++tot] = {query[i].x1 - 1, query[i].y2, 0, 1, 0};
}
sort(a + 1, a + tot + 1, cmp);
cdq(1, tot);
for (int i = 1; i <= tot; i++) {
if (a[i].type) {
res_a[a[i].x][a[i].y] = a[i].res;
}
}
for (int i = 1; i <= m; i++) {
int x1 = query[i].x1, y1 = query[i].y1;
int x2 = query[i].x2, y2 = query[i].y2; int ans = res_a[x2][y2] - res_a[x2][y1 - 1] - res_a[x1 - 1][y2] + res_a[x1 - 1][y1 - 1];
cout << ans << '\n';
}
return 0;
}

P3755 [CQOI2017]老C的任务题解的更多相关文章

  1. P3755 [CQOI2017]老C的任务

    传送门 可以离线,把询问拆成四个,然后把所有的按\(x\)坐标排序,这样就只要考虑\(y\)坐标了.然后把\(y\)坐标离散化,用树状数组统计即可 记得开longlong //minamoto #in ...

  2. [CQOI2017]老C的键盘

    [CQOI2017]老C的键盘 题目描述 额,网上题解好像都是用的一大堆组合数,然而我懒得推公式. 设\(f[i][j]\)表示以\(i\)为根,且\(i\)的权值为\(j\)的方案数. 转移: \[ ...

  3. 【BZOJ4822】[CQOI2017]老C的任务(扫描线)

    [BZOJ4822][CQOI2017]老C的任务(扫描线) 题面 BZOJ 洛谷 题解 没有修改操作,都不需要分治了... 直接排序之后扫描线算贡献就好了... 不知道为啥洛谷上过不了... #in ...

  4. bzoj4823: [Cqoi2017]老C的方块(最小割)

    4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强   黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...

  5. 【BZOJ4823】[CQOI2017]老C的方块(网络流)

    [BZOJ4823][CQOI2017]老C的方块(网络流) 题面 BZOJ 题解 首先还是给棋盘进行黑白染色,然后对于特殊边左右两侧的格子单独拎出来考虑. 为了和其他格子区分,我们把两侧的这两个格子 ...

  6. bzoj 4822: [Cqoi2017]老C的任务

    4822: [Cqoi2017]老C的任务 练手速... #include <iostream> #include <cstdio> #include <cstring& ...

  7. bzoj 4823: [Cqoi2017]老C的方块 [最小割]

    4823: [Cqoi2017]老C的方块 题意: 鬼畜方块游戏不解释... 有些特殊边,有些四个方块组成的图形,方块有代价,删掉一些方块使得没有图形,最小化代价. 比较明显的最小割,一个图形中必须删 ...

  8. [BZOJ4824][Cqoi2017]老C的键盘 树形dp+组合数

    4824: [Cqoi2017]老C的键盘 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 218  Solved: 171[Submit][Statu ...

  9. [BZOJ4822][CQOI2017]老C的任务(扫描线+树状数组)

    4822: [Cqoi2017]老C的任务 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 379  Solved: 203[Submit][Statu ...

  10. [BZOJ4824][CQOI2017]老C的键盘(树形DP)

    4824: [Cqoi2017]老C的键盘 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 193  Solved: 149[Submit][Statu ...

随机推荐

  1. Cesium 案例(九)示例中小程序集合(1)

    因为这几天在忙一些客观上无法逃脱的事,没有大块时间对中大型案例进行学习,所以对官方案例中的代码不超过40行的程序进行了学习.我把他们放在一到两个随笔中. 注:[所有案例中最前面务必加上] 1 Cesi ...

  2. 授权普通用户 sudo -i 使其具有root的权限

    1.ssh 秘钥登录授权 将此代码片更名为  adduser.sh 放在linux的 /root 目录下 #!/bin/sh echo 'ssh-key:' $1 echo 'username:' $ ...

  3. 在有限 computational budget 下,借助 low-fidelity 模型提高精度

    论文名称:context-aware learning of hierarchies of low-fidelity models for multi-fidelity uncertainty qua ...

  4. BAT 基础语法

    命令 //功能 echo //标准输出命令 在CMD窗口中 显示echo 后的内容 @ //关闭当前行的 回显    回显:源代码在 CMD 窗口中再次显示 pasue     // 暂停程序 的执行 ...

  5. ASTAR机台(win7 p'rofessional)使用python tool中文显示异常问题解决

    1.双击"computer"打开界面如下,再单击"open control panel"打开控制面板. 2.在控制面板中点击"Clock,Langua ...

  6. 05-打包样式资源(编写webpack配置文件)

    /** * webpack.config.js webpack的配置文件 * 作用:指示 webpack 干哪些活(当你运行 webpack 指令时,会加载里面的配置) * * 所有构件工具都是基于n ...

  7. [OpenCV-Python] 15 图像阈值

    文章目录 OpenCV-Python:IV OpenCV中的图像处理 15 图像阈值 15.1 简单阈值 15.2 自适应阈值 15.3 Otsu' 's 二值化 15.4 Otsu' 's 二值化是 ...

  8. Xposed框架关于无法在模拟器中下载和激活的问题

    开头 最近xposed不知道出了什么问题,导致安装的时候一直在失败,所以记录下网上参考到的并用于实践中 安装软件 1.模拟器 逍遥游模拟器 安卓7.1 版本.下载地址为: https://www.52 ...

  9. 2022-04-27:用go语言重写ffmpeg的remuxing.c示例。

    2022-04-27:用go语言重写ffmpeg的remuxing.c示例. 答案2022-04-27: ffmpeg的remuxing.c是一个用于将多媒体文件从一种容器格式转换为另一种容器格式的命 ...

  10. Can't uninstall 'Pillow'. No files were found to uninstall.

    Can't uninstall 'Pillow'. No files were found to uninstall. Pillow卸载不掉的解决办法 1.进入python所在路径,进入scripts ...