悬调求关
查看原帖
悬调求关
939431
szh_AK_alldevotion楼主2025/7/25 09:28
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;

struct tree {
	int val, sum, qmi, qma, hmi, hma, sz, key, t1, t2, t3, l, r;
} t[N];
int cnt, rt;

void add(int z) {
	t[++cnt].val = z;
	t[cnt].sum = z;
	if (z == 1)
		t[cnt].qmi = t[cnt].hmi = 0, t[cnt].qma = t[cnt].hma = 1;
	else
		t[cnt].qma = t[cnt].hma = 0, t[cnt].qmi = t[cnt].hmi = -1;
	t[cnt].sz = 1;
	t[cnt].key = rand();
}

void pushup(int d) {
	int l = t[d].l, r = t[d].r;
	t[d].sum = t[l].sum + t[r].sum + t[d].val;
	t[d].sz = t[l].sz + t[r].sz + 1;
	t[d].qma = max(t[l].qma, t[l].sum + t[r].qma + t[d].val);
	t[d].hma = max(t[r].hma, t[r].sum + t[l].hma + t[d].val);
	t[d].qmi = min(t[l].qmi, t[l].sum + t[r].qmi + t[d].val);
	t[d].hmi = min(t[r].hmi, t[r].sum + t[l].hmi + t[d].val);
}

void push1(int d) {
	t[d].val *= -1, t[d].sum *= -1;
	int yqmi = t[d].qmi, yqma = t[d].qma;
	int yhmi = t[d].hmi, yhma = t[d].hma;
	t[d].qmi = -yqma, t[d].qma = -yqmi;
	t[d].hmi = -yhma, t[d].hma = -yhmi;
	t[d].t1 ^= 1;
	t[d].t2 *= -1;
}

void push2(int d, int z) {
	t[d].val = z;
	t[d].sum = t[d].sz * z;
	if (z == 1)
		t[d].qmi = t[d].hmi = 0, t[d].qma = t[d].hma = t[d].sum;
	else
		t[d].qma = t[d].hma = 0, t[d].qmi = t[d].hmi = t[d].sum;
	t[d].t2 = z;
}

void push3(int d) {
	swap(t[d].l, t[d].r), swap(t[d].qma, t[d].hma);
	swap(t[d].qmi, t[d].hmi);
	t[d].t3 ^= 1;
}

void pushdown(int d) {
	if (t[d].t1) {
		if (t[d].l)
			push1(t[d].l);
		if (t[d].r)
			push1(t[d].r);
		t[d].t1 = 0;
	}
	if (t[d].t2) {
		if (t[d].l)
			push2(t[d].l, t[d].t2);
		if (t[d].r)
			push2(t[d].r, t[d].t2);
		t[d].t2 = 0;
	}
	if (t[d].t3) {
		if (t[d].l)
			push3(t[d].l);
		if (t[d].r)
			push3(t[d].r);
		t[d].t3 = 0;
	}
}

int he(int x, int y) {
	if (!x)
		return y;
	if (!y)
		return x;
	pushdown(x);
	pushdown(y);
	pushup(x);
	pushup(y);
	if (t[x].key < t[y].key) {
		t[x].r = he(t[x].r, y);
		pushup(x);
		return x;
	} else {
		t[y].l = he(x, t[x].l);
		pushup(y);
		return y;
	}
}

void split(int d, int x, int &l, int &r) {
	if (!d) {
		l = r = 0;
		return;
	}
	pushdown(d);
	if (t[t[d].l].sz + 1 <= x) {
		l = d;
		split(t[d].r, x - (t[t[d].l].sz + 1), t[d].r, r);
	} else {
		r = d;
		split(t[d].l, x, l, t[d].l);
	}
	pushup(d);
}

void Replace(int x, int y, int z) {
	int l, mid, r;
	split(rt, x - 1, l, mid);
	split(mid, y - x + 1, mid, r);
	push2(mid, z);
	rt = he(he(l, mid), r);
}

void Swap(int x, int y) {
	int l, mid, r;
	split(rt, x - 1, l, mid);
	split(mid, y - x + 1, mid, r);
	push3(mid);
	rt = he(he(l, mid), r);
}

void Invert(int x, int y) {
	int l, mid, r;
	split(rt, x - 1, l, mid);
	split(mid, y - x + 1, mid, r);
	push1(mid);
	rt = he(he(l, mid), r);
}

int ask(int x, int y) {
	int l, mid, r;
	split(rt, x - 1, l, mid);
	split(mid, y - x + 1, mid, r);
	int ans = (t[mid].qma + 1) / 2;
	ans += (abs(t[mid].hmi) + 1) / 2;
	rt = he(he(l, mid), r);
	return ans;
}

int main() {
	int n, q;
	cin >> n >> q;
	string s;
	cin >> s;
	for (int i = 0; i < (int)s.size(); i++) {
		int z = 1;
		if (s[i] == '(')
			z = -1;
		add(z);
		rt = he(rt, cnt);
	}
	while (q--) {
		string op;
		int l, r;
		cin >> op >> l >> r;
		char c;
		if (op[0] == 'R') {
			c = getchar();
			while (c != '(' && c != ')')
				c = getchar();
			int z = 1;
			if (c == '(')
				z = -1;
			Replace(l, r, z);
		} else if (op[0] == 'S')
			Swap(l, r);
		else if (op[0] == 'I')
			Invert(l, r);
		else
			cout << ask(l, r) << "\n";
	}
}

全部MLE,样例能过,不知道为什么

2025/7/25 09:28
加载中...