QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105354#6192. Interval ProblemMelacauWA 227ms30072kbC++173.9kb2023-05-13 22:48:142023-05-13 22:48: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-05-13 22:48:15]
  • 评测
  • 测评结果:WA
  • 用时:227ms
  • 内存:30072kb
  • [2023-05-13 22:48:14]
  • 提交

answer

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

using s64 = long long;

const int N = 2e5 + 5;
const int M = 4e5 + 5;
const int T = 1 << 20;

int n, m, cnt[M], lpos[M], rpos[M];
struct Seg{int l, r, k;}seg[N];
int siz[T], tag[T];
s64 sum[T], ans[N];

void build(int k = 1, int L = 0, int R = m) {
    tag[k] = 0;
    if (L + 1 == R) {
        siz[k] = cnt[L];
        sum[k] = siz[k] << 1;
        return;
    }
    int mid = L + R >> 1;
    build(k << 1, L, mid);
    build(k << 1 | 1, mid, R);
    siz[k] = siz[k << 1] + siz[k << 1 | 1];
    sum[k] = sum[k << 1] + sum[k << 1 | 1];
}

void addn(int k, int v) {
    tag[k] += v;
    sum[k] += s64(tag[k]) * siz[k];
}

void down(int k) {
    addn(k << 1, tag[k]);
    addn(k << 1 | 1, tag[k]);
    tag[k] = 0;
}

void add(int l, int r, int k = 1, int L = 0, int R = m) {
    if (l >= r || r <= L || R <= l) return;
    if (l <= L && R <= r) return addn(k, 1);
    down(k); int mid = L + R >> 1;
    add(l, r, k << 1, L, mid);
    add(l, r, k << 1 | 1, mid, R);
    sum[k] = sum[k << 1] + sum[k << 1 | 1];
}

s64 query(int l, int r, int k = 1, int L = 0, int R = m) {
    if (l >= r || r <= L || R <= l) return 0;
    if (l <= L && R <= r) return sum[k];
    down(k); int mid = L + R >> 1;
    return query(l, r, k << 1, L, mid) + query(l, r, k << 1 | 1, mid, R);
}

void solve() {
    sort(seg, seg + n, [](const Seg& a, const Seg& b){
        return a.l == b.l ? a.r > b.r : a.l < b.l;
    });
    fill(lpos, lpos + m + 1, m + 1);
    fill(rpos, rpos + m + 1, -1);
    for (int i = 0, j = 0, curR = -1; i < m; ++i) {
        if (j < n && seg[j].l == i && seg[j].r > curR) {
            if (curR >= 0) rpos[curR] = i;
            lpos[curR = seg[j].r] = i;
        }
        while (j < n && seg[j].l == i) ++j;
        if (curR == i) {rpos[curR] = i + 1; curR = -1;}
        if (curR >= 0) lpos[curR] = min(lpos[curR], lpos[i]);
    }
    fill(cnt, cnt + m + 1, 0);
    for (int i = 0; i < n; ++i) ++cnt[seg[i].r];
    build();
    for (int i = 0, j = 0, l = 0; j < n && i < m; ++i) {
        while (j < n && seg[j].l == i) {
            ans[seg[j].k] += query(l, seg[j].l);
            // cerr << j << ' ' << seg[j].l << ' ' << seg[j].r << ' ' << ans[seg[j].k] << endl;
            ++j;
        }
        // cerr << i << ' ' << lpos[i] << ' ' << rpos[i] << endl;
        if (rpos[i] == i + 1) l = i + 1;
        else if (rpos[i] >= 0) {
            add(lpos[i], rpos[i]);
            // cerr << "ADD " << lpos[i] << ' ' << rpos[i] << endl;
        }
    }
    // cerr << endl;
}

void solveint() {
    sort(seg, seg + n, [](const Seg& a, const Seg& b){
        return a.r > b.r;
    });
    fill(cnt, cnt + m + 1, 0);
    static int lcnt[M] = {0};
    for (int i = 0; i < n; ++i) ++cnt[seg[i].r];
    for (int i = 1; i < m; ++i) cnt[i] += cnt[i - 1];
    for (int i = 0, j, now = 0, x = m; i < n; i = j) {
        for (j = i; j < n && seg[j].r == seg[i].r; ++j) ++lcnt[seg[j].l];
        for (; x > seg[i].r; --x) now -= lcnt[x];
        for (int k = i; k < j; ++k) {
            ans[seg[k].k] += cnt[seg[k].r] - (seg[k].l == 0 ? 0 : cnt[seg[k].l - 1]);
            ans[seg[k].k] += now;
        }
        now += j - i;
    }
}

void reverse() {
    for (int i = 0; i < n; ++i) {
        swap(seg[i].l, seg[i].r);
        seg[i].l = m - seg[i].l - 1;
        seg[i].r = m - seg[i].r - 1;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin >> n; m = n << 1;
    for (int i = 0; i < n; ++i) {
        cin >> seg[i].l >> seg[i].r;
        --seg[i].l; seg[i].r -= 2;
        seg[i].k = i;
    }
    solve();
    // for (int i = 0; i < n; ++i)
    //     cerr << ans[i] << " \n"[i + 1 == n];
    solveint();
    reverse();
    // for (int i = 0; i < n; ++i)
    //     cerr << ans[i] << " \n"[i + 1 == n];
    solve();
    // for (int i = 0; i < n; ++i)
    //     cerr << ans[i] << " \n"[i + 1 == n];
    for (int i = 0; i < n; ++i)
        cout << ans[i] - 1 << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 227ms
memory: 30072kb

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:

12
37
23
6
16
96
2
1
1
19
7
0
25
31
7
11
2
26
14
8
2
11
0
1
0
2
61
77
40
6
4
0
27
30
17
12
32
10
5
9
1
16
16
33
9
1
2
50
18
22
21
15
4
37
2
50
0
0
47
21
0
63
4
18
18
19
25
14
2
4
0
43
10
0
1
6
116
7
1
20
3
6
1
86
7
26
16
13
49
43
4
10
27
24
1
0
4
4
0
1
8
3
1
18
93
14
97
5
19
58
216
0
0
0
51
0
20
24
...

result:

wrong answer 2nd numbers differ - expected: '21', found: '37'