답안 #891360

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891360 2023-12-22T19:41:19 Z stefanneagu RMQ (NOI17_rmq) C++17
23 / 100
1000 ms 4696 KB
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1e5 + 1;

struct str {
  int a, b, val;
  const bool operator < (str ult) const {
    if(val != ult.val) {
      return val < ult.val;
    }
    return (b - a + 1) < (ult.b - ult.a + 1);
  }
} v[nmax];

int aint[4 * nmax], lz[4 * nmax], rmq[nmax][20], f[nmax], used[nmax];
bool viz[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);
  }
}

int check(int n, int q, vector<int> &nou) {
  vector<vector<int>> r(n + 1, vector<int>((int) log2(n) + 4));
  for(int i = 1; i <= n; i ++) {
    r[i][0] = nou[i];
  }
  for(int j = 1; (1 << j) <= n; j ++) {
    for(int i = 1; i + (1 << j) - 1 <= n; i ++) {
      r[i][j] = min(r[i][j - 1], r[i + (1 << (j - 1))][j - 1]);
    }
  }
  for(int i = 1; i <= q; i ++) {
    int dist = (v[i].b - v[i].a + 1);
    dist = (int) log2(dist);
    int curr = min(r[v[i].a][dist], r[v[i].b - (1 << dist) + 1][dist]);
    if(curr != 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 = 1; 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;
        if(check(n, q, nou)) {
          viz[i] = 1;
          ok = 1;
          break;
        } else {
          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] - 1 << " ";
  }
}

int 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;
    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] - 1 << " ";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4576 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4576 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 816 ms 4688 KB Output is correct
13 Execution timed out 1022 ms 4440 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4576 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 816 ms 4688 KB Output is correct
13 Execution timed out 1022 ms 4440 KB Time limit exceeded
14 Halted 0 ms 0 KB -