답안 #331118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331118 2020-11-27T12:27:13 Z Falcon Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
2519 ms 119496 KB
#pragma GCC optimize("O2")

#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d)    for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }
#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }



namespace segtree {

struct node {
    int max_a{}, v{}, lzy{-1};
};

vector<node> seg;

void push(int n, int s, int e) {
    if(seg[n].lzy == -1)
        return;

    seg[n].v = seg[n].max_a + seg[n].lzy;
    
    if(s != e)
        seg[n << 1].lzy = seg[n << 1 | 1].lzy = seg[n].lzy;
    
    seg[n].lzy = -1;
}

void update(int n, int s, int e, int l, int r, int v) {
    push(n, s, e);
    if(r < s || e < l)
        return;

    if(l <= s && e <= r) {
        seg[n].lzy = v;
        push(n, s, e);
        return;
    }

    int m{(s + e) >> 1};
    update(n << 1, s, m, l, r, v); update(n << 1 | 1, m + 1, e, l, r, v);
    seg[n].v = max(seg[n << 1].v, seg[n << 1 | 1].v);
}

void add_front(int n, int s, int e, int i, int v) {
    push(n, s, e);
    if(e < i || i < s)
        return;

    if(s == e) {
        assert(seg[n].max_a == 0 && seg[n].v == 0);
        seg[n].max_a = v;
        return;        
    }

    int m{(s + e) >> 1};
    add_front(n << 1, s, m, i, v), add_front(n << 1 | 1, m + 1, e, i, v);
    seg[n].v = max(seg[n << 1].v, seg[n << 1 | 1].v);
    seg[n].max_a = max(seg[n << 1].max_a, seg[n << 1 | 1].max_a);
}

int query(int n, int s, int e, int l, int r) {
    push(n, s, e);
    if(r < s || e < l)
        return 0;

    if(l <= s && e <= r)
        return seg[n].v;

    int m{(s + e) >> 1};
    return max(query(n << 1, s, m, l, r), query(n << 1 | 1, m + 1, e, l, r));
}

} // namespace segtree


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    #ifdef DEBUG
    freopen("debug", "w", stderr);
    #endif
    
    int N, M; cin >> N >> M;
    vector<int> A(N); segtree::seg.resize(N << 2);
    rep(i, N) cin >> A[i];

    struct query { int i, r, k; };

    vector<vector<query>> queries(N);
    vector<bool> ans(M);
    rep(i, M) {
        int l, r, k; cin >> l >> r >> k; --l, --r;
        queries[l].push_back({i, r, k});
    }
    
    A.push_back(*max_element(all(A)) + 1);
    stack<int> max_vals; max_vals.push(N);
    rrep(i, N) {
        while(!max_vals.empty() && A[max_vals.top()] < A[i])
            max_vals.pop();
        int e = max_vals.top() - 1;
        debug(i, e);
        if(e > i) segtree::update(1, 0, N - 1, i + 1, e, A[i]);
        for(const query& q : queries[i]) {
            int x{segtree::query(1, 0, N - 1, i, q.r)};
            debug(x, q.i);
            ans[q.i] = x <= q.k;

        }
        segtree::add_front(1, 0, N - 1, i, A[i]);
        max_vals.push(i);
    }

    for(const auto& b : ans) cout << b << '\n';

    #ifdef DEBUG
    dbg::dout << "\nExecution time: " << clock() * 1000 / CLOCKS_PER_SEC  << "ms" << endl;
    #endif

    return 0;
}


Compilation message

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:35:29: warning: statement has no effect [-Wunused-value]
   35 | #define debug(x...)         42
      |                             ^~
sortbooks.cpp:145:9: note: in expansion of macro 'debug'
  145 |         debug(i, e);
      |         ^~~~~
sortbooks.cpp:35:29: warning: statement has no effect [-Wunused-value]
   35 | #define debug(x...)         42
      |                             ^~
sortbooks.cpp:149:13: note: in expansion of macro 'debug'
  149 |             debug(x, q.i);
      |             ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 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 0 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 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 4 ms 748 KB Output is correct
