Submission #891378

# Submission time Handle Problem Language Result Execution time Memory
891378 2023-12-22T20:27:11 Z stefanneagu RMQ (NOI17_rmq) C++17
23 / 100
1000 ms 348 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "unroll-loops", "O3")
#pragma GCC warning "deprecated"
#define int short

using namespace std;

const int nmax = 3000 + 1;

struct str {
  int a, b, val;
  const bool operator < (str ult) const {
    return val < ult.val;
  }
} v[nmax];

int aint[4 * nmax], lz[4 * nmax], rmq[nmax][20], used[nmax], viz[nmax], laba[nmax];

int rmquery(int a, int b) {
  int lg = (int) log2(b - a + 1);
  return min(rmq[a][lg], rmq[b - (1 << lg) + 1][lg]);
}

void lazy(int nod, int st, int dr) {
  aint[nod] = lz[nod];
  if(st != dr) {
    lz[nod << 1] = lz[nod];
    lz[nod << 1|1] = lz[nod];
  }
  lz[nod] = 0;
}

void update(int nod, int st, int dr, int l, int r, int val) {
  if(lz[nod]) {
    lazy(nod, st, dr);
  }
  if(st > r || dr < l) {
    return;
  }
  if(l <= st && dr <= r) {
    lz[nod] = val;
    lazy(nod, st, dr);
    return;
  }
  int mid = (st + dr) / 2;
  update(nod << 1, st, mid, l, r, val);
  update(nod << 1|1, mid + 1, dr, l, r, val);
}

int query(int nod, int st, int dr, int poz) {
  if(lz[nod]) {
    lazy(nod, st, dr);
  }
  if(st == dr) {
    return aint[nod];
  }
  int mid = (st + dr) / 2;
  if(poz <= mid) {
    return query(nod << 1, st, mid, poz);
  } else {
    return query(nod << 1|1, mid + 1, dr, poz);
  }
}

void laba_change(int nod, int st, int dr, int poz, int val) {
  if(st == dr) {
    laba[nod] = val;
    return;
  }
  int mid = (st + dr) / 2;
  if(poz <= mid) {
    laba_change(nod << 1, st, mid, poz, val);
  } else {
    laba_change(nod << 1|1, mid + 1, dr, poz, val);
  }
  laba[nod] = min(laba[nod << 1], laba[nod << 1|1]);
}

int laba_query(int nod, int st, int dr, int l, int r) {
  if(st > r || dr < l) {
    return 0; // nush de ce pula mea s-ar ajunce aici, dar ma rog
  }
  if(st == l && dr == r) {
    return laba[nod];
  }
  int mid = (st + dr) / 2, ans1 = 6969, ans2 = 6969;
  if(l <= mid) {
    ans1 = laba_query(nod << 1, st, mid, l, min(r, mid));
  }
  mid ++;
  if(r >= mid) {
    ans2 = laba_query(nod << 1|1, mid, dr, max(l, mid), r);
    //ans2 = laba_query(nod << 1|1, mid + 1, dr, max(mid + 1, l), r);
  }
  return min(ans1, ans2);
}

bool check(int n, int q, vector<int> &nou) {
  for(int i = 1; i <= q; i ++) {
    if(laba_query(1, 1, n, v[i].a, v[i].b) != v[i].val) {
      return 0;
    }
  }
  return 1;
}

void bulan_pe_subtask(int n, int q, vector<int> &nou) {
  for(int i = 1; i <= n; i ++) {
    used[nou[i]] ++;
  }
  vector<int> un;
  for(int i = 0; i < n; i ++) {
    if(!used[i]) {
      un.push_back(i);
    }
  }
  for(auto nr : un) {
    bool ok = 0;
    for(int i = 1; i <= n; i ++) {
      if(!viz[i]) {
        int ult = nou[i];
        nou[i] = nr;
        laba_change(1, 1, n, i, nr);
        if(check(n, q, nou)) {
          viz[i] = 1;
          ok = 1;
          break;
        } else {
          laba_change(1, 1, n, i, ult);
          nou[i] = ult;
        }
      }
    }
    if(!ok) {
      for(int i = 1; i <= n; i ++) {
        cout << "-1 ";
      }
      return;
    }
  }
  for(int i = 1; i <= n; i ++) {
    cout << nou[i] << " ";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  for(int i = 1; i <= q; i ++) {
    int a, b, val;
    cin >> a >> b >> val;
    a ++;
    b ++;
    v[i].a = a, v[i].b = b, v[i].val = val;
  }
  sort(v + 1, v + q + 1);
  for(int i = 1; i <= q; i ++) {
    update(1, 1, n, v[i].a, v[i].b, v[i].val);
  }
  vector<int> nou(n + 1);
  for(int i = 1; i <= n; i ++) {
    nou[i] = query(1, 1, n, i);
    rmq[i][0] = nou[i];
  }
  for(int j = 1; (1 << j) <= n; j ++) {
    for(int i = 1; i + (1 << j) - 1 <= n; i ++) {
      rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
    }
  }
  if(n <= 1000) {
    bulan_pe_subtask(n, q, nou);
    return 0;
  }
  for(int i = 1; i <= q; i ++) {
    if(rmquery(v[i].a, v[i].b) != v[i].val) {
      for(int j = 1; j <= n; j ++) {
        cout << "-1 ";
      }
      return 0;
    }
  }
  for(int i = 1; i <= n; i ++) {
    cout << nou[i] << " ";
  }
  return 0;
}

Compilation message

rmq.cpp:3:21: warning: deprecated
    3 | #pragma GCC warning "deprecated"
      |                     ^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Execution timed out 1062 ms 348 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Execution timed out 1062 ms 348 KB Time limit exceeded
13 Halted 0 ms 0 KB -