답안 #1112676

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1112676 2024-11-14T14:33:02 Z Zero_OP Fish 2 (JOI22_fish2) C++14
25 / 100
4000 ms 5044 KB
#include <bits/stdc++.h>

using namespace std;

template<typename T>
struct segment_tree{
    vector<T> st;
    int n;

    segment_tree(int n) : n(n), st(n * 2 + 1) {}

    void update(int i, T v){
        st[i += n] = v;
        for(; i > 1; i >>= 1){
            st[i >> 1] = max(st[i], st[i ^ 1]);
        }
    }

    T query(int l, int r){
         l += n;
         r += n + 1;
         T res = {0, 0};

         for(; l < r; l >>= 1, r >>= 1){
            if(l & 1) res = max(res, st[l++]);
            if(r & 1) res = max(res, st[--r]);
         }
         return res;
    }
};

template<typename T>
struct fenwick_tree{
    vector<T> bit;
    static constexpr int offset = 1;
    fenwick_tree(int n) : bit(n + 1, 0) {}

    void update(int id, T val){
        id += offset;
        for(; id < (int)bit.size(); id += id & (-id)){
            bit[id] += val;
        }
    }

    T query(int l, int r){
        T res(0);
        for(; l > 0; l -= l & (-l)) res -= bit[l];
        for(++r; r > 0; r -= r & (-r)) res += bit[r];
        return res;
    }
};

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

    int N;
    cin >> N;
    vector<int> a(N);
    segment_tree<pair<int, int>> st(N);
    fenwick_tree<long long> ft(N);

    for(int i = 0; i < N; ++i){
        cin >> a[i];
        st.update(i, {a[i], i});
        ft.update(i, a[i]);
    }

    function<int(int, int)> f = [&](int l, int r){
        if(l == r) return 1;

        int mx, pos;
        tie(mx, pos) = st.query(l, r);

        int ans = 1;
        if(l < pos && ft.query(l, pos - 1) >= mx) ans += f(l, pos - 1);
        if(pos < r && ft.query(pos + 1, r) >= mx) ans += f(pos + 1, r);
        return ans;
    };

    int Q;
    cin >> Q;
    while(Q--){
        int type;
        cin >> type;
        if(type == 1){
            int i, v;
            cin >> i >> v;
            --i;
            st.update(i, {v, i});
            ft.update(i, -a[i] + v);
            a[i] = v;
        } else{
            int l, r;
            cin >> l >> r;
            --l, --r;
            cout << f(l, r) << '\n';
        }
    }

    return 0;
}

Compilation message

fish2.cpp: In instantiation of 'segment_tree<T>::segment_tree(int) [with T = std::pair<int, int>]':
fish2.cpp:60:38:   required from here
fish2.cpp:8:9: warning: 'segment_tree<std::pair<int, int> >::n' will be initialized after [-Wreorder]
    8 |     int n;
      |         ^
fish2.cpp:7:15: warning:   'std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > segment_tree<std::pair<int, int> >::st' [-Wreorder]
    7 |     vector<T> st;
      |               ^~
fish2.cpp:10:5: warning:   when initialized here [-Wreorder]
   10 |     segment_tree(int n) : n(n), st(n * 2 + 1) {}
      |     ^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 472 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 15 ms 3664 KB Output is correct
