Codeforces722C-Destroying Array(线段树 or 并查集)
来源:互联网 发布:网络集成实施方案 编辑:程序博客网 时间:2024/06/05 00:56
题目链接
http://codeforces.com/contest/722/problem/C
思路
1. 线段树
其实就是线段树的区间并操作,对于每个节点o,需要维护4个值,sumv[o], suml[o], sumr[o], f[o](分别代表当前区间的最大子区间和,当前区间的左端点到断点的区间和,当前区间的右端点到断点的区间和,以及f代表当前区间是否被分割过)
对于合并操作,见下图:
关键就是pushup()函数
2.并查集
并查集的需要倒跑,先将全部点设置为destroy,然后从最后一个操作开始加点,维护一个最大值
感觉还是线段树的思路更好理解呐= =
代码
- 线段树
#include <iostream>#include <cstring>#include <stack>#include <vector>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)const int maxn = 100000 + 5;LL sumv[maxn << 2], suml[maxn << 2], sumr[maxn << 2];bool f[maxn << 2];int pos, n;LL max3(LL x, LL y, LL z) { return max(x, max(y, z));}void pushup(int o) { f[o] = f[o << 1] & f[o << 1 | 1]; sumv[o] = max3(suml[o << 1], sumr[o << 1 | 1], sumr[o << 1] + suml[o << 1 | 1]); sumv[o] = max3(sumv[o], sumv[o << 1], sumv[o << 1 | 1]); if (f[o << 1]) suml[o] = suml[o << 1] + suml[o << 1 | 1]; else suml[o] = suml[o << 1]; if (f[o << 1 | 1]) sumr[o] = sumr[o << 1 | 1] + sumr[o << 1]; else sumr[o] = sumr[o << 1 | 1];}void build(int o, int L, int R) { if (L == R) { scanf("%I64d", &sumv[o]); suml[o] = sumr[o] = sumv[o]; f[o] = true; return; } int M = (L + R) >> 1; build(o << 1, L, M); build(o << 1 | 1, M + 1, R); pushup(o);}void set(int o, int L, int R) { if (L == R) { sumv[o] = suml[o] = sumr[o] = 0; f[o] = false; return; } int M = (L + R) >> 1; if (pos <= M) set(o << 1, L, M); else set(o << 1 | 1, M + 1, R); pushup(o);}int main() { scan(n); build(1, 1, n); for (int i = 0; i < n; i++) { scan(pos); set(1, 1, n); printf("%I64d\n", max3(sumv[1], suml[1], sumr[1])); } return 0;}
- 并查集
#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)const int maxn = 100000 + 5;LL ans[maxn], a[maxn], sumv[maxn];int p[maxn], n, pos[maxn], vis[maxn];int find(int x) { return p[x] == x ? x : p[x] = find(p[x]);}void merge(int x, int y) { x = find(x), y = find(y); if (x != y) { if (sumv[x] > sumv[y]) { sumv[x] += sumv[y]; p[y] = x; } else { sumv[y] += sumv[x]; p[x] = y; } }}int main() { scan(n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (int i = 1; i <= n; i++) scan(pos[i]); for (int i = 1; i <= n; i++) p[i] = i; for (int i = n; i >= 2; i--) { int pn = pos[i]; vis[pn] = true; ans[i] = sumv[pn] = a[pn]; if (vis[pn - 1]) { ans[i] += sumv[find(pn - 1)]; merge(pn, pn - 1); } if (vis[pn + 1]) { ans[i] += sumv[find(pn + 1)]; merge(pn, pn + 1); } ans[i] = max(ans[i], ans[i + 1]); } for (int i = 2; i <= n; i++) printf("%I64d\n", ans[i]); printf("0\n"); return 0;}
0 0
- Codeforces722C-Destroying Array(线段树 or 并查集)
- CodeForces722C Destroying Array【瞎搞】
- Destroying Array(并查集)
- Codeforces722 C. Destroying Array (并查集)
- 并查集(Destroying Array,cf 722C)
- codeforces 722C. Destroying Array(并查集||set)
- 并查集巧妙用法(codeforces)C. Destroying Array
- CodeForces 722C Destroying Array(并查集和我奇怪的二叉树)
- codeforces 722C Destroying Array ( 并查集 / Multiset )
- [codeforces]C. Destroying Array——并查集
- 【Codeforces 722 C Destroying Array】+ 并查集
- Codeforces 722C Destroying Array(并查集)
- Destroying Array(并查集 高能坑点)
- poj1456 并查集or线段树
- 并查集——Destroying Array(Intel Code Challenge Elimination Round C)
- Codeforces 722C Destroying Array【逆序(离线)思维+并查集】
- CodeForces 722C. Destroying Array(并查集 好题)
- 722C - Destroying Array 线段树
- 美团2017笔试题 给定一颗多叉树
- P1315 观光公交
- clusterinit初始化环境脚本
- ccpc东北四——重现赛
- 161006
- Codeforces722C-Destroying Array(线段树 or 并查集)
- 判断一窜字符串当中同一个单词出现的频率次数
- JavaScript对象和数组
- 汇编预备知识(五)
- JAVA 中的 StringBuilder 和 StringBuffer 适用的场景是什么?
- aar文件在android studio中的使用
- 车牌识别步骤及部分代码
- if与else同时运行
- Android qq 登录 界面 圆头像