2018 wannafly-day2-Princess Principal

题目链接


题意:
给出一串括号串,然后多次询问某个 [ l , r ] 区间内是否为何发的括号串


题解:使用栈维护出每个字符对应匹配的哪一个括号,然后两个循环处理出当前括号能够匹配到最远的括号是谁


AC代码:

#include <iostream>
#include <stdio.h>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stack>
#include <map>

using namespace  std;
typedef long long ll;
const int maxn = 2e6 + 10;
const ll inf = 0x3f3f3f3f;
typedef long double ld;
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
inline ll mul(ll x, ll y, ll mod) {ll res = (x * y - (ll)(ld)(x / mod * y + 1e-8) * mod); return res < 0 ? res + mod : res;}
const double PI = acos(-1.0);
const int mod = 1e9+7;
ll qPow(ll base, ll n) {ll res = 1; while(n) {if(n & 1) res = (res * base) % mod; base = (base * base) % mod; n >>= 1;} return res % mod;}

int n, m, q, a[maxn];
int pos[maxn];
pair<int, int> p[maxn];
int main() {
    while(~scanf("%d%d%d", &n, &m, &q)) {
        rep(i, 1, n) {
            scanf("%d", &a[i]);
            if(a[i] % 2 == 0) {
                a[i] /= 2;
            } else {
                a[i] /= 2;
                a[i] += m;
            }
            p[i] = mp(i, a[i]);
        }
        stack<pair<int, int> > s;

        met(pos, -1);
        while(!s.empty()) s.pop();
        rep(i, 1, n) {
            if(s.empty()) {s.push(p[i]); continue;}
            if(p[i].se >= m) {
                if(s.top().se != p[i].se - m) {
                    s.push(p[i]);
                } else {
                    pos[s.top().fi] = p[i].fi;
                    pos[p[i].fi] = s.top().fi;
                    s.pop();
                }
            } else {
                s.push(p[i]);
            }
        }
//        rep(i, 1, n) {
//            printf("%d ", pos[i]);
//        }
//        puts("");
        per(i, n, 1) {
            if(a[i] < m) {
                pos[i] = max(pos[i], pos[pos[i] + 1]);
            }
        }
        rep(i, 1, n) {
            if(a[i] >= m) {
                pos[i] = min(pos[i], pos[pos[i] - 1]);
            }
        }

        int l, r;
        while(q--) {
            scanf("%d%d", &l, &r);
            if(pos[l] >= r && pos[r] <= l) {
                puts("Yes");
            } else {
                puts("No");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38081836/article/details/81608522