답안 #486221

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486221 2021-11-11T02:46:09 Z diedie Index (COCI21_index) C++17
110 / 110
335 ms 51056 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi32 = pair<int, int>;
using pi64 = pair<int64_t, int64_t>;

const int maxN = 2e5 + 5;
const int maxC = 10000000;
const ll INF = 2e18 + 7;
const int MOD = 998244353;

int n, nTest;
int idx;

struct Segment {
    int left, right, val;
} st[maxC];

void init (int id, int l, int r){
    if (l == r) return;

    int mid = (l + r) / 2;
    st[id].left = ++idx;
    st[id].right = ++idx;
    init(st[id].left, l, mid);
    init(st[id].right, mid + 1, r);
}

void update (int id, int pos, int l, int r, int h) {
    if (l == r) {
        st[id].val = st[pos].val + 1;
        return;
    }

    int mid = (l + r) / 2;
    st[id].left = st[pos].left;
    st[id].right = st[pos].right;
    if (h <= mid) {
        st[id].left = ++idx;
        update(st[id].left, st[pos].left, l, mid, h);
    }
    else {
        st[id].right = ++idx;
        update(st[id].right, st[pos].right, mid + 1, r, h);
    }
    st[id].val = st[st[id].left].val + st[st[id].right].val;
}

int root[maxN];

int query (int id, int pos, int l, int r, int add) {
    if (l == r) return l;

    int mid = (l + r) / 2;
    int tmp = st[st[id].right].val - st[st[pos].right].val + add;
    if (tmp >= mid + 1)
        return query(st[id].right, st[pos].right, mid + 1, r, add);
    else return query(st[id].left, st[pos].left, l, mid, tmp);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> nTest;
    idx = 1;
    root[0] = 1;
    init(1, 1, n);
    for (int i = 1; i <= n; ++i) {
        int h; cin >> h;
        root[i] = root[i - 1];
        int old = root[i];
        root[i] = ++idx;
        update(root[i], old, 1, n, h);
    }

    while (nTest--) {
        int l, r; cin >> l >> r;
        cout << query(root[r], root[l-1], 1, n, 0) << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 39 ms 11728 KB Output is correct
12 Correct 39 ms 11716 KB Output is correct
13 Correct 43 ms 11716 KB Output is correct
14 Correct 40 ms 11772 KB Output is correct
15 Correct 39 ms 11780 KB Output is correct
16 Correct 39 ms 11700 KB Output is correct
17 Correct 40 ms 11832 KB Output is correct
18 Correct 52 ms 11764 KB Output is correct
19 Correct 41 ms 11724 KB Output is correct
20 Correct 39 ms 11684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 39 ms 11728 KB Output is correct
12 Correct 39 ms 11716 KB Output is correct
13 Correct 43 ms 11716 KB Output is correct
14 Correct 40 ms 11772 KB Output is correct
15 Correct 39 ms 11780 KB Output is correct
16 Correct 39 ms 11700 KB Output is correct
17 Correct 40 ms 11832 KB Output is correct
18 Correct 52 ms 11764 KB Output is correct
19 Correct 41 ms 11724 KB Output is correct
20 Correct 39 ms 11684 KB Output is correct
21 Correct 245 ms 50864 KB Output is correct
22 Correct 253 ms 50932 KB Output is correct
23 Correct 233 ms 50824 KB Output is correct
24 Correct 248 ms 50756 KB Output is correct
25 Correct 251 ms 50792 KB Output is correct
26 Correct 240 ms 50812 KB Output is correct
27 Correct 271 ms 51056 KB Output is correct
28 Correct 254 ms 50756 KB Output is correct
29 Correct 243 ms 50868 KB Output is correct
30 Correct 335 ms 50756 KB Output is correct