QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#378658#8567. Cheap Constructionucup-team180#WA 0ms3640kbC++145.5kb2024-04-06 14:04:072024-04-06 14:04:08

Judging History

This is the latest submission verdict.

  • [2024-04-06 14:04:08]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3640kb
  • [2024-04-06 14:04:07]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
template <typename T>
struct splay_tree{
  struct node{
    node* p;
    array<node*, 2> ch;
    int sz;
    T val;
    node(): p(nullptr), ch({nullptr, nullptr}){
    }
    node(T x): p(nullptr), ch({nullptr, nullptr}), sz(1), val(x){
    }
  };
  node* root = nullptr;
  splay_tree(){
  }
  splay_tree(vector<T> A){
    root = build(A, 0, A.size());
  }
  node* build(vector<T> &A, int L, int R){
    if (L == R){
      return nullptr;
    }
    int m = (L + R) / 2;
    node* v = new node(A[m]);
    node* l = build(A, L, m);
    v->ch[0] = l;
    if (l != nullptr){
      l->p = v;
    }
    node* r = build(A, m + 1, R);
    v->ch[1] = r;
    if (r != nullptr){
      r->p = v;
    }
    update(v);
    return v;
  }
  bool empty(){
    return root == nullptr;
  }
  int size(){
    return size(root);
  }
  int size(node* v){
    if (v == nullptr){
      return 0;
    }
    return v->sz;
  }
  void update(node* v){
    v->sz = size(v->ch[0]) + 1 + size(v->ch[1]);
  }
  int child_id(node *v){
    if (v->p == nullptr){
      return -1;
    } else if (v->p->ch[0] == v){
      return 0;
    } else {
      return 1;
    }
  }
  void update_child(node* v, node *w){
    w->p = v->p;
    if (v->p == nullptr){
      return;
    }
     if (v->p->ch[0] == v){
      v->p->ch[0] = w;
    } else {
      v->p->ch[1] = w;
    }
  }
  void rotate(node* v, int c){
    node* w = v->ch[c ^ 1];
    update_child(v, w);
    v->ch[c ^ 1] = w->ch[c];
    if (w->ch[c] != nullptr){
      w->ch[c]->p = v;
    }
    v->p = w;
    w->ch[c] = v;
    update(v);
    update(w);
  }
  void splay(node* v){
    while (v->p != nullptr){
      node* p = v->p;
      node* g = p->p;
      int x = child_id(v);
      int y = child_id(p);
      if (y == -1){
        rotate(p, x ^ 1);
      } else if (x == y){
        rotate(g, x ^ 1);
        rotate(p, x ^ 1);
      } else {
        rotate(p, y);
        rotate(g, x);
      }
    }
    root = v;
  }
  node* get(node *v, int k){
    while (true){
      int s = size(v->ch[0]);
      if (k < s){
        v = v->ch[0];
      } else if (k == s){
        splay(v);
        return v;
      } else {
        k -= s + 1;
        v = v->ch[1];
      }
    }
  }
  node* get(int k){
    return get(root, k);
  }
  T operator [](int k){
    return get(root, k)->x;
  }
  node* insert(node *r, int k, T x){
    node* v = new node(x);
    if (k == size(r)){
      v->ch[0] = r;
      if (r != nullptr){
        r->p = v;
      }
      r = v;
      update(v);
    } else {
      node* u = get(r, k);
      v->ch[0] = u->ch[0];
      v->ch[1] = u;
      if (u->ch[0] != nullptr){
        u->ch[0]->p = v;
      }
      u->ch[0] = nullptr;
      u->p = v;
      update(u);
      update(v);
    }
    root = v;
    return v;
  }
  node* insert(int k, T x){
    return insert(root, k, x);
  }
  vector<int> get(){
    vector<int> ans;
    auto dfs = [&](auto dfs, node* v) -> void {
      if (v == nullptr){
        return;
      }
      dfs(dfs, v->ch[0]);
      ans.push_back(v->val);
      dfs(dfs, v->ch[1]);
    };
    dfs(dfs, root);
    return ans;
  }
};
struct binay_indexed_tree{
  int N;
  vector<int> BIT;
  binay_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i, int x){
    i++;
    while (i <= N){
      BIT[i] += x;
      i += i & -i;
    }
  }
  int sum(int i){
    int ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  int sum(int L, int R){
    return sum(R) - sum(L);
  }
};
struct segment_tree{
  int N;
  vector<int> A;
  vector<int> ST;
  segment_tree(vector<int> A): A(A){
    int n = A.size();
    N = 1;
    while (N < n){
      N *= 2;
    }
    ST = vector<int>(N * 2 - 1, -1);
    for (int i = 0; i < n; i++){
      ST[N - 1 + i] = i;
    }
    for (int i = N - 2; i >= 0; i--){
      ST[i] = op(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  int op(int a, int b){
    if (a == -1){
      return b;
    } else if (b == -1){
      return a;
    } else if (A[a] < A[b] || A[a] == A[b] && a < b){
      return a;
    } else {
      return b;
    }
  }
  int query(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return -1;
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return op(query(L, R, i * 2 + 1, l, m), query(L, R, i * 2 + 2, m, r));
    }
  }
  int query(int L, int R){
    return query(L, R, 0, 0, N);
  }
};
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++){
    int n;
    cin >> n;
    vector<int> p(n), h(n);
    for (int j = 0; j < n; j++){
      cin >> p[j] >> h[j];
      p[j]--;
    }
    splay_tree<int> ST;
    for (int j = 0; j < n; j++){
      ST.insert(p[j], h[j]);
    }
    vector<int> a = ST.get();
    set<int> st1, st2;
    for (int j = 0; j < n; j++){
      st1.insert(j);
    }
    st2.insert(-1);
    st2.insert(n);
    segment_tree ST2(a);
    binay_indexed_tree BIT(n);
    for (int j = 0; j < n; j++){
      int idx = *st1.begin();
      int L = *prev(st2.lower_bound(idx)) + 1;
      int R = *st2.lower_bound(idx);
      int idx2 = ST2.query(L, R);
      int P = BIT.sum(0, idx2);
      cout << P + 1 << ' ' << a[idx2] << '\n';
      st1.erase(idx2);
      st2.insert(idx2);
      BIT.add(idx2, 1);
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3624kb

input:

1
3
1 1
2 2
1 3

output:

1 1
1 3
3 2

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3640kb

input:

2
3
1 1
1 2
3 1
3
1 3
2 1
3 3

output:

1 1
1 2
3 1
1 1
1 3
3 3

result:

wrong answer 2nd lines differ - expected: '1 1', found: '1 2'