题解:CF1838D Bracket Walk

涉及知识点:字符串,set

解题思路

首先,如果序列的长度为奇数,是一定不能做到的,所以 \(q\) 个询问直接输出 NO

如果 \(i\) 为奇数且 \(s_i\)\()\),则将 \(i\) 插入数据结构中。

同理,如果 \(i\) 为偶数且 \(s_i\)\((\),则将 \(i\) 也插入数据结构中。

对于每一次询问操作,令输入的数为 \(idx\),如果 \(idx\) 在数据结构里,就把 \(idx\) 删除,否则把 \(idx\) 插入数据结构。

然后判断如果数据结构的长度不等于 \(0\) 且最小值为奇数或最大值为偶数,则输出 NO,否则输出 YES

由于需要查询元素是否存在,最大值,最小值,插入以及删除,所以可以使用 set 来维护。

代码

#include <bits/stdc++.h>
#define int long long
#define ll __int128
#define db double
#define ldb long double
#define vo void
#define endl '\n'
#define il inline
#define re register
#define ve vector
#define p_q priority_queue
#define PII pair<int, int>
#define u_m unordered_map
#define bt bitset

using namespace std;

//#define O2 1
#ifdef O2
	#pragma GCC optimize(1)
	#pragma GCC optimize(2)
	#pragma GCC optimize(3, "Ofast", "inline")
#endif

struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
	char buf[MAXSIZE], *p1, *p2;
	char pbuf[MAXSIZE], *pp;
	IO() : p1(buf), p2(buf), pp(pbuf) {}

	~IO() {
		fwrite(pbuf, 1, pp - pbuf, stdout);
	}
	char gc() {
		if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
		return p1 == p2 ? ' ' : *p1++;
	}

	bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}

	template <class T>
	void read(T &x) {
		double tmp = 1;
		bool sign = 0;
		x = 0;
		char ch = gc();
		while (!isdigit(ch)) {
			if (ch == '-') sign = 1;
			ch = gc();
		}
		while (isdigit(ch)) {
			x = x * 10 + (ch - '0');
			ch = gc();
		}
		if (ch == '.') {
			ch = gc();
			while (isdigit(ch)) {
				tmp /= 10.0, x += tmp * (ch - '0');
				ch = gc();
			}
		}
		if (sign) x = -x;
	}

	void read(char *s) {
		char ch = gc();
		for (; blank(ch); ch = gc());
		for (; !blank(ch); ch = gc()) * s++ = ch;
		*s = 0;
	}

	void read(char &c) {
		for (c = gc(); blank(c); c = gc());
	}

	void push(const char &c) {
		if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
		*pp++ = c;
	}

	template <class T>
	void write(T x) {
		if (x < 0) x = -x, push('-');
		static T sta[35];
		T top = 0;
		do {
			sta[top++] = x % 10, x /= 10;
		} while (x);
		while (top) push(sta[--top] + '0');
	}

	template <class T>
	void write(T x, char lastChar) {
		write(x), push(lastChar);
	}
} io;

namespace Template {
	int fact[200000];
	int Triangle[1010][1010];
	int Prime[2000000], Prime_vis[2000000];
	int Prime_len;
	void Fact(int n, int mod) {
		fact[0] = 1;
		for (int i = 1; i <= n; i ++ ) fact[i] = ((fact[i - 1]) % mod * (i % mod)) % mod;
	}
	void Pascal_s_triangle(int n, int mod) {
		for (int i = 0; i <= n; i ++ ) Triangle[i][0] = 1;
		for (int i = 1; i <= n; i ++ )
			for (int j = 1; j <= i; j ++ )
				Triangle[i][j] = (Triangle[i - 1][j] + Triangle[i - 1][j - 1]) % mod;
	}
	void Get_prime(int n) {
		for (int i = 2; i <= n; i ++ ) {
			if (!Prime_vis[i]) Prime[++Prime_len] = i;
			for (int j = 1; Prime[j] * i <= n; j ++ ) {
				Prime_vis[Prime[j] * i] = 1;
				if (i % Prime[j] == 0) break;
			}
		}
	}
	int pw(int x, int y, int mod) {
		int res = 1;
		while (y) {
			if (y & 1) res = ((res % mod) * (x % mod)) % mod;
			x = (x % mod) * (x % mod) % mod;
			y >>= 1;
		}
		return res;
	}
	int pw(int x, int y) {
		int res = 1;
		while (y) {
			if (y & 1) res *= x;
			x *= x;
			y >>= 1;
		}
		return res;
	}
	int GCD(int x, int y, int mod) {
		return __gcd(x, y) % mod;
	}
	int LCM(int x, int y, int mod) {
		return (((x % mod) * (y % mod)) % mod / (GCD(x, y, mod) % mod)) % mod;
	}
	int C(int n, int m, int mod) {
		if (m > n || m < 0) return 0;
		return fact[n] * pw(fact[m], mod - 2, mod) % mod * pw(fact[n - m], mod - 2, mod) % mod;
	}
	int Ask_triangle(int x, int y) {
		return Triangle[x][y];
	}
}
using namespace Template;

//#define fre 1
#define IOS 1
//#define multitest 1

const int N = 4e6 + 10;
const int M = 4e5 + 10;
const int inf = 1e12;
const int Mod = 1e9 + 9;

namespace zla {
	int n, q;
	string str;
	set<int> s;

	il void Init() {
		cin >> n >> q;
		cin >> str;
	}

	il void Solve() {
		if (n % 2) {
			while (q -- ) cout << "NO\n";
			return ;
		}
		str = " " + str;
		for (int i = 1; i <= n; i ++ ) {
			if (i % 2 && str[i] == ')') s.insert(i);
			else if ((!(i % 2)) && str[i] == '(') s.insert(i);
		}
		while (q -- ) {
			int idx;
			cin >> idx;
			if (s.count(idx)) s.erase(idx);
			else s.insert(idx);
			if (!s.empty() && ((*s.begin() % 2) || (!(*s.rbegin() % 2)))) cout << "NO\n";
			else cout << "YES\n";
		}
	}

	il void main() {
		Init();
		Solve();
	}
}

signed main() {
	int T;
#ifdef IOS
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
#ifdef fre
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
#ifdef multitest
	cin >> T;
#else
	T = 1;
#endif
	while (T--) zla::main();
	return 0;
}
posted @ 2024-10-25 14:41  zla_2012  阅读(2)  评论(0编辑  收藏  举报