답안 #890719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
890719 2023-12-21T19:31:31 Z NotLinux Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
2597 ms 255228 KB
#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 20) + 7;
const int inf = 2e9 + 7;
// bool debug_mode = 0;
inline int closest(vector < int > &v , int x){
    auto it = lower_bound(v.begin() , v.end() , x);
    if(it == v.begin()){
        return -1;
    }
    else{
        return *(--it);
    }
}
struct MergeSortTree{
    vector < int > tree[2*N];
    int sz;
    void merge(vector < int > &a , vector < int > &b , vector < int > &c){
        int pa = 0 , pb = 0;
        while(pa != (int)a.size() or pb != (int)b.size()){
            if(pa == (int)a.size()){
                c.push_back(b[pb++]);
            }
            else if(pb == (int)b.size()){
                c.push_back(a[pa++]);
            }
            else{
                if(a[pa] < b[pb]){
                    c.push_back(a[pa++]);
                }
                else{
                    c.push_back(b[pb++]);                    
                }
            }
        }
    }
    void build(vector < int > &v){
        for(int i = sz;i<2*sz;i++){
            tree[i].push_back(v[i-sz]);
        }
        for(int i = sz-1;i>0;i--){
            // cerr << i << " : ";
            // for(auto itr : tree[i<<1])cout << itr << " ";
            // cerr << "|| ";
            // for(auto itr : tree[i<<1|1])cout << itr << " ";
            // cerr << endl;
            merge(tree[i<<1] , tree[i<<1|1] , tree[i]);
            // cerr << "res : ";for(auto itr : tree[i])cout << itr << " ";cout << endl;
        }
    }
    inline int query(int ind , int val){
        return closest(tree[ind] , val);
    }
} mst;
struct SegmentTree{
    struct Node{
        int mx=0 , indx=0 , ans=-inf;
    } tree[2*N];
    int sz;
    Node merge(Node &a , Node &b){
        Node c;
        c.mx = max(a.mx , b.mx);
        int tmp = mst.query(b.indx , a.mx);
        // cerr << "b : ";for(auto itr : mst.tree[b.indx])cout << itr << " ";cout << endl;
        if(tmp != -1){
            c.ans = max({a.ans , b.ans , (int)a.mx + (int)tmp});
        }
        else {
            c.ans = max(a.ans , b.ans);
        }
        return c;
    }
    void build(vector < int > &v){
        for(int i = sz;i<2*sz;i++){
            tree[i].mx = v[i-sz];
            tree[i].indx = i;
        }
        for(int i = sz-1;i>0;i--){
            tree[i] = merge(tree[i << 1] , tree[i << 1|1]);
            tree[i].indx = i;
            // cerr << tree[i].indx << " : " << tree[i].mx << " " << tree[i].ans << endl;
        }
    }
    vector < Node > vec1 , vec2;
    void dfs(int ql , int qr){
        for(ql += sz , qr += sz; ql < qr;ql >>= 1 , qr >>= 1){
            if(ql&1){
                // cerr << "added : " << ql << endl;
                vec1.push_back(tree[ql++]);
            }
            if(qr&1){
                vec2.push_back(tree[--qr]);
                // cerr << "added : " << qr << endl;
            }
        }
    }
    int query(int l , int r){
        Node ret;
        // debug_mode = 1;
        reverse(vec2.begin() , vec2.end());
        for(auto itr : vec2)vec1.push_back(itr);
        for(auto itr : vec1){
            // cerr << itr.indx << " : " << itr.mx << " " << itr.ans << endl;
            ret = merge(ret , itr);
        }
        // debug_mode = 0;
        vec1.clear();
        vec2.clear();
        // cerr << "query : " << ret.ans << endl;
        return ret.ans;
    }
} segt;
void solve(){
    int n,m;
    cin >> n >> m;
    vector < int > arr(n);
    for(int i = 0;i<n;i++){
        cin >> arr[i];
    }

    int hsb = __lg(n);
    n = (1 << (hsb+1));
    while(arr.size() < n){
        arr.push_back(1e9);
    }

    mst.sz = segt.sz = n;
    mst.build(arr);
    segt.build(arr);
    // cerr << "---" << endl;
    for(int qt = 0;qt<m;qt++){
        int l,r,k;
        cin >> l >> r >> k;
        // cerr << "query : " << l << " " << r << endl;
        l--;
        segt.dfs(l,r);
        cout << (segt.query(l,r) <= k) << '\n';
        // cerr << "---" << endl;
    }
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int testcase = 1;//cin >> testcase;
    while(testcase--)solve();
}

