제출 #1347955

#제출 시각아이디문제언어결과실행 시간메모리
1347955kantaponzAlternating Heights (CCO22_day1problem1)C++20
25 / 25
376 ms13272 KiB
#include <bits/stdc++.h>
using namespace std;

const int nx = 3001;

bool dp[nx][nx]; // 1 = valid
int n, k, q, a[nx];
vector<int> adj[nx];
bool vis[nx];
bool stk[nx];

int dfs(int u) { // detect false
    vis[u] = 1;
    stk[u] = 1;
    //cout << u << " ";
    int ans = 1;
    for (auto v : adj[u]) {
        if (vis[v]) {
            if (stk[v]){
                //cout << v << "\n";
                return 0;
            } 
            continue;
        }
        ans &= dfs(v);
    }
    stk[u] = 0;
    return ans;
}

bool toposort(int x, int y) {
    for (int i = x; i <= y; i++) adj[a[i]].clear();
    memset(vis, 0, sizeof vis);
    memset(stk, 0, sizeof stk);
    for (int i = x; i + 1 <= y; i++) {
        if (i % 2) adj[a[i+1]].emplace_back(a[i]);
        else adj[a[i]].emplace_back(a[i+1]);
    }
    for (int i = x; i <= y; i++) {
        if (vis[a[i]]) continue;
        if (!dfs(a[i])) return false;
        //cout << endl;
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n >> k >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int x = 1; x <= n; x++) {
        int l = x, r = n;
        while (l < r) {
            int mid = ceil(1.0 * (l + r) / 2.0);
            //cout << "toposort at " << x << " " << mid << "\n";
            if (toposort(x, mid)) l = mid;
            else r = mid - 1;
        }
        for (int i = x; i <= l; i++) {
            dp[x][i] = 1;
        }
    }

    while (q--) {
        int x, y;
        cin >> x >> y;
        if (dp[x][y]) cout << "YES\n";
        else cout << "NO\n";
    }
}

/*
6 3 3
1 1 2 3 1 2
1 2
2 5
2 6


6 3 1
1 1 2 3 1 2
2 5


*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...