Codeforces Round #162 E. Roadside Trees dp+线段树

48 篇文章 0 订阅
9 篇文章 0 订阅

Description
在 1∼n 的位置能种树,刚开始能种树。
第 i 个时刻会有操作:
1.在一个没种过树的位置 p_i 种一颗高度为 h_i 的树。
2.砍掉第 x_i 棵树,保证这个位置以后不会种树。
每天树会长高1,每执行一次操作,输出最长上升子序列长度。
任意时刻树的高度不同。


Sample Input
4 6
1 1 1
1 4 4
1 3 4
2 2
1 2 8
2 3


Sample Output
1
2
3
2
2
2


题目中给出了一些有用的性质,如:
每次加入的高度小于等于10,每次删除的位置小于等于10,每个时刻不会有高度相同的两棵树。
那你就考虑,对于插入操作,高度比他小的不超过10个,那你就开一棵维护位置的线段树,暴力修改他们的信息。
对于删除操作,位置比他小的不超过10个,那你就开一棵维护权值的线段树,暴力修改他们的信息。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
const int maxn = 200000 + 11;
int _max(int x, int y) {return x > y ? x : y;}
int _min(int x, int y) {return x < y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct node {
	int x, p;
} pp[20]; int plen;
struct tnode {
	int lc, rc, l, r, c[2];
} t[410000]; int cnt;
int pos[210000];
int a[210000];

bool cmp1(node a, node b) {return a.p < b.p;}
bool cmp2(node a, node b) {return a.x < b.x;}

int lowbit(int x) {return x & -x;}
void cpos(int x, int c) {for(int i = x; i <= maxn; i += lowbit(i)) pos[i] += c;}
int findkth(int k) {
	int now = 0, sum = 0;
	for(int i = 18; i >= 0; i--) {
		if((now + (1 << i)) < maxn && sum + pos[now + (1 << i)] < k) now += (1 << i), sum += pos[now];
	} return now + 1;
}

void bt(int l, int r) {
	int now = ++cnt;
	t[now].l = l, t[now].r = r;
	t[now].lc = t[now].rc = -1;
	if(l < r) {
		int mid = (l + r) / 2;
		t[now].lc = cnt + 1; bt(l, mid);
		t[now].rc = cnt + 1; bt(mid + 1, r);
	}
}

void change(int now, int p, int c, int opt) {
	if(t[now].l == t[now].r) {t[now].c[opt] = c; return ;}
	int mid = (t[now].l + t[now].r) / 2;
	if(p <= mid) change(t[now].lc, p, c, opt);
	else change(t[now].rc, p, c, opt);
	t[now].c[opt] = _max(t[t[now].lc].c[opt], t[t[now].rc].c[opt]);
}

int findmax(int now, int l, int r, int opt) {
	if(t[now].l == l && t[now].r == r) {return t[now].c[opt];}
	int mid = (t[now].l + t[now].r) / 2;
	if(r <= mid) return findmax(t[now].lc, l, r, opt);
	else if(l > mid) return findmax(t[now].rc, l, r, opt);
	else return _max(findmax(t[now].lc, l, mid, opt), findmax(t[now].rc, mid + 1, r, opt));
}

int main() {
	int n = read(), m = read();
	bt(1, maxn); plen = 0;
	for(int i = 1; i <= m; i++) {
		int opt = read(), p, h, now = 0;
		for(int j = 1; j <= plen; j++) {
			if(pp[j].x + i <= 10) pp[++now] = pp[j];
		} plen = now;
		if(opt == 1) {
			p = read(), h = read();
			a[p] = h - i + m;
			for(int j = 1; j <= plen; j++) if(pp[j].x + i < h){
				change(1, pp[j].p, 0, 0);
				change(1, pp[j].x + m, 0, 1);
			} int hh = findmax(1, p + 1, n + 1, 0) + 1;
			change(1, p, hh, 0), change(1, h + m - i, hh, 1);
			sort(pp + 1, pp + plen + 1, cmp2);
			for(int j = plen; j >= 1; j--) if(pp[j].x + i < h){
				hh = findmax(1, pp[j].p + 1, n + 1, 0) + 1;
				change(1, pp[j].p, hh, 0), change(1, pp[j].x + m, hh, 1);
			} pp[++plen].p = p, pp[plen].x = h - i;
			cpos(p, 1);
			printf("%d\n", findmax(1, 1, n + 1, 0));
		} else {
			int p = read();
			for(int j = 1; j < p; j++) {
				int u = findkth(j);
				change(1, u, 0, 0);
				change(1, a[u], 0, 1);
			} int q = findkth(p);
			cpos(q, -1);
			change(1, q, 0, 0);
			change(1, a[q], 0, 1); a[q] = 0;
			int now = 0;
			for(int j = 1; j <= plen; j++) if(pp[j].p != q) pp[++now] = pp[j];
			plen = now;
			for(int j = p - 1; j >= 1; j--) {
				int u = findkth(j);
				int hh = findmax(1, a[u] + 1, maxn, 1) + 1;
				change(1, u, hh, 0), change(1, a[u], hh, 1);
			} printf("%d\n", findmax(1, 1, n + 1, 0));
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值