3 Correct 14 ms 3408 KB Output is correct
4 Correct 16 ms 3664 KB Output is correct
5 Correct 15 ms 3408 KB Output is correct
6 Correct 21 ms 3920 KB Output is correct
7 Correct 14 ms 3408 KB Output is correct
8 Correct 23 ms 4080 KB Output is correct
9 Correct 13 ms 3416 KB Output is correct
10 Correct 16 ms 3664 KB Output is correct
11 Correct 15 ms 3500 KB Output is correct
12 Correct 14 ms 3576 KB Output is correct
13 Correct 15 ms 3408 KB Output is correct
14 Correct 19 ms 3920 KB Output is correct
15 Correct 20 ms 4184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 472 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 15 ms 3664 KB Output is correct
23 Correct 14 ms 3408 KB Output is correct
24 Correct 16 ms 3664 KB Output is correct
25 Correct 15 ms 3408 KB Output is correct
26 Correct 21 ms 3920 KB Output is correct
27 Correct 14 ms 3408 KB Output is correct
28 Correct 23 ms 4080 KB Output is correct
29 Correct 13 ms 3416 KB Output is correct
30 Correct 16 ms 3664 KB Output is correct
31 Correct 15 ms 3500 KB Output is correct
32 Correct 14 ms 3576 KB Output is correct
33 Correct 15 ms 3408 KB Output is correct
34 Correct 19 ms 3920 KB Output is correct
35 Correct 20 ms 4184 KB Output is correct
36 Correct 75 ms 3920 KB Output is correct
37 Correct 30 ms 3408 KB Output is correct
38 Correct 26 ms 3420 KB Output is correct
39 Correct 44 ms 3664 KB Output is correct
40 Correct 23 ms 3576 KB Output is correct
41 Correct 962 ms 4176 KB Output is correct
42 Correct 1209 ms 4176 KB Output is correct
43 Correct 17 ms 3408 KB Output is correct
44 Correct 16 ms 3284 KB Output is correct
45 Correct 108 ms 3768 KB Output is correct
46 Correct 132 ms 3776 KB Output is correct
47 Correct 18 ms 3496 KB Output is correct
48 Correct 18 ms 3576 KB Output is correct
49 Correct 29 ms 3408 KB Output is correct
50 Correct 441 ms 4064 KB Output is correct
51 Correct 1798 ms 4168 KB Output is correct
52 Correct 2121 ms 4184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 15 ms 3664 KB Output is correct
3 Correct 14 ms 3408 KB Output is correct
4 Correct 16 ms 3664 KB Output is correct
5 Correct 15 ms 3408 KB Output is correct
6 Correct 21 ms 3920 KB Output is correct
7 Correct 14 ms 3408 KB Output is correct
8 Correct 23 ms 4080 KB Output is correct
9 Correct 13 ms 3416 KB Output is correct
10 Correct 16 ms 3664 KB Output is correct
11 Correct 15 ms 3500 KB Output is correct
12 Correct 14 ms 3576 KB Output is correct
13 Correct 15 ms 3408 KB Output is correct
14 Correct 19 ms 3920 KB Output is correct
15 Correct 20 ms 4184 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1018 ms 5044 KB Output is correct
18 Execution timed out 4070 ms 4716 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 15 ms 3664 KB Output is correct
3 Correct 14 ms 3408 KB Output is correct
4 Correct 16 ms 3664 KB Output is correct
5 Correct 15 ms 3408 KB Output is correct
6 Correct 21 ms 3920 KB Output is correct
7 Correct 14 ms 3408 KB Output is correct
8 Correct 23 ms 4080 KB Output is correct
9 Correct 13 ms 3416 KB Output is correct
10 Correct 16 ms 3664 KB Output is correct
11 Correct 15 ms 3500 KB Output is correct
12 Correct 14 ms 3576 KB Output is correct
13 Correct 15 ms 3408 KB Output is correct
14 Correct 19 ms 3920 KB Output is correct
15 Correct 20 ms 4184 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Execution timed out 4038 ms 4200 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 472 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 15 ms 3664 KB Output is correct
23 Correct 14 ms 3408 KB Output is correct
24 Correct 16 ms 3664 KB Output is correct
25 Correct 15 ms 3408 KB Output is correct
26 Correct 21 ms 3920 KB Output is correct
27 Correct 14 ms 3408 KB Output is correct
28 Correct 23 ms 4080 KB Output is correct
29 Correct 13 ms 3416 KB Output is correct
30 Correct 16 ms 3664 KB Output is correct
31 Correct 15 ms 3500 KB Output is correct
32 Correct 14 ms 3576 KB Output is correct
33 Correct 15 ms 3408 KB Output is correct
34 Correct 19 ms 3920 KB Output is correct
35 Correct 20 ms 4184 KB Output is correct
36 Correct 75 ms 3920 KB Output is correct
37 Correct 30 ms 3408 KB Output is correct
38 Correct 26 ms 3420 KB Output is correct
39 Correct 44 ms 3664 KB Output is correct
40 Correct 23 ms 3576 KB Output is correct
41 Correct 962 ms 4176 KB Output is correct
42 Correct 1209 ms 4176 KB Output is correct
43 Correct 17 ms 3408 KB Output is correct
44 Correct 16 ms 3284 KB Output is correct
45 Correct 108 ms 3768 KB Output is correct
46 Correct 132 ms 3776 KB Output is correct
47 Correct 18 ms 3496 KB Output is correct
48 Correct 18 ms 3576 KB Output is correct
49 Correct 29 ms 3408 KB Output is correct
50 Correct 441 ms 4064 KB Output is correct
51 Correct 1798 ms 4168 KB Output is correct
52 Correct 2121 ms 4184 KB Output is correct
53 Correct 1 ms 336 KB Output is correct
54 Correct 1018 ms 5044 KB Output is correct
55 Execution timed out 4070 ms 4716 KB Time limit exceeded
56 Halted 0 ms 0 KB -