QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93789#6192. Interval ProblemksunhokimWA 104ms12452kbC++172.2kb2023-04-02 14:22:132023-04-02 14:22:15

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-02 14:22:15]
  • 评测
  • 测评结果:WA
  • 用时:104ms
  • 内存:12452kb
  • [2023-04-02 14:22:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int INF = 1e9;

// Fenwick Tree for summation
struct fenwick_tree {
  int n;
  vector<ll> bits;
  fenwick_tree(int n) : n(n), bits(n+1) {}

  // add delta to node v
  void update(int v, int delta) {
    for (++v; v <= n; v += v&(-v)) 
      bits[v] += delta;
  }

  // get prefix sum of range [0,r]
  ll query(int r) {
    ll res = 0;
    for (++r; r > 0; r -= r&(-r))
      res += bits[r];
    return res;
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int n;
  cin >> n;
  
  vector<tuple<int,int,int>> intervals(n);
  vector<int> A(n*2);
  for (int i=0;i<n;i++){
    int l,r;
    cin >> l >> r;
    --l,--r;
    A[l] = i, A[r] = i;
    intervals[i]={l,r,i};
  }

  vector<ll> ans(n);
  vector<int> vis(n);
  fenwick_tree ft(n*2+1);
  sort(begin(intervals), end(intervals));
  for (int i=0;i<n;i++){
    auto [l,r,j] = intervals[i];
    ft.update(r,1);
  }
  for (int i=0;i<n;i++){
    auto [l,r,j] = intervals[i];
    ft.update(r,-1);
    ans[j] += ft.query(r) - ft.query(l);
  }
  for (int i=0;i<n;i++){
    auto [l,r,j] = intervals[i];
    ft.update(r,1);
  }
  for (int i=n-1;i>=0;i--){
    auto [l,r,j] = intervals[i];
    ft.update(r,-1);
    ans[j] -= ft.query(2*n) - ft.query(r);
  }


  int opens = 0;
  int closes = 0;
  ll disjoint_sum = 0;
  map<int, int> stack;
  
  for (int i=0;i<n*2;i++) {
    if (!stack.count(A[i])) {
      stack.insert({A[i],opens});
      opens++;
      ans[A[i]] += disjoint_sum + closes;
    } else {
      stack.erase(A[i]);
      closes++;
      disjoint_sum += 1;
      opens--;
      ans[A[i]] += opens;
      if (opens == 0) {
        disjoint_sum = 0;
        closes = 0;
      }
    }
  }
  opens=0,closes=0,disjoint_sum=0;
  for (int i=n*2-1;i>=0;i--) {
    if (!stack.count(A[i])) {
      stack.insert({A[i],opens});
      opens++;
      ans[A[i]] += disjoint_sum + closes;
    } else {
      stack.erase(A[i]);
      closes++;
      disjoint_sum += 1;
      opens--;
      ans[A[i]] += opens;
      if (opens == 0) {
        disjoint_sum = 0;
        closes = 0;
      }
    }
  }


  for (int i=0;i<n;i++){
    cout << ans[i] << "\n";
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
2 3
6 7
1 9
5 10
4 8

output:

7
5
4
5
5

result:

ok 5 number(s): "7 5 4 5 5"

Test #2:

score: -100
Wrong Answer
time: 104ms
memory: 12452kb

input:

200000
342504 342505
248314 248317
259328 259334
234817 234821
225726 225732
371424 371425
260562 260565
34752 34753
132098 132100
262130 262134
7254 7255
228491 228492
43488 43492
188408 188410
11691 11692
350334 350335
327350 327352
360684 360685
182051 182052
72131 72135
194666 194668
61303 61313...

output:

11
14
13
6
16
17
2
1
1
15
5
0
21
16
6
11
2
17
14
8
2
10
0
1
0
2
30
20
22
6
4
0
20
15
14
9
17
10
5
9
1
16
12
19
6
1
2
28
16
13
16
12
4
16
2
18
0
0
27
16
0
22
4
9
18
16
20
12
2
4
0
29
9
0
1
6
30
7
1
13
3
6
1
25
7
19
16
12
17
28
4
10
19
16
1
0
4
4
0
1
8
3
1
14
23
13
25
5
12
19
27
0
0
0
20
0
13
11
7
14
...

result:

wrong answer 1st numbers differ - expected: '12', found: '11'