Submission #886425

#TimeUsernameProblemLanguageResultExecution timeMemory
886425tsumondaiAlternating Heights (CCO22_day1problem1)C++14
25 / 25
268 ms14148 KiB
#include <bits/stdc++.h>
using namespace std;

#define Task "CCO22_day1problem1"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME  (1.0 * clock() / CLOCKS_PER_SEC)

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 3e3 + 5;

const int oo = 1e9, mod = 1e9 + 7;

int n, m, k;
int f[3001], a[3001], val[3001], state[3001];
bool ok = 1, exist[3001];
vector<int> adj[N];

void dfs(int u) {
    state[u] = 1;
    for (int v : adj[u]) if (state[v] < 2) {
            if (state[v] == 1) {ok = 0; return;}
            else {
                dfs(v);
                if (ok == 0) return;
            }
        }
    state[u] = 2;
}

bool check(int l, int r) {
    for (int i = 1; i <= k; i++) {
        adj[i].clear();
        f[i] = 0; state[i] = 0; exist[i] = 0;
    }
    for (int i = l; i <= r; i++) {
        exist[a[i]] = 1;
        if (i % 2 == 1) {
            if (i - 1 >= l) {
                adj[a[i]].push_back(a[i - 1]);
                f[a[i - 1]]++;
            }
            if (i + 1 <= r) {
                adj[a[i]].push_back(a[i + 1]);
                f[a[i + 1]]++;
            }
        }
    }
    ok = 1;
    for (int i = 1; i <= k; i++) if (f[i] == 0) {
            dfs(i);
            if (ok == 0) return 0;
        }
    for (int i = 1; i <= k; i++) if (exist[i] == 1 && state[i] == 0) return 0;
    return ok;
}

void process() {
    int n, q;
    cin >> n >> k >> q;
    map<pair<int, int>, int> f;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int j = 1;
    for (int i = 1; i <= n; i++) {
        while (j <= n && check(i, j) == 1) j++;
        val[i] = j;
    }
    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        if (val[l] <= r) cout << "NO" << '\n';
        else cout << "YES" << '\n';
    }
    return;
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    if (fopen(Task".INP", "r")) {
        freopen(Task".INP", "r", stdin);
        freopen(Task".OUT", "w", stdout);
    }
    process();
    cerr << "Time elapsed: " << __TIME << " s.\n";
    return 0;
}

// dont stop

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(Task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         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...