Submission #798559

# Submission time Handle Problem Language Result Execution time Memory
798559 2023-07-30T20:33:36 Z HunterXD RMQ (NOI17_rmq) C++17
0 / 100
1 ms 212 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> > state(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 (state[mini].f == -1) {
      state[mini] = make_pair(l, r);
    } else {
      state[mini].f = max(state[mini].f, l);
      state[mini].s = min(state[mini].s, r);
    }
  }

  set<ll> s;
  for (ll i = 0; i < n; i++) s.insert(i);

  set<ll>::iterator it;

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

    it = s.lower_bound(state[i].f);

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

    do {
      if (*it > state[i].s) break;
      res[*it] = i;
      s.erase(it);
      it = s.lower_bound(state[i].f);
    } while (it != s.end());
  }

  for (ll i = 0; i < n; i++) s.insert(i);

  for (auto v : res) {
    if (v != -1) s.erase(v);
  }

  for (auto &v : res) {
    if (v == -1) {
      v = n - 1;
    }
  }

  // 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:85:17: warning: unused variable 'v' [-Wunused-variable]
   85 |       for (auto v : res) cout << -1 << " ";
      |                 ^
rmq.cpp:118:17: warning: unused variable 'v' [-Wunused-variable]
  118 |       for (auto v : res) cout << -1 << " ";
      |                 ^
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -