답안 #798568

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
798568 2023-07-30T20:49:56 Z HunterXD RMQ (NOI17_rmq) C++17
100 / 100
103 ms 12676 KB
#include <bits/stdc++.h>
using namespace std;

typedef int ll;
typedef vector<int> vl;
typedef vector<vl> vvl;

#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second

const char nd = '\n';
const int infi = 1e9;

struct queries {
  ll l, r, mini;
};

struct nodo {
  ll l, r;
  ll mini;
  nodo *left, *right;
};

nodo *st;

void start(nodo *yo, vl &a) {
  if (yo->l == yo->r) {
    yo->mini = a[yo->l];
    return;
  }
  ll mid = (yo->l + yo->r) / 2;

  yo->left = new nodo({yo->l, mid, 0, NULL, NULL});
  yo->right = new nodo({mid + 1, yo->r, 0, NULL, NULL});

  start(yo->left, a);
  start(yo->right, a);

  yo->mini = min(yo->left->mini, yo->right->mini);
}

ll query(nodo *c, ll l, ll r) {
  if (r < c->l || c->r < l) return infi;
  if (l <= c->l && c->r <= r) return c->mini;
  return min(query(c->left, l, r), query(c->right, l, r));
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, q;
  cin >> n >> q;

  vector<queries> que;
  vector<pair<int, int> > might(n, make_pair(-1, -1)),
      need(n, make_pair(-1, -1));
  vl res(n, -1);

  ll l, r, mini;
  while (q--) {
    cin >> l >> r >> mini;
    que.pb({l, r, mini});
    if (need[mini].f == -1) {
      need[mini] = make_pair(l, r);
    } else {
      need[mini].f = max(need[mini].f, l);
      need[mini].s = min(need[mini].s, r);
    }

    if (might[mini].f == -1) {
      might[mini] = make_pair(l, r);
    } else {
      might[mini].f = min(might[mini].f, l);
      might[mini].s = max(might[mini].s, r);
    }
  }

  set<ll> have, marked;
  for (ll i = 0; i < n; i++) have.insert(i), marked.insert(i);

  set<ll>::iterator it, it2;

  for (ll i = n - 1; i >= 0; i--) {
    if (need[i].f == -1) continue;

    it = marked.lower_bound(need[i].f);

    if (need[i].f > need[i].s || it == marked.end() || *it > need[i].s) {
      for (auto v : res) cout << -1 << " ";
      cout << nd;
      return 0;
    }

    res[*it] = i;
    marked.erase(it);
    have.erase(i);

    while (true) {
      it = marked.lower_bound(might[i].f);
      if (it == marked.end() || *it > might[i].s) break;

      it2 = have.lower_bound(i);
      if (it2 == have.end()) {
        for (auto v : res) cout << -1 << " ";
        cout << nd;
        return 0;
      }

      res[*it] = *it2;
      marked.erase(it);
      have.erase(it2);
    }
  }

  for (auto &v : res) {
    if (v == -1) {
      v = *(have.begin());
      have.erase(have.begin());
    }
  }

  // for (auto v : res) cout << v << " ";
  // cout << nd;

  nodo *st = new nodo({0, n - 1, 0, NULL, NULL});
  start(st, res);

  for (auto v : que) {
    if (query(st, v.l, v.r) != v.mini) {
      for (auto v : res) cout << -1 << " ";
      cout << nd;
      return 0;
    }
  }

  for (auto v : res) cout << v << " ";
  cout << nd;
}

Compilation message

rmq.cpp: In function 'int main()':
rmq.cpp:93:17: warning: unused variable 'v' [-Wunused-variable]
   93 |       for (auto v : res) cout << -1 << " ";
      |                 ^
rmq.cpp:108:19: warning: unused variable 'v' [-Wunused-variable]
  108 |         for (auto v : res) cout << -1 << " ";
      |                   ^
rmq.cpp:134:17: warning: unused variable 'v' [-Wunused-variable]
  134 |       for (auto v : res) cout << -1 << " ";
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 252 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 252 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 452 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 252 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 452 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 103 ms 10892 KB Output is correct
28 Correct 94 ms 11712 KB Output is correct
29 Correct 78 ms 11016 KB Output is correct
30 Correct 99 ms 12424 KB Output is correct
31 Correct 74 ms 9588 KB Output is correct
32 Correct 81 ms 9536 KB Output is correct
33 Correct 38 ms 10116 KB Output is correct
34 Correct 25 ms 6868 KB Output is correct
35 Correct 54 ms 11572 KB Output is correct
36 Correct 42 ms 12608 KB Output is correct
37 Correct 46 ms 12676 KB Output is correct
38 Correct 22 ms 7952 KB Output is correct
39 Correct 42 ms 11840 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct