답안 #330282

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
330282 2020-11-24T12:42:52 Z dolphingarlic Matryoshka (JOI16_matryoshka) C++14
100 / 100
653 ms 140524 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

struct Node {
    int l, r, val;
    Node *lc, *rc;

    Node(int L, int R): l(L), r(R), val(0), lc(nullptr), rc(nullptr) {}

    void update(int p, int v) {
        val = max(val, v);
        if (l != r) {
            int mid = (l + r) / 2;
            if (p > mid) {
                if (!rc) rc = new Node(mid + 1, r);
                rc->update(p, v);
            } else {
                if (!lc) lc = new Node(l, mid);
                lc->update(p, v);    
            }
        }
    }

    int query(int a, int b) {
        if (r < a || l > b) return 0;
        if (r <= b && l >= a) return val;
        int ret = 0;
        if (lc) ret = max(ret, lc->query(a, b));
        if (rc) ret = max(ret, rc->query(a, b));
        return ret;
    }
};

pair<int, int> doll[200000];
pair<pair<int, int>, int> query[200000];
int lis[200000], ans[200000];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        cin >> doll[i].first >> doll[i].second;
        doll[i].second = -doll[i].second;
    }
    for (int i = 0; i < q; i++) {
        cin >> query[i].first.first >> query[i].first.second;
        query[i].second = i;
    }
    sort(doll, doll + n, greater<pair<int, int>>());
    sort(query, query + q, greater<pair<pair<int, int>, int>>());

    vector<int> dp;
    for (int i = 0; i < n; i++) {
        lis[i] = upper_bound(dp.begin(), dp.end(), -doll[i].second) - dp.begin();
        if (lis[i] == dp.size()) dp.push_back(-doll[i].second);
        else dp[lis[i]] = -doll[i].second;
        lis[i]++;
    }

    Node *segtree = new Node(1, 1000000000);
    for (int i = 0, j = 0; i < q; i++) {
        while (j < n && doll[j].first >= query[i].first.first) {
            segtree->update(-doll[j].second, lis[j]);
            j++;
        }
        ans[query[i].second] = segtree->query(1, query[i].first.second);
    }

    for (int i = 0; i < q; i++) cout << ans[i] << '\n';
    return 0;
}

Compilation message

matryoshka.cpp: In function 'int main()':
matryoshka.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         if (lis[i] == dp.size()) dp.push_back(-doll[i].second);
      |             ~~~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 2 ms 876 KB Output is correct
23 Correct 5 ms 2412 KB Output is correct
24 Correct 5 ms 2412 KB Output is correct
25 Correct 8 ms 2412 KB Output is correct
26 Correct 4 ms 1388 KB Output is correct
27 Correct 5 ms 2412 KB Output is correct
28 Correct 3 ms 1004 KB Output is correct
29 Correct 5 ms 2412 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
31 Correct 7 ms 2412 KB Output is correct
32 Correct 5 ms 2412 KB Output is correct
33 Correct 6 ms 2432 KB Output is correct
34 Correct 5 ms 2412 KB Output is correct
35 Correct 5 ms 2412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 2 ms 876 KB Output is correct
23 Correct 5 ms 2412 KB Output is correct
24 Correct 5 ms 2412 KB Output is correct
25 Correct 8 ms 2412 KB Output is correct
26 Correct 4 ms 1388 KB Output is correct
27 Correct 5 ms 2412 KB Output is correct
28 Correct 3 ms 1004 KB Output is correct
29 Correct 5 ms 2412 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
31 Correct 7 ms 2412 KB Output is correct
32 Correct 5 ms 2412 KB Output is correct
33 Correct 6 ms 2432 KB Output is correct
34 Correct 5 ms 2412 KB Output is correct
35 Correct 5 ms 2412 KB Output is correct
36 Correct 424 ms 25708 KB Output is correct
37 Correct 204 ms 11116 KB Output is correct
38 Correct 181 ms 67564 KB Output is correct
39 Correct 179 ms 9548 KB Output is correct
40 Correct 186 ms 10220 KB Output is correct
41 Correct 250 ms 11244 KB Output is correct
42 Correct 168 ms 9076 KB Output is correct
43 Correct 128 ms 9832 KB Output is correct
44 Correct 637 ms 140268 KB Output is correct
45 Correct 629 ms 140524 KB Output is correct
46 Correct 653 ms 140336 KB Output is correct
47 Correct 516 ms 139500 KB Output is correct
48 Correct 544 ms 139860 KB Output is correct
49 Correct 525 ms 139308 KB Output is correct
50 Correct 519 ms 140012 KB Output is correct