「BOI2007」Mokia

传送门

把查询拆成四部分然后容斥计算答案(二维前缀和)

然后 \(\text{CDQ}\) 分治算答案。

参考代码:

#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} const int _ = 2e5 + 5, __ = 2e6 + 5; int W, tr[__], num;
struct node { int opt, id, x, y, v, ans; } t[_], tt[_];
inline bool cmp(const node& a, const node& b) { return a.id < b.id; } inline void update(int x, int v)
{ for (rg int i = x; i <= W; i += i & -i) tr[i] += v; } inline int query(int x)
{ int res = 0; for (rg int i = x; i >= 1; i -= i & -i) res += tr[i]; return res; } inline void CDQ(int l, int r) {
if (l == r) return ;
int mid = (l + r) >> 1;
CDQ(l, mid), CDQ(mid + 1, r);
int i = l, j = mid + 1, p = l;
while (i <= mid && j <= r) {
if (t[i].x <= t[j].x) { if (t[i].opt == 0) update(t[i].y, t[i].v); tt[p++] = t[i++]; }
else { if (t[j].opt == 1) t[j].ans += query(t[j].y); tt[p++] = t[j++]; }
}
while (i <= mid) { if (t[i].opt == 0) update(t[i].y, t[i].v); tt[p++] = t[i++]; }
while (j <= r) { if (t[j].opt == 1) t[j].ans += query(t[j].y); tt[p++] = t[j++]; }
for (rg int i = l; i <= mid; ++i) if (t[i].opt == 0) update(t[i].y, -t[i].v);
for (rg int i = l; i <= r; ++i) t[i] = tt[i];
} int main() {
int opt; read(opt), read(W);
while (1) {
read(opt); if (opt == 3) break ;
if (opt == 1) {
int x, y, v; read(x), read(y), read(v);
t[++num] = (node) { 0, num, x, y, v, 0 };
} else {
int xl, yl, xr, yr;
read(xl), read(yl), read(xr), read(yr);
t[++num] = (node) { 1, num, xr, yr, 0, 0 };
t[++num] = (node) { 1, num, xl - 1, yr, 0, 0 };
t[++num] = (node) { 1, num, xr, yl - 1, 0, 0 };
t[++num] = (node) { 1, num, xl - 1, yl - 1, 0, 0 };
}
}
CDQ(1, num), sort(t + 1, t + num + 1, cmp);
for (rg int i = 1; i <= num; ++i)
if (t[i].opt == 1)
printf("%d\n", t[i].ans - t[i + 1].ans - t[i + 2].ans + t[i + 3].ans), i += 3;
return 0;
}

最新文章

  1. [CentOS] 指定命令别名:Alias &amp; 软链接生成命令 ln -s
  2. HttpContext详解
  3. Maven 自动部署
  4. 理解Linux中断 (2)【转】
  5. javascript/jquery给动态加载的元素添加click事件
  6. 转载——Struts2中的constant详解
  7. 对TCP说三道四(三次握手)
  8. Windows常用的一些DOS命令整理
  9. 【php】phpExcel使用教程,如何导出excel表格
  10. 【HTML】 HTML基础知识 一些标签
  11. Scrum笔记
  12. Jexus 安装asp.net mvc EF 项目引发的错误总
  13. maven 学习笔记--仓库,聚合和继承,私服搭建
  14. 从操作系统rm数据文件后,利用句柄与rman恢复的过程。(已验证)
  15. OC 线程操作2 - NSThread
  16. 【POJ】1819.Disks
  17. 使用JDK实现动态代理
  18. python定制类(1):__getitem__和slice切片
  19. svg_png
  20. [集合]Collection集合框架源码分析

热门文章

  1. acm数论之旅--欧拉函数的证明
  2. esp8266(wifi)模块调试记录
  3. 一些封装的php函数
  4. 一个汇编小demo
  5. makefile的书写
  6. 【C语言】(数组)C语言字符串中的小写字母转换为大写字母
  7. 【C语言】(for循环嵌套)找出1000以内的水仙花数
  8. IDEA启动报错-java.net.BindException: Address already in use: bind
  9. Windows配置本地Hadoop运行环境
  10. cmd添加管理员账号