Compilation message

sortbooks.cpp: In function 'void solve()':
sortbooks.cpp:123:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  123 |     while(arr.size() < n){
      |           ~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74076 KB Output is correct
2 Correct 17 ms 74076 KB Output is correct
3 Correct 18 ms 74336 KB Output is correct
4 Correct 16 ms 74332 KB Output is correct
5 Correct 17 ms 74296 KB Output is correct
6 Correct 17 ms 74340 KB Output is correct
7 Correct 17 ms 74332 KB Output is correct
8 Correct 17 ms 74328 KB Output is correct
9 Correct 16 ms 74332 KB Output is correct
10 Correct 17 ms 74332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74076 KB Output is correct
2 Correct 17 ms 74076 KB Output is correct
3 Correct 18 ms 74336 KB Output is correct
4 Correct 16 ms 74332 KB Output is correct
5 Correct 17 ms 74296 KB Output is correct
6 Correct 17 ms 74340 KB Output is correct
7 Correct 17 ms 74332 KB Output is correct
8 Correct 17 ms 74328 KB Output is correct
9 Correct 16 ms 74332 KB Output is correct
10 Correct 17 ms 74332 KB Output is correct
11 Correct 18 ms 74596 KB Output is correct
12 Correct 21 ms 75100 KB Output is correct
13 Correct 21 ms 75100 KB Output is correct
14 Correct 21 ms 75352 KB Output is correct
15 Correct 22 ms 75352 KB Output is correct
16 Correct 20 ms 75096 KB Output is correct
17 Correct 20 ms 75100 KB Output is correct
18 Correct 20 ms 75352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2517 ms 251480 KB Output is correct
2 Correct 2508 ms 254268 KB Output is correct
3 Correct 2597 ms 253508 KB Output is correct
4 Correct 2513 ms 252824 KB Output is correct
5 Correct 2154 ms 253424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 92760 KB Output is correct
2 Correct 134 ms 92728 KB Output is correct
3 Correct 155 ms 92860 KB Output is correct
4 Correct 126 ms 92868 KB Output is correct
5 Correct 170 ms 92860 KB Output is correct
6 Correct 103 ms 92648 KB Output is correct
7 Correct 103 ms 92612 KB Output is correct
8 Correct 119 ms 92596 KB Output is correct
9 Correct 47 ms 75856 KB Output is correct
10 Correct 127 ms 92596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74076 KB Output is correct
2 Correct 17 ms 74076 KB Output is correct
3 Correct 18 ms 74336 KB Output is correct
4 Correct 16 ms 74332 KB Output is correct
5 Correct 17 ms 74296 KB Output is correct
6 Correct 17 ms 74340 KB Output is correct
7 Correct 17 ms 74332 KB Output is correct
8 Correct 17 ms 74328 KB Output is correct
9 Correct 16 ms 74332 KB Output is correct
10 Correct 17 ms 74332 KB Output is correct
11 Correct 18 ms 74596 KB Output is correct
12 Correct 21 ms 75100 KB Output is correct
13 Correct 21 ms 75100 KB Output is correct
14 Correct 21 ms 75352 KB Output is correct
15 Correct 22 ms 75352 KB Output is correct
16 Correct 20 ms 75096 KB Output is correct
17 Correct 20 ms 75100 KB Output is correct
18 Correct 20 ms 75352 KB Output is correct
19 Correct 384 ms 114052 KB Output is correct
20 Correct 391 ms 114536 KB Output is correct
21 Correct 273 ms 114076 KB Output is correct
22 Correct 270 ms 113984 KB Output is correct
23 Correct 274 ms 114100 KB Output is correct
24 Correct 224 ms 113728 KB Output is correct
25 Correct 224 ms 113848 KB Output is correct
26 Correct 309 ms 114048 KB Output is correct
27 Correct 311 ms 114176 KB Output is correct
28 Correct 323 ms 114132 KB Output is correct
29 Correct 317 ms 113984 KB Output is correct
30 Correct 342 ms 114188 KB Output is correct
31 Correct 324 ms 114164 KB Output is correct
32 Correct 344 ms 114380 KB Output is correct
33 Correct 359 ms 114260 KB Output is correct
34 Correct 208 ms 113832 KB Output is correct
35 Correct 212 ms 113768 KB Output is correct
36 Correct 217 ms 113832 KB Output is correct
37 Correct 210 ms 113828 KB Output is correct
38 Correct 209 ms 113776 KB Output is correct
39 Correct 290 ms 113368 KB Output is correct
40 Correct 203 ms 112492 KB Output is correct
41 Correct 284 ms 113016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74076 KB Output is correct
2 Correct 17 ms 74076 KB Output is correct
3 Correct 18 ms 74336 KB Output is correct
4 Correct 16 ms 74332 KB Output is correct
5 Correct 17 ms 74296 KB Output is correct
6 Correct 17 ms 74340 KB Output is correct
7 Correct 17 ms 74332 KB Output is correct
8 Correct 17 ms 74328 KB Output is correct
9 Correct 16 ms 74332 KB Output is correct
10 Correct 17 ms 74332 KB Output is correct
11 Correct 18 ms 74596 KB Output is correct
12 Correct 21 ms 75100 KB Output is correct
13 Correct 21 ms 75100 KB Output is correct
14 Correct 21 ms 75352 KB Output is correct
15 Correct 22 ms 75352 KB Output is correct
16 Correct 20 ms 75096 KB Output is correct
17 Correct 20 ms 75100 KB Output is correct
18 Correct 20 ms 75352 KB Output is correct
19 Correct 2517 ms 251480 KB Output is correct
20 Correct 2508 ms 254268 KB Output is correct
21 Correct 2597 ms 253508 KB Output is correct
22 Correct 2513 ms 252824 KB Output is correct
23 Correct 2154 ms 253424 KB Output is correct
24 Correct 151 ms 92760 KB Output is correct
25 Correct 134 ms 92728 KB Output is correct
26 Correct 155 ms 92860 KB Output is correct
27 Correct 126 ms 92868 KB Output is correct
28 Correct 170 ms 92860 KB Output is correct
29 Correct 103 ms 92648 KB Output is correct
30 Correct 103 ms 92612 KB Output is correct
31 Correct 119 ms 92596 KB Output is correct
32 Correct 47 ms 75856 KB Output is correct
33 Correct 127 ms 92596 KB Output is correct
34 Correct 384 ms 114052 KB Output is correct
35 Correct 391 ms 114536 KB Output is correct
36 Correct 273 ms 114076 KB Output is correct
37 Correct 270 ms 113984 KB Output is correct
38 Correct 274 ms 114100 KB Output is correct
39 Correct 224 ms 113728 KB Output is correct
40 Correct 224 ms 113848 KB Output is correct
41 Correct 309 ms 114048 KB Output is correct
42 Correct 311 ms 114176 KB Output is correct
43 Correct 323 ms 114132 KB Output is correct
44 Correct 317 ms 113984 KB Output is correct
45 Correct 342 ms 114188 KB Output is correct
46 Correct 324 ms 114164 KB Output is correct
47 Correct 344 ms 114380 KB Output is correct
48 Correct 359 ms 114260 KB Output is correct
49 Correct 208 ms 113832 KB Output is correct
50 Correct 212 ms 113768 KB Output is correct
51 Correct 217 ms 113832 KB Output is correct
52 Correct 210 ms 113828 KB Output is correct
53 Correct 209 ms 113776 KB Output is correct
54 Correct 290 ms 113368 KB Output is correct
55 Correct 203 ms 112492 KB Output is correct
56 Correct 284 ms 113016 KB Output is correct
57 Correct 2432 ms 253532 KB Output is correct
58 Correct 2502 ms 254656 KB Output is correct
59 Correct 2345 ms 253820 KB Output is correct
60 Correct 2371 ms 253276 KB Output is correct
61 Correct 2401 ms 254276 KB Output is correct
62 Correct 2364 ms 254672 KB Output is correct
63 Correct 1110 ms 252940 KB Output is correct
64 Correct 1129 ms 252076 KB Output is correct
65 Correct 2062 ms 253832 KB Output is correct
66 Correct 2020 ms 254940 KB Output is correct
67 Correct 2054 ms 254384 KB Output is correct
68 Correct 2110 ms 254944 KB Output is correct
69 Correct 2080 ms 254064 KB Output is correct
70 Correct 2149 ms 254988 KB Output is correct
71 Correct 2146 ms 255228 KB Output is correct
72 Correct 2168 ms 254676 KB Output is correct
73 Correct 1044 ms 250460 KB Output is correct
74 Correct 1064 ms 251488 KB Output is correct
75 Correct 1087 ms 251300 KB Output is correct
76 Correct 1050 ms 251444 KB Output is correct
77 Correct 1031 ms 251476 KB Output is correct
78 Correct 1868 ms 247592 KB Output is correct
79 Correct 1207 ms 242488 KB Output is correct
80 Correct 1770 ms 245732 KB Output is correct