POJ 3468 A Simple Problem with Integers 伸展树splay 区间更新
来源:互联网 发布:mysql批量insert bug 编辑:程序博客网 时间:2024/06/07 19:57
题目:
http://poj.org/problem?id=3468
题意:
给定一个序列,有两种操作:
1. Q a b询问[a,b]区间内元素和
2. C a b c把区间[a,b]内的元素全部加上c
思路:
线段树区间更新模板题,用splay来找虐了。。。旋转某个节点前,要把它所有的祖先节点都往下更新,旋转节点时也要让节点向下更新。最后,这个题,用单旋过的很愉快,用双旋卡着时间过。。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define key_val son[son[root][1]][0]typedef long long ll;const int N = 100010, INF = 0x3f3f3f3f, MOD = 1000000;int son[N][2], pre[N], siz[N];ll key[N], val[N], lazy[N];int root, num, cas;int arr[N];int n, m;void new_node(int &x, int fa, int v){ x = ++num; pre[x] = fa, key[x] = val[x] = v; siz[x] = 1, lazy[x] = 0; son[x][0] = son[x][1] = 0;}void push_up(int x){ siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1; val[x] = key[x] + lazy[x] + val[son[x][0]] + val[son[x][1]];}void push_down(int x){ if(lazy[x]) { key[x] += lazy[x]; lazy[son[x][0]] += lazy[x], lazy[son[x][1]] += lazy[x]; val[son[x][0]] += siz[son[x][0]] * lazy[x]; val[son[x][1]] += siz[son[x][1]] * lazy[x]; lazy[x] = 0; }}void _rotate(int x, int dir){ int y = pre[x]; push_down(y), push_down(x); son[y][!dir] = son[x][dir], pre[son[x][dir]] = y; if(pre[y]) son[pre[y]][son[pre[y]][1]==y] = x; pre[x] = pre[y]; son[x][dir] = y, pre[y] = x; push_up(y);}void splay(int x, int goal){ push_down(x); while(pre[x] != goal) { int y = pre[x]; if(pre[y] == goal) _rotate(x, son[y][0] == x); else { int dir = son[pre[y]][0] == y; if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir); else _rotate(y, dir), _rotate(x, dir); } } push_up(x); if(goal == 0) root = x;}//void splay(int x, int goal)//{// push_down(x);// while(pre[x] != goal) _rotate(x, son[pre[x]][0] == x);// push_up(x);// if(goal == 0) root = x;//}int get_prec(int x){ push_down(x); int t = son[x][0]; push_down(t); while(son[t][1]) t = son[t][1], push_down(t); return t;}int get_subs(int x){ push_down(x); int t = son[x][1]; push_down(t); while(son[t][0]) t = son[t][0], push_down(t); return t;}int _find(int v){ int x = root; push_down(x); while(x && key[x] != v) x = son[x][key[x]<v], push_down(x); return x;}void _delete(int v){ int x = _find(v); if(!x) return; splay(x, 0); if(!son[root][0] || !son[root][1]) root = son[root][0] + son[root][1], pre[root] = 0; else { int t = get_subs(root); splay(t, root); son[son[root][1]][0] = son[root][0], root = son[root][1]; pre[son[root][0]] = root, pre[root] = 0; push_up(root); }}int get_kth(int k){ int x = root; push_down(x); while(siz[son[x][0]] + 1 != k) { if(siz[son[x][0]] + 1 > k) x = son[x][0]; else k -= (siz[son[x][0]] + 1), x = son[x][1]; push_down(x); } return x;}bool _insert(int v){ if(!root) new_node(root, 0, v); else { int x = root; while(son[x][key[x]<v]) { if(key[x] == v) { splay(x, 0); return false; } x = son[x][key[x]<v]; } new_node(son[x][key[x]<v], x, v); splay(son[x][key[x]<v], 0); } return true;}void build(int &x, int l, int r, int fa){ if(l > r) return; int mid = (l + r) >> 1; new_node(x, fa, arr[mid]); build(son[x][0], l, mid-1, x); build(son[x][1], mid+1, r, x); push_up(x);}ll query(int l, int r){ splay(get_kth(l), 0); splay(get_kth(r+2), root); return val[key_val];}void update(int l, int r, int v){ splay(get_kth(l), 0); splay(get_kth(r+2), root); lazy[key_val] += v; val[key_val] += v * siz[key_val];}void init(){ root = num = 0; son[root][0] = son[root][1] = siz[root] = pre[root] = 0; key[root] = val[root] = lazy[root] = 0; new_node(root, 0, 0); new_node(son[root][1], root, 0); build(key_val, 1, n, son[root][1]); push_up(son[root][1]), push_up(root);}int main(){ while(~ scanf("%d%d", &n, &m)) { for(int i = 1; i <= n; i++) scanf("%d", &arr[i]); init(); char ch; int a, b, c; for(int i = 1; i <= m; i++) { scanf(" %c%d%d", &ch, &a, &b); if(ch == 'Q') printf("%lld\n", query(a, b)); else scanf("%d", &c), update(a, b, c); } } return 0;}
0 0
- POJ 3468 A Simple Problem with Integers 伸展树splay 区间更新
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)
- Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers (Splay 区间更新、区间求和)
- A Simple Problem with Integers+poj+splay树区间操作
- POJ 3468 A Simple Problem with Integers (splay 成段更新、区间求和)
- poj 3468 A Simple Problem with Integers(伸展树)
- POJ A Simple Problem with Integers (Splay 伸展树 入门)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- A Simple Problem with Integers +poj+线段树区间更新
- poj 3468 A Simple Problem with Integers (Splay树)
- POJ 3468 A Simple Problem with Integers (Splay树练习)
- poj-3468-A Simple Problem with Integers-splay树
- [POJ 3468] A Simple Problem with Integers Splay树
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- ubuntu16.04怎么设置宽带连接
- Verilog 初级入门概念讲解(wire 和 reg 类型的区别, always 和 assign 的区别,“阻塞”赋值 和 “非阻塞赋值”的区别 )
- python笔记1
- poj-2942-Knights of the Round Tabler
- CSS设置页面背景色
- POJ 3468 A Simple Problem with Integers 伸展树splay 区间更新
- AADL中component type与component implementation的区别
- linux系统编程之基础必备(七):read/write函数与(非)阻塞I/O的概念
- 回归学习算法---偏最小二乘回归、PCA降维与理论
- 简单的ajax批删
- 猎聘网2017年校园招聘研发类岗位笔试题
- d3.js学习9----地图的制作2
- 总结
- Python 用for循环实现猜数字游戏