12 Correct 6 ms 896 KB Output is correct
13 Correct 6 ms 876 KB Output is correct
14 Correct 8 ms 1004 KB Output is correct
15 Correct 8 ms 1004 KB Output is correct
16 Correct 7 ms 1004 KB Output is correct
17 Correct 5 ms 876 KB Output is correct
18 Correct 6 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2519 ms 100500 KB Output is correct
2 Correct 2519 ms 102252 KB Output is correct
3 Correct 2471 ms 102224 KB Output is correct
4 Correct 2499 ms 102252 KB Output is correct
5 Correct 2219 ms 107368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 10476 KB Output is correct
2 Correct 170 ms 12140 KB Output is correct
3 Correct 160 ms 12516 KB Output is correct
4 Correct 164 ms 12644 KB Output is correct
5 Correct 162 ms 12644 KB Output is correct
6 Correct 133 ms 11876 KB Output is correct
7 Correct 131 ms 11876 KB Output is correct
8 Correct 133 ms 11688 KB Output is correct
9 Correct 54 ms 3492 KB Output is correct
10 Correct 142 ms 11688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 4 ms 748 KB Output is correct
12 Correct 6 ms 896 KB Output is correct
13 Correct 6 ms 876 KB Output is correct
14 Correct 8 ms 1004 KB Output is correct
15 Correct 8 ms 1004 KB Output is correct
16 Correct 7 ms 1004 KB Output is correct
17 Correct 5 ms 876 KB Output is correct
18 Correct 6 ms 876 KB Output is correct
19 Correct 429 ms 27196 KB Output is correct
20 Correct 418 ms 27116 KB Output is correct
21 Correct 371 ms 26476 KB Output is correct
22 Correct 382 ms 26476 KB Output is correct
23 Correct 374 ms 26476 KB Output is correct
24 Correct 329 ms 26972 KB Output is correct
25 Correct 318 ms 26844 KB Output is correct
26 Correct 352 ms 27100 KB Output is correct
27 Correct 364 ms 27100 KB Output is correct
28 Correct 364 ms 27228 KB Output is correct
29 Correct 382 ms 27612 KB Output is correct
30 Correct 385 ms 27612 KB Output is correct
31 Correct 377 ms 27484 KB Output is correct
32 Correct 381 ms 27612 KB Output is correct
33 Correct 388 ms 27484 KB Output is correct
34 Correct 301 ms 25820 KB Output is correct
35 Correct 301 ms 25820 KB Output is correct
36 Correct 291 ms 25692 KB Output is correct
37 Correct 293 ms 25564 KB Output is correct
38 Correct 300 ms 25820 KB Output is correct
39 Correct 331 ms 25108 KB Output is correct
40 Correct 229 ms 18856 KB Output is correct
41 Correct 305 ms 24356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 4 ms 748 KB Output is correct
12 Correct 6 ms 896 KB Output is correct
13 Correct 6 ms 876 KB Output is correct
14 Correct 8 ms 1004 KB Output is correct
15 Correct 8 ms 1004 KB Output is correct
16 Correct 7 ms 1004 KB Output is correct
17 Correct 5 ms 876 KB Output is correct
18 Correct 6 ms 876 KB Output is correct
19 Correct 2519 ms 100500 KB Output is correct
20 Correct 2519 ms 102252 KB Output is correct
21 Correct 2471 ms 102224 KB Output is correct
22 Correct 2499 ms 102252 KB Output is correct
23 Correct 2219 ms 107368 KB Output is correct
24 Correct 199 ms 10476 KB Output is correct
25 Correct 170 ms 12140 KB Output is correct
26 Correct 160 ms 12516 KB Output is correct
27 Correct 164 ms 12644 KB Output is correct
28 Correct 162 ms 12644 KB Output is correct
29 Correct 133 ms 11876 KB Output is correct
30 Correct 131 ms 11876 KB Output is correct
31 Correct 133 ms 11688 KB Output is correct
32 Correct 54 ms 3492 KB Output is correct
33 Correct 142 ms 11688 KB Output is correct
34 Correct 429 ms 27196 KB Output is correct
35 Correct 418 ms 27116 KB Output is correct
36 Correct 371 ms 26476 KB Output is correct
37 Correct 382 ms 26476 KB Output is correct
38 Correct 374 ms 26476 KB Output is correct
39 Correct 329 ms 26972 KB Output is correct
40 Correct 318 ms 26844 KB Output is correct
41 Correct 352 ms 27100 KB Output is correct
42 Correct 364 ms 27100 KB Output is correct
43 Correct 364 ms 27228 KB Output is correct
44 Correct 382 ms 27612 KB Output is correct
45 Correct 385 ms 27612 KB Output is correct
46 Correct 377 ms 27484 KB Output is correct
47 Correct 381 ms 27612 KB Output is correct
48 Correct 388 ms 27484 KB Output is correct
49 Correct 301 ms 25820 KB Output is correct
50 Correct 301 ms 25820 KB Output is correct
51 Correct 291 ms 25692 KB Output is correct
52 Correct 293 ms 25564 KB Output is correct
53 Correct 300 ms 25820 KB Output is correct
54 Correct 331 ms 25108 KB Output is correct
55 Correct 229 ms 18856 KB Output is correct
56 Correct 305 ms 24356 KB Output is correct
57 Correct 2480 ms 103276 KB Output is correct
58 Correct 2481 ms 103432 KB Output is correct
59 Correct 2348 ms 101484 KB Output is correct
60 Correct 2394 ms 101484 KB Output is correct
61 Correct 2338 ms 101760 KB Output is correct
62 Correct 2341 ms 101612 KB Output is correct
63 Correct 1670 ms 98764 KB Output is correct
64 Correct 1683 ms 99036 KB Output is correct
65 Correct 2099 ms 103356 KB Output is correct
66 Correct 2092 ms 103344 KB Output is correct
67 Correct 2095 ms 103972 KB Output is correct
68 Correct 2184 ms 105636 KB Output is correct
69 Correct 2187 ms 105764 KB Output is correct
70 Correct 2223 ms 104928 KB Output is correct
71 Correct 2195 ms 112300 KB Output is correct
72 Correct 2201 ms 105252 KB Output is correct
73 Correct 1560 ms 96696 KB Output is correct
74 Correct 1614 ms 96404 KB Output is correct
75 Correct 1566 ms 96876 KB Output is correct
76 Correct 1613 ms 96676 KB Output is correct
77 Correct 1560 ms 96748 KB Output is correct
78 Correct 1909 ms 100184 KB Output is correct
79 Correct 1197 ms 64524 KB Output is correct
80 Correct 1799 ms 119496 KB Output is correct