QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#879631#8612. The Best Wifehos_lyricWA 1ms3840kbC++144.4kb2025-02-02 09:20:542025-02-02 09:20:55

Judging History

This is the latest submission verdict.

  • [2025-02-02 09:20:55]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3840kb
  • [2025-02-02 09:20:54]
  • Submitted

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// Link-Cut Tree
//   solid path as BST; left: root side
//   Modify update() for other data.
struct Tree {
  Tree *l, *r, *p;
  int sz;
  int id;
  Int val, sum;
  explicit Tree(int id_ = -1, Int val_ = 0) : id(id_), val(val_), sum(val_) {
    l = r = p = nullptr;
    sz = 1;
  }
  void update() {
    sz = (l ? l->sz : 0) + 1 + (r ? r->sz : 0);
    sum = (l ? l->sum : 0) + val + (r ? r->sum : 0);
  }
  bool isRoot() const {
    return (!p || (p->l != this && p->r != this));
  }
  void rotate() {
         if (p->l == this) { if (r) { r->p = p; } p->l = r; r = p; }
    else if (p->r == this) { if (l) { l->p = p; } p->r = l; l = p; }
    Tree *pp = p->p;
    if (pp) {
           if (pp->l == p) pp->l = this;
      else if (pp->r == p) pp->r = this;
    }
    p->update(); p->p = this; p = pp;
  }
  void splay() {
    for (; !isRoot(); rotate()) {
      if (!p->isRoot()) ((p->l == this) == (p->p->l == p)) ? p->rotate() : rotate();
    }
    update();
  }

  // Makes the path from v to the root solid
  // Returns the node where it entered the last solid path
  Tree *expose() {
    Tree *u = this, *v = nullptr;
    for (; u; u = u->p) { u->splay(); u->r = v; u->update(); v = u; }
    splay();
    return v;
  }

  // parent of this := u
  void link(Tree *u) {
    expose(); u->expose(); p = u; u->r = this; u->update();
  }

  // parent of this := null
  void cut() {
    expose(); l->p = nullptr; l = nullptr; update();
  }

  // the root of the tree this belongs
  Tree *root() {
    expose();
    for (Tree *u = this; ; u = u->l) if (!u->l) { u->splay(); return u; }
  }

  // LCA of this and u
  //   Assumes this->root == u->root
  Tree *lca(Tree *u) {
    expose(); return u->expose();
  }
};

////////////////////////////////////////////////////////////////////////////////


int N;
vector<int> L, R;

int main() {
  for (; ~scanf("%d", &N); ) {
    L.resize(N);
    R.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d%d", &L[i], &R[i]);
      --L[i];
    }
    
    const int maxR = *max_element(R.begin(), R.end());
    vector<Tree> nodes(maxR + 1);
    for (int x = 0; x <= maxR; ++x) nodes[x] = Tree(x, 0);
    for (int x = 0; x < maxR; ++x) nodes[x].link(&nodes[x + 1]);
    
    set<pair<int, int>> lrs;
    lrs.emplace(-1, -1);
    lrs.emplace(maxR + 1, maxR + 1);
    for (int i = 0; i < N; ++i) {
      auto it = lrs.lower_bound(make_pair(L[i], R[i]));
      if (L[i] <= it->first && R[i] <= it->second) {
        for (; ; it = lrs.erase(it)) {
          --it;
          const int l = it->first, r = it->second;
          if (l <= L[i] && r <= R[i]) break;
// cerr<<"erase "<<l<<" "<<r<<endl;
          nodes[l].cut();
          nodes[l].val = 0;
          nodes[l].link(&nodes[l + 1]);
        }
// cerr<<"insert "<<L[i]<<" "<<R[i]<<endl;
        nodes[L[i]].cut();
        nodes[L[i]].val = 1;
        nodes[L[i]].link(&nodes[R[i]]);
        lrs.emplace_hint(it, L[i], R[i]);
      }
// pv(lrs.begin(),lrs.end());
      nodes[0].expose();
      const int ans = (nodes[0].l ? nodes[0].l->sum : 0) + nodes[0].val;
      printf("%d\n", ans);
    }
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3840kb

input:

5
1 3
3 5
1 2
5 6
4 4

output:

1
1
2
2
3

result:

ok 5 number(s): "1 1 2 2 3"

Test #2:

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

input:

100
67 72
1 100
61 65
87 91
19 77
47 97
3 85
64 97
6 92
33 36
7 27
33 44
13 98
3 62
36 97
26 39
7 35
2 92
8 64
37 83
5 89
26 87
6 96
58 71
49 96
3 85
27 65
16 93
26 70
8 97
1 100
8 93
5 59
9 92
9 99
1 100
8 81
7 66
4 78
12 52
28 42
1 36
2 100
75 81
26 61
8 86
4 44
58 74
29 100
40 77
83 100
39 59
3 9...

output:

1
1
2
3
3
3
3
3
3
4
5
5
5
5
5
5
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
5
5
5
5
5
5
5
5
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
8
8
8
8
8
8
9
9
9
9
10
10

result:

wrong answer 17th numbers differ - expected: '5', found: '4'