$ubsequence$ Day7 T2

版权声明:本文为博主原创文章,未经博主允许必须转载。 https://blog.csdn.net/qq_35950004/article/details/86362263

在这里插入图片描述 n < = 1 0 5 n<=10^5
在这里插入图片描述
先有结论:长度为k+1的最大序列包含长度为k的最大序列。
然后就可以。。。。

标程:

#include <bits/stdc++.h>

#define For(i, j, k) for (int i = j; i <= k; i++)

using namespace std;

const int N = 1e5 + 10;

int n;
int A[N];

typedef long long LL;

struct Splay {

	int rt;
	int fa[N], ch[N][2], sz[N];
	LL val[N], addv[N];

	void pushup(int x) {
		sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
	}

	void update(int x, LL w) {
		if (x) val[x] += w, addv[x] += w;
	}

	void pushdown(int x) {
		if (addv[x]) {
			LL &w = addv[x];
			if (ch[x][0]) update(ch[x][0], w);
			if (ch[x][1]) update(ch[x][1], w);
			w = 0;
		}
	}

	void rotate(int x) {
		int f = fa[x], g = fa[f], c = ch[f][1] == x;
		if (g) ch[g][ch[g][1] == f] = x; else rt = x; fa[x] = g;
		fa[ch[x][c ^ 1]] = f, ch[f][c] = ch[x][c ^ 1];
		ch[x][c ^ 1] = f, fa[f] = x;
		pushup(f);
	}

	void push(int x) {
		if (fa[x]) push(fa[x]);
		pushdown(x);
	}

	void splay(int x, int aim = 0) {
		push(x);
		while (fa[x] != aim) {
			int f = fa[x];
			if (fa[f] != aim && ((ch[fa[f]][1] == f) == (ch[f][1] == x))) rotate(f);
			rotate(x);
		}
		pushup(x);
	}

	int node;

	int find(int &x, int f, int cur, int key) {	
		if (!x) {
			x = ++node;
			val[x] = 1ll * key * (cur + 1);
			sz[x] = 1, fa[x] = f;
			if (f) ++sz[f];
			splay(x);
			return x;
		}
		int j = cur + sz[ch[x][0]] + 1;
		pushdown(x);
		if (val[x] >= 1ll * j * key) return find(ch[x][1], x, j, key);
		else return find(ch[x][0], x, cur, key);
	}

	void getsum(int x, LL &cur) {
		pushdown(x);
		if (ch[x][0]) getsum(ch[x][0], cur);
		cur += val[x];
		printf("%lld ", cur);
		if (ch[x][1]) getsum(ch[x][1], cur);
	}

}T;

int main() {

	freopen("subsequence.in", "r", stdin);
	freopen("subsequence.out", "w", stdout);

	scanf("%d", &n);
	For(i, 1, n) scanf("%d", &A[i]);
	For(i, 1, n) {
		T.find(T.rt, 0, 0, A[i]);
		T.update(T.ch[T.rt][1], A[i]);
	}
	LL cur = 0;
	T.getsum(T.rt, cur);
	puts("");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35950004/article/details/86362263
今日推荐