首页 > 其他分享 >Educational Codeforces Round 102 (Rated for Div. 2)D(线段树求贡献)

Educational Codeforces Round 102 (Rated for Div. 2)D(线段树求贡献)

时间:2023-02-16 21:56:04浏览次数:58  
标签:lazy Rated min int mid Educational Codeforces max query

Educational Codeforces Round 102 (Rated for Div. 2)D(线段树求贡献)

D.Program

题目大意:

最初x为0,给定一个长度为n的操作序列,共有两种操作:

  1. -,x-=1;
  2. +, x+=1;

有m次询问,每次考虑如果忽略[l,r] 的操作,所能出现的不同的数的个数。


解题思路:

我们先不考虑询问,对于每一位我们考虑它经过变化后的值为多少。经过几次模拟我们发现对于第i位它的值为2*pre[i]-len,其中pre[i]表示前i位的操作2前缀和,我们考虑在一个区间内所能出现的不同的值可以表示为(max-min)+1,所以我们的问题变成了,如果取掉区间[l,r],求max([1,l-1],[r+1,n])和min([1,l-1],[r+1,n]),求区间max和区间min,我们可以用线段树来维护

考虑询问时,我们每次先消除区间[l,r]的影响,对区间[r+1,n]减去2*(pre[r]-pre[l-1])-(r-l+1) 也就是[l,r]这一段的值,然后求区间max和min,求得答案后再恢复区间[l,r]


代码实现

#include<iostream>
# include<bits/stdc++.h>
using namespace std;
# define int long long
# define endl "\n"
const int N = 3e5 + 10, inf = 0x3f3f3f3f, mod = 998244353;
const int esp = 1e-6;
int prez[N];
# define ls u<<1
# define rs u<<1|1
struct segtree {
	int sum[4 * N], lazy[4 * N];
	int maxn[4 * N], minn[4 * N];
	void ini() {
		memset(lazy, 0, sizeof lazy);
		memset(sum, 0, sizeof lazy);
		memset(maxn, 0, sizeof maxn);
	}
	void pushup(int u) {
		sum[u] = (sum[ls] + sum[rs]);
		maxn[u] = max(maxn[ls], maxn[rs]);
		minn[u] = min(minn[ls], minn[rs]);
	}

	void build(int u, int l, int r) {
		lazy[u] = 0;
		if (l == r) {
			sum[u] = maxn[u] = minn[u] = 2 * prez[l] - l;
			return;
		}

		int mid = l + r >> 1;
		build(ls, l, mid);
		build(rs, mid + 1, r);
		pushup(u);
	}

	void pushdown(int u, int l, int r) {
		int mid = l + r >> 1;
		if (lazy[u]) {
			sum[ls] += (mid - l + 1) * lazy[u];
			sum[rs] += (r - mid) * lazy[u];

			maxn[ls] += lazy[u];
			maxn[rs] += lazy[u];

			minn[ls] += lazy[u];
			minn[rs] += lazy[u];

			lazy[ls] += lazy[u];
			lazy[rs] += lazy[u];
		}
		lazy[u] = 0;
	}

	void modify(int u, int l, int r, int L, int R, int c) {
		if (l > R || r < L) return;
		if (L <= l && r <= R) {
			sum[u] += (r - l + 1) * c;
			lazy[u] += c;
			maxn[u] += c;
			minn[u] += c;

			return;
		}
		int mid = l + r >> 1;
		pushdown(u, l, r);
		if (L <= mid) modify(ls, l, mid, L, R, c);
		if (mid + 1 <= R) modify(rs, mid + 1, r, L, R, c);
		pushup(u);
	}

	int query_max(int u, int l, int r, int L, int R) {
		if (L > r || R < l) return 0;
		if (l >= L && r <= R) {
			return maxn[u];
		}
		pushdown(u, l, r);
		int mid = l + r >> 1;
		if (R <= mid) return query_max(ls, l, mid, L, R);
		else if (L > mid) return query_max(rs, mid + 1, r, L, R);
		else return max(query_max(ls, l, mid, L, R), query_max(rs, mid + 1, r, L, R));
	}

	int query_min(int u, int l, int r, int L, int R) {
		if (L > r || R < l) return 1e9;
		if (l >= L && r <= R) {
			return minn[u];
		}
		pushdown(u, l, r);
		int mid = l + r >> 1;
		if (R <= mid) return query_min(ls, l, mid, L, R);
		else if (L > mid) return query_min(rs, mid + 1, r, L, R);
		else return min(query_min(ls, l, mid, L, R), query_min(rs, mid + 1, r, L, R));
	}

} tr;
void solve() {
	int n, q;
	cin >> n >> q;

	for (int i = 1; i <= n; ++i) {
		char c;
		cin >> c;
		prez[i] = prez[i - 1] + (c == '+');
	}
	tr.build(1, 1, n);
	while (q--) {
		int l, r;
		cin >> l >> r;
		int cnt = prez[r] - prez[l - 1];
		int len = r - l + 1;
		int now = 2 * cnt - len;
		tr.modify(1, 1, n, r + 1, n, -now);
		int maxn = max({0ll,tr.query_max(1, 1, n, 1, l - 1), tr.query_max(1, 1, n, r + 1, n)});
		int minn = min({0ll,tr.query_min(1, 1, n, 1, l - 1), tr.query_min(1, 1, n, r + 1, n)});
		cout << maxn - minn + 1 << endl;
		tr.modify(1,1,n,r+1,n,now);
	}
}

int tt;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	tt = 1;
	cin >> tt;
	while (tt--) solve();


	return 0;
}

标签:lazy,Rated,min,int,mid,Educational,Codeforces,max,query
From: https://www.cnblogs.com/empty-y/p/17128451.html

相关文章