题目链接
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;
}