제출 #1245353

#제출 시각아이디문제언어결과실행 시간메모리
1245353huydayyAlternating Heights (CCO22_day1problem1)C++20
4 / 25
105 ms4588 KiB
#include <bits/stdc++.h>
#define TASK "E"
#define int long long
#define pb push_back
#define FOR(i,a,b) for (int i = a, _b = b; i <= _b; ++i)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;

const int MAXN = 3005;
int N, K, Q;
int a[MAXN], ok[MAXN];
vector<int> G[MAXN];
bool vis[MAXN], in_stack[MAXN];

bool dfs(int u) {
    vis[u] = in_stack[u] = 1;
    for (int v : G[u]) {
        if (!vis[v]) {
            if (dfs(v)) return true;
        } else if (in_stack[v]) return true;
    }
    in_stack[u] = 0;
    return false;
}

bool has_cycle() {
    fill(vis + 1, vis + K + 1, 0);
    fill(in_stack + 1, in_stack + K + 1, 0);
    FOR(u, 1, K) if (!vis[u]) if (dfs(u)) return true;
    return false;
}

void preprocess() {
    int r = 1;
    FOR(l, 1, N) {
        for (int i = 1; i <= K; ++i) G[i].clear();
        r = max(r, l);
        int t = 1;
        while (r < N) {
            int u = a[r], v = a[r + 1];
            if (u == v) break;
            if (t & 1) G[v].pb(u);
            else G[u].pb(v);
            if (has_cycle()) {
                if (t & 1) G[v].pop_back();
                else G[u].pop_back();
                break;
            }
            ++r;
            t ^= 1;
        }
        ok[l] = r;
    }
}

int32_t main() {
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    FAST;
    cin >> N >> K >> Q;
    FOR(i, 1, N) cin >> a[i];

    preprocess(); // two-pointer với kiểm tra chu trình

    while (Q--) {
        int x, y; cin >> x >> y;
        cout << (ok[x] >= y ? "YES" : "NO") << '\n';
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'int32_t main()':
Main.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...