如果询问 \(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. python:模拟购票的小程序

    问题描述:小白学习python的第N天,继续练习.做一个模拟购票的小程序,没有用数据库和文件来存储数据,只是能够单词选择. # hzh 每天进步一点点 # 2022/5/13 17:24 import ...

  2. 介绍箭头函数的 this

    由于箭头函数不绑定this, 它会捕获其所在(即定义的位置)上下文的this值, 作为自己的this值 1. 所以 call() / apply() / bind() 方法对于箭头函数来说只是传入参数 ...

  3. mysql 命令批量修改一个字段/帝国cms sql命令修改一个字段

    UPDATE phome_enewstagsdata SET classid=5 where classid=1 UPDATE phome_ecms_news SET classid=8 where ...

  4. SpringBoot自动装配原理剖析(自己理解,有错请指出)

    注解 主类 @SpringBootApplication @EnableAutoConfiguration @Import({AutoConfigurationImportSelector.class ...

  5. 数据分析02-(pandas介绍、jupyter notebook)

    数据分析-02 数据分析-02 pandas pandas介绍 pandas核心数据结构 Series DataFrame 核心数据结构操作 复合索引 Jupyter notebook 数据加载 处理 ...

  6. P2482 [SDOI2010] 猪国杀

    方法论 这是一道复杂的模拟题.由于游戏规则的条目很多,我们需要仔细考虑程序的组织.否则,在编写程序的过程中极容易陷入停滞的状态(不知道下一步应该怎么做),或在发现程序出问题时,难以快速定位到错误点,对 ...

  7. 学node 之前你要知道这些

    初识nodejs   19年年底一个偶然的机会接到年会任务,有微信扫码登录.投票.弹幕等功能,于是决定用node 来写几个服务,结果也比较顺利.   当时用看了下koa2的官方文档,知道怎么连接数据库 ...

  8. 文心一言 VS chatgpt (12)-- 算法导论3.1 6~7题

    六.证明:一个算法的运行时间为θ(g(n))当且仅当其最坏情况运行时间为O(g(n)),且其最好情况运行时间为Ω(g(n)) . 文心一言: chatgpt: 要证明「一个算法的运行时间为θ(g(n) ...

  9. 2022-10-06:以下go语言代码输出什么?A:[1 2 3] [1 2 3] ;B:[1 2 3] [3 4 5]; C:[1 2 3] [3 4 5 6 7 8 9];D:[1 2 3] [3

    2022-10-06:以下go语言代码输出什么?A:[1 2 3] [1 2 3] :B:[1 2 3] [3 4 5]: C:[1 2 3] [3 4 5 6 7 8 9]:D:[1 2 3] [3 ...

  10. vue全家桶进阶之路17:组件与组件间的通信

    在 Vue2 中,组件与组件之间的通信可以通过以下几种方式来实现: Props 和 Events 这是 Vue2 中最基础和常用的父子组件通信方式.父组件通过属性传递数据给子组件,子组件通过事件触发向 ...