【线段树】 HDOJ 5367 digger
2015-08-09 18:41
260 查看
线段树区间合并。。。
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 30000005; struct node { int l, r, l_h, r_h, l_c, r_c, l_s, r_s, delta, ans; node *lson, *rson; }pool[maxn], *tail, *root; int n, m, R; node* newnode(int l, int r, int h) { tail->l = l, tail->r = r; tail->l_h = tail->r_h = h; tail->l_s = tail->r_s = 0; tail->l_c = tail->r_c = r - l + 1; tail->lson = tail->rson = 0; tail->delta = tail->ans = 0; return tail++; } void init() { tail = pool; root = newnode(1, n, R); } void pushup(node *o) { o->ans = o->lson->ans + o->rson->ans; o->l_h = o->lson->l_h; o->r_h = o->rson->r_h; o->l_c = o->lson->l_c; o->r_c = o->rson->r_c; if(o->lson->r_h == o->rson->l_h) { if(o->lson->r_s == 1 && o->rson->l_s == 1) o->ans += o->lson->r_c + o->rson->l_c; if(o->rson->r_s == 0) o->r_s = o->lson->r_s, o->r_c += o->lson->r_c; else o->r_s = o->rson->r_s; if(o->lson->l_s == 0) o->l_s = o->rson->l_s, o->l_c += o->rson->l_c; else o->l_s = o->lson->l_s; } else if(o->lson->r_h > o->rson->l_h) { if(o->rson->l_s == 1) o->ans += o->rson->l_c; if(o->lson->l_s == 0) o->l_s = 2; else o->l_s = o->lson->l_s; if(o->rson->r_s == 0) o->r_s = 1; else o->r_s = o->rson->r_s; } else { if(o->lson->r_s == 1) o->ans += o->lson->r_c; if(o->lson->l_s == 0) o->l_s = 1; else o->l_s = o->lson->l_s; if(o->rson->r_s == 0) o->r_s = 2; else o->r_s = o->rson->r_s; } } void pushdown(node *o) { if(o->delta) { if(!o->lson) o->delta = 0; else { o->lson->l_h += o->delta; o->lson->r_h += o->delta; o->lson->delta += o->delta; o->rson->l_h += o->delta; o->rson->r_h += o->delta; o->rson->delta += o->delta; o->delta = 0; } } } void update(node *o, int ql, int qr, int v) { if(ql <= o->l && qr >= o->r) { o->delta += v; o->l_h += v; o->r_h += v; return; } pushdown(o); int mid = o->l + (o->r - o->l) / 2; if(!o->lson) { o->lson = newnode(o->l, mid, o->l_h); o->rson = newnode(mid+1, o->r, o->l_h); } if(ql <= mid) update(o->lson, ql, qr, v); if(qr > mid) update(o->rson, ql, qr, v); pushup(o); } void work() { int ans = 0, ql, qr, v; while(m--) { scanf("%d%d%d", &ql, &qr, &v); ql ^= ans, qr ^= ans, v ^= ans; update(root, ql, qr, -v); printf("%d\n", ans = root->ans); } } int main() { while(scanf("%d%d%d", &n, &m, &R) != EOF) { init(); work(); } return 0; }
相关文章推荐
- jquery 解析返回的xml和json
- 程序员的谈判技巧
- E - Minimum Cost - POJ 2516(最小费)
- 关于动态规划与分治的问题
- 313.C
- Microsoft 神经网络分析算法
- HDU 1542 Atlantis 线段树+扫描线
- androidannotations编译错误
- hdu 1239 Calling Extraterrestrial Intelligence Again(素数,枚举)
- Android 之夜间模式(多主题)的实现
- hdu 1239 Calling Extraterrestrial Intelligence Again(素数,枚举)
- POJ 2492 A Bug's Life(虫虫危机)
- CButtonST使用技巧: CButtonST简介
- poj1703 并查集
- 十个利用矩阵乘法解决的经典题目
- 获取全部子类或接口的全部实现
- Pots
- iPhone开发中全局静态变量内存的管理
- Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏
- 文章标题