题解:CF1838D Bracket Walk

· · 题解

题目思路

使用数据结构维护,本人建议使用 set。

如果 x 是奇数且 s_x=),就将 x 插入数据结构。

同理,如果 x 是偶数且 s_x=(,就将 x 插入数据结构。

每一次求的时候,对于修改,如果当前询问的数 Qry 在数据结构里,就将其移除,否则插入。

随后判断数据结构的元素个数、最小值与最大值,看是否满足元素个数为 0,或最大值为奇数且最小值为偶数,如果是,说明可行。否则不可行。

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);
}