bzoj1176: [Balkan2007]Mokia【cdq分治】
2015-12-23 12:44
513 查看
把询问搞成4个,cdq分治。
View Code
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a;i <= b; i++) #define drep(i, a, b) for (int i = a; i >= b; i--) #define REP(i, a, b) for (int i = a; i < b; i++) #define mp make_pair #define pb push_back #define clr(x) memset(x, 0, sizeof(x)) #define xx first #define yy second using namespace std; typedef long long i64; typedef pair<int, int> pii; const int inf = ~0U >> 1; const i64 INF = ~0ULL >> 1; //********************************** const int maxn = 200005; int c[2000005], w; struct Complex { int flag; int x, y, c; int id, ans; int pos, l; inline bool operator < (const Complex &a) const { return x < a.x || x == a.x && y < a.y || x == a.x && y == a.y && c < a.c; } } src[maxn], t[maxn]; inline void add(int x, int v) { while (x <= w) { c[x] += v; x += x & -x; } } inline int get(int x) { int ret(0); while (x > 0) { ret += c[x]; x -= x & -x; } return ret; } int ans[10005]; void cdq(int l, int r) { if (l == r) return; int mid = l + r >> 1, l1 = l, l2 = mid + 1; rep(i, l, r) { if (src[i].id <= mid && !src[i].l) add(src[i].y, src[i].c); if (src[i].id > mid && src[i].l) ans[src[i].pos] += src[i].l * get(src[i].y); } rep(i, l, r) if (src[i].id <= mid && !src[i].l) add(src[i].y, -src[i].c); rep(i, l, r) if (src[i].id <= mid) t[l1++] = src[i]; else t[l2++] = src[i]; memcpy(src + l, t + l, (r - l + 1) * sizeof(Complex)); cdq(l, mid); cdq(mid + 1, r); } int main() { int cnt(0), n(0), s; scanf("%d%d", &s, &w); int flag; while (scanf("%d", &flag), flag ^ 3) { if (flag == 1) { ++n; src .id = n; src .l = 0; src .pos = 0; scanf("%d%d%d", &src .x, &src .y, &src .c); } else { int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b); ans[++cnt] = s * (a - x) * (b - y); ++n; src .id = n; src .l = 1; src .pos = cnt; src .x = a, src .y = b, src .c = inf; ++n; src .id = n; src .l = -1; src .pos = cnt; src .x = a, src .y = y - 1, src .c = inf; ++n; src .id = n; src .l = -1; src .pos = cnt; src .x = x - 1, src .y = b, src .c = inf; ++n; src .id = n; src .l = 1; src .pos = cnt; src .x = x - 1, src .y = y - 1, src .c = inf; } } sort(src + 1, src + n + 1); cdq(1, n); rep(i, 1, cnt) printf("%d\n", ans[i]); return 0; }
View Code
相关文章推荐
- JQuery中$.ajax()方法参数详解
- 面向对象的六大设计原则(三):依赖倒置原则
- Mysql,Oracle,Java数据类型对应
- C#的注释方法与数据类型
- Content-Based Information Retrieval 基于内容的信息检索
- 拷贝构造函数什么时候调用?
- C语言学习笔记--------伊能C语言学习笔记----使用指针来解决函数的副本机制
- 【转载】愚者与智者的48个差距
- poj 2395 Out of Hay(最小生成树)
- linux内核中的GPIO系统之(2):pin control subsystem
- 你为什么要努力?(很深刻)
- ARM LINUX 扩展串口
- 创业板VOL-MY 10点研究
- CBIR: Indexing and Retrieval--基于内容的图像检索:索引和检索
- C语言学习笔记----伊能C语言学习笔记-----函数的副本机制
- bzoj3110: [Zjoi2013]K大数查询 【cdq分治&树套树】
- 软件工程学习总结
- Sales Engineer
- Android 基于MVC的MVVM模式开发
- Android 基于MVC的MVVM模式开发