Submission #1124843

#TimeUsernameProblemLanguageResultExecution timeMemory
1124843VinhLuuAlternating Heights (CCO22_day1problem1)C++20
25 / 25
899 ms28060 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define all(lpv) lpv.begin(), lpv.end()
#define fi first
#define se second
#define pot(x, y) lower_bound(x.begin(), x.end(), y) - x.begin()
using namespace std;

typedef pair<int,int> pii;

const int N = 1e6 + 5;
const int oo = 1e9;

int n, f[N], k, q, a[N];
vector<int> p[N];

int in[N];

bool ff = true;

void dfs(int u) {
  in[u] = 1;
  for(auto j : p[u]) {
    if(in[j] == 2) continue;
    if(!in[j]) {
      dfs(j);
    } else {
      ff = false;
      return;
    }
    if(!ff) return;
  }
  in[u] = 2;
}

signed main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")) {
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }
  cin >> n >> k >> q;
  for(int i = 1; i <= n; i ++) cin >> a[i];

  for(int i = 1; i <= n; i ++) {
    f[i] = i;
    int l = i + 1, r = n, mid;
    while(l <= r) {
      mid = (l + r) / 2;
      for(int j = 1; j <= k; j ++) {
        p[j].clear();
        in[j] = 0;
      }
      for(int j = i; j < mid; j ++) {
        if(j % 2 == i % 2) { // >
          p[a[j]].push_back(a[j + 1]);
        } else {
          p[a[j + 1]].push_back(a[j]);
        }
      }
      ff = true;
      for(int j = 1; j <= k; j ++) if(!in[j]) {
        dfs(j);
        if(!ff) break;
      }

      if(ff) f[i] = mid, l = mid + 1;
      else r = mid - 1;
    }
  }

  while(q--) {
    int l, r; cin >> l >> r;
    if(f[l] < r) cout << "NO\n";
    else cout << "YES\n";
  }
}

Compilation message (stderr)

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