题解:CF1838D Bracket Walk
题目思路
使用数据结构维护,本人建议使用 set。
如果
同理,如果
每一次求的时候,对于修改,如果当前询问的数
随后判断数据结构的元素个数、最小值与最大值,看是否满足元素个数为
AC Code
#include <set>
#include <cstring>
#include <iostream>
#include <algorithm>
#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f
#define lnf 0x3f3f3f3f3f3f3f3f
const bool Debug = false;
//#define Debug true
using namespace std;
const int N = 2e5;
void init ();
int n, m;
string s;
set <int> _St;
int x;
signed main () {
init ();
cin >> n >> m;
cin >> s;
s = " " + s;
if (Debug) {
cout << s << endl;
}
for (register int i = 1; i <= n; ++ i) {
if (i % 2) {
if (s[i] == ')') {
_St.insert (i);
}
} else {
if (s[i] == '(') {
_St.insert (i);
}
}
}
if (Debug) {
for (auto it : _St) {
cout << it << ' ';
}
cout << endl;
}
while (m --) {
cin >> x;
if (_St.count (x)) {
_St.erase (x);
} else {
_St.insert (x);
}
if (n % 2) {
cout << "No" << endl;
} else {
if (_St.size () && ((*_St.begin () % 2) || (!(*_St.rbegin () % 2)))) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
}
}
return 0;
}
void init () {
ios::sync_with_stdio (Debug);
cin.tie (0);
cout.tie (0);
}