답안 #876468

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
876468 2023-11-21T19:21:05 Z danikoynov Fish 2 (JOI22_fish2) C++14
36 / 100
4000 ms 23752 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 10;


int n, q;
ll a[maxn], pref[maxn];
void input()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
        cin >> a[i];
    cin >> q;
    a[0] = 1e9 + 10;
    a[n + 1] = 1e9 + 10;

}

vector < pair < int, int > > ranges;
void get_ranges()
{

    for (int i = 1; i <= n; i ++)
        pref[i] = pref[i - 1] + a[i];
    pref[n + 1] = pref[n];

    ranges.clear();
    stack < int > st;
    st.push(0);
    for (int i = 1; i <= n; i ++)
    {
        while(!st.empty() && a[st.top()] < a[i])
            st.pop();


        if (pref[i - 1] - pref[st.top()] < a[i])
            ranges.push_back({st.top() + 1, i - 1});

        st.push(i);
    }

    while(!st.empty())
        st.pop();
    st.push(n + 1);
    for (int i = n; i > 0; i --)
    {
        while(!st.empty() && a[st.top()] < a[i])
            st.pop();

        if (pref[st.top() - 1] - pref[i] < a[i])
            ranges.push_back({i + 1, st.top() - 1});

        st.push(i);
    }
}

int b[maxn];

struct node
{
    int cnt, mx;

    node(int _cnt = 0, int _mx = 1e9 + 10)
    {
        cnt = _cnt;
        mx = _mx;
    }
};

node tree[4 * maxn];

node merge_node(node lf, node rf)
{
    if (lf.cnt == -1 || rf.mx < lf.mx)
        return rf;
    if (rf.cnt == -1 || lf.mx < rf.mx)
        return lf;

    return node(lf.cnt + rf.cnt, lf.mx);
}

void build_tree(int root, int left, int right)
{
    if (left == right)
    {
        tree[root].mx = b[left];
        tree[root].cnt = 1;
        return;
    }

    int mid = (left + right) / 2;
    build_tree(root * 2, left, mid);
    build_tree(root * 2 + 1, mid + 1, right);

    tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}

node query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return node(-1, 1e9 + 10);

    if (left >= qleft && right <= qright)
        return tree[root];

    int mid = (left + right) / 2;

    return merge_node(query(root * 2, left, mid, qleft, qright),
            query(root * 2 + 1, mid + 1, right, qleft, qright));
}

ll values[maxn];

struct segment_tree
{
    ll tree[4 * maxn], lazy[4 * maxn];


    void build_tree(int root, int left, int right)
    {
        lazy[root] = 0;
        if (left == right)
        {
            tree[root] = values[left];
            return;
        }

        int mid = (left + right) / 2;
        build_tree(root * 2, left, mid);
        build_tree(root * 2 + 1, mid + 1, right);

        tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
    }

    void push_lazy(int root, int left, int right)
    {
        tree[root] += lazy[root];
        if (left != right)
        {
            lazy[root * 2] += lazy[root];
            lazy[root * 2 + 1] += lazy[root];
        }

        lazy[root] = 0;
    }

    void update_range(int root, int left, int right, int qleft, int qright, ll val)
    {
        push_lazy(root, left, right);
        if (left > qright || right < qleft)
            return;

        if (left >= qleft && right <= qright)
        {
            lazy[root] += val;
            push_lazy(root, left, right);
            return;
        }

        int mid = (left + right) / 2;
        update_range(root * 2, left, mid, qleft, qright, val);
        update_range(root * 2 + 1, mid + 1, right, qleft, qright, val);

        tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
    }

    ll walk_left(int root, int left, int right, int qleft, int qright, ll val)
    {
        push_lazy(root, left, right);
        if (left > qright || right < qleft || tree[root] <= val)
            return n + 1;

        if (left == right)
            return left;

        int mid = (left + right) / 2;
        if (left >= qleft && right <= qright)
        {
            if (tree[root * 2] > val)
                return walk_left(root * 2, left, mid, qleft, qright, val);
            return walk_left(root * 2 + 1, mid + 1, right, qleft, qright, val);
        }

        return min(walk_left(root * 2, left, mid, qleft, qright, val),
                walk_left(root * 2 + 1, mid + 1, right, qleft, qright, val));
    }

    ll walk_right(int root, int left, int right, int qleft, int qright, ll val)
    {
        push_lazy(root, left, right);
        if (left > qright || right < qleft || tree[root] <= val)
            return 0;

        if (left == right)
            return left;

        int mid = (left + right) / 2;
        if (left >= qleft && right <= qright)
        {
            if (tree[root * 2 + 1] > val)
                    return walk_right(root * 2 + 1, mid + 1, right, qleft, qright, val);
            return walk_right(root * 2, left, mid, qleft, qright, val);
        }

        return max(walk_right(root * 2, left, mid, qleft, qright, val),
                walk_right(root * 2 + 1, mid + 1, right, qleft, qright, val));
    }
};

segment_tree left_tree, right_tree;

ll fen[maxn];

void update_fen(int pos, ll val)
{
    for (int i = pos; i <= n; i += (i & -i))
        fen[i] += val;
}

ll query_fen(int pos)
{
    ll s = 0;
    for (int i = pos; i > 0; i -= (i & -i))
        s += fen[i];
    return s;
}

ll range_sum(int left, int right)
{
    return query_fen(right) - query_fen(left - 1);
}

void solve_query(int left, int right)
{



    int lb = left_tree.walk_right(1, 1, n, left, right, - query_fen(left - 1));
    int rb = right_tree.walk_left(1, 1, n, left, right, query_fen(right));


    cout << query(1, 1, n, lb, rb).cnt << endl;
}

void restructure()
{
    get_ranges();
    for (int i = 1; i <= n; i ++)
        b[i] = 0;
    for (pair < int, int > cur : ranges)
        for (int i = cur.first; i <= cur.second; i ++)
        b[i] ++;
    build_tree(1, 1, n);
    for (int i = 1; i <= n; i ++)
    {
        values[i] = a[i] - query_fen(i - 1);
    }
    left_tree.build_tree(1, 1, n);

    for (int i = 1; i <= n; i ++)
    {
        values[i] = a[i] + query_fen(i);
    }
    right_tree.build_tree(1, 1, n);
}
void simulate()
{
    for (int i = 1; i <= n; i ++)
        update_fen(i, a[i]);
    restructure();
    for (int i = 1; i <= q; i ++)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int idx;
            ll x;
            cin >> idx >> x;
            update_fen(idx, x - a[idx]);
            a[idx] = x;
            restructure();
        }
        else
        {
            int l, r;
            cin >> l >> r;
            solve_query(l, r);
        }
    }
}
void solve()
{
    input();
    simulate();
}

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main()
{
    speed();
    solve();
    return 0;
}
/*
12
32 32 4 1 1 1 1 4 4 16 32 128
1
2 8 10

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 10 ms 14860 KB Output is correct
6 Correct 6 ms 14940 KB Output is correct
7 Correct 10 ms 14684 KB Output is correct
8 Correct 6 ms 14684 KB Output is correct
9 Correct 5 ms 14820 KB Output is correct
10 Correct 8 ms 14684 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 11 ms 14680 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 11 ms 14860 KB Output is correct
15 Correct 8 ms 14684 KB Output is correct
16 Correct 4 ms 14684 KB Output is correct
17 Correct 9 ms 14860 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 8 ms 14692 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 16 ms 21708 KB Output is correct
3 Correct 15 ms 21460 KB Output is correct
4 Correct 15 ms 21716 KB Output is correct
5 Correct 15 ms 21564 KB Output is correct
6 Correct 18 ms 21824 KB Output is correct
7 Correct 14 ms 21140 KB Output is correct
8 Correct 18 ms 21716 KB Output is correct
9 Correct 14 ms 21092 KB Output is correct
10 Correct 16 ms 21716 KB Output is correct
11 Correct 15 ms 21460 KB Output is correct
12 Correct 15 ms 20948 KB Output is correct
13 Correct 15 ms 21096 KB Output is correct
14 Correct 15 ms 21716 KB Output is correct
15 Correct 17 ms 21720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 10 ms 14860 KB Output is correct
6 Correct 6 ms 14940 KB Output is correct
7 Correct 10 ms 14684 KB Output is correct
8 Correct 6 ms 14684 KB Output is correct
9 Correct 5 ms 14820 KB Output is correct
10 Correct 8 ms 14684 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 11 ms 14680 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 11 ms 14860 KB Output is correct
15 Correct 8 ms 14684 KB Output is correct
16 Correct 4 ms 14684 KB Output is correct
17 Correct 9 ms 14860 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 8 ms 14692 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 2 ms 14680 KB Output is correct
22 Correct 16 ms 21708 KB Output is correct
23 Correct 15 ms 21460 KB Output is correct
24 Correct 15 ms 21716 KB Output is correct
25 Correct 15 ms 21564 KB Output is correct
26 Correct 18 ms 21824 KB Output is correct
27 Correct 14 ms 21140 KB Output is correct
28 Correct 18 ms 21716 KB Output is correct
29 Correct 14 ms 21092 KB Output is correct
30 Correct 16 ms 21716 KB Output is correct
31 Correct 15 ms 21460 KB Output is correct
32 Correct 15 ms 20948 KB Output is correct
33 Correct 15 ms 21096 KB Output is correct
34 Correct 15 ms 21716 KB Output is correct
35 Correct 17 ms 21720 KB Output is correct
36 Correct 2625 ms 21964 KB Output is correct
37 Correct 1691 ms 21964 KB Output is correct
38 Correct 293 ms 21544 KB Output is correct
39 Correct 3980 ms 21712 KB Output is correct
40 Correct 298 ms 21544 KB Output is correct
41 Correct 1882 ms 21868 KB Output is correct
42 Correct 714 ms 21864 KB Output is correct
43 Correct 1831 ms 21236 KB Output is correct
44 Correct 611 ms 21220 KB Output is correct
45 Correct 2636 ms 21892 KB Output is correct
46 Correct 1636 ms 21960 KB Output is correct
47 Correct 296 ms 21440 KB Output is correct
48 Execution timed out 4085 ms 21204 KB Time limit exceeded
49 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 16 ms 21708 KB Output is correct
3 Correct 15 ms 21460 KB Output is correct
4 Correct 15 ms 21716 KB Output is correct
5 Correct 15 ms 21564 KB Output is correct
6 Correct 18 ms 21824 KB Output is correct
7 Correct 14 ms 21140 KB Output is correct
8 Correct 18 ms 21716 KB Output is correct
9 Correct 14 ms 21092 KB Output is correct
10 Correct 16 ms 21716 KB Output is correct
11 Correct 15 ms 21460 KB Output is correct
12 Correct 15 ms 20948 KB Output is correct
13 Correct 15 ms 21096 KB Output is correct
14 Correct 15 ms 21716 KB Output is correct
15 Correct 17 ms 21720 KB Output is correct
16 Correct 2 ms 14684 KB Output is correct
17 Correct 144 ms 23240 KB Output is correct
18 Correct 150 ms 23700 KB Output is correct
19 Correct 142 ms 23432 KB Output is correct
20 Correct 147 ms 23340 KB Output is correct
21 Correct 142 ms 23240 KB Output is correct
22 Correct 134 ms 23684 KB Output is correct
23 Correct 143 ms 23284 KB Output is correct
24 Correct 148 ms 23272 KB Output is correct
25 Correct 151 ms 23752 KB Output is correct
26 Correct 152 ms 23244 KB Output is correct
27 Correct 136 ms 23752 KB Output is correct
28 Correct 130 ms 23528 KB Output is correct
29 Correct 129 ms 23752 KB Output is correct
30 Correct 141 ms 22732 KB Output is correct
31 Correct 143 ms 22728 KB Output is correct
32 Correct 167 ms 23436 KB Output is correct
33 Correct 134 ms 23492 KB Output is correct
34 Correct 163 ms 23112 KB Output is correct
35 Correct 154 ms 23284 KB Output is correct
36 Correct 140 ms 23496 KB Output is correct
37 Correct 118 ms 22724 KB Output is correct
38 Correct 115 ms 22544 KB Output is correct
39 Correct 130 ms 23448 KB Output is correct
40 Correct 120 ms 23552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 16 ms 21708 KB Output is correct
3 Correct 15 ms 21460 KB Output is correct
4 Correct 15 ms 21716 KB Output is correct
5 Correct 15 ms 21564 KB Output is correct
6 Correct 18 ms 21824 KB Output is correct
7 Correct 14 ms 21140 KB Output is correct
8 Correct 18 ms 21716 KB Output is correct
9 Correct 14 ms 21092 KB Output is correct
10 Correct 16 ms 21716 KB Output is correct
11 Correct 15 ms 21460 KB Output is correct
12 Correct 15 ms 20948 KB Output is correct
13 Correct 15 ms 21096 KB Output is correct
14 Correct 15 ms 21716 KB Output is correct
15 Correct 17 ms 21720 KB Output is correct
16 Correct 2 ms 14684 KB Output is correct
17 Execution timed out 4070 ms 21876 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 10 ms 14860 KB Output is correct
6 Correct 6 ms 14940 KB Output is correct
7 Correct 10 ms 14684 KB Output is correct
8 Correct 6 ms 14684 KB Output is correct
9 Correct 5 ms 14820 KB Output is correct
10 Correct 8 ms 14684 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 11 ms 14680 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 11 ms 14860 KB Output is correct
15 Correct 8 ms 14684 KB Output is correct
16 Correct 4 ms 14684 KB Output is correct
17 Correct 9 ms 14860 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 8 ms 14692 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 2 ms 14680 KB Output is correct
22 Correct 16 ms 21708 KB Output is correct
23 Correct 15 ms 21460 KB Output is correct
24 Correct 15 ms 21716 KB Output is correct
25 Correct 15 ms 21564 KB Output is correct
26 Correct 18 ms 21824 KB Output is correct
27 Correct 14 ms 21140 KB Output is correct
28 Correct 18 ms 21716 KB Output is correct
29 Correct 14 ms 21092 KB Output is correct
30 Correct 16 ms 21716 KB Output is correct
31 Correct 15 ms 21460 KB Output is correct
32 Correct 15 ms 20948 KB Output is correct
33 Correct 15 ms 21096 KB Output is correct
34 Correct 15 ms 21716 KB Output is correct
35 Correct 17 ms 21720 KB Output is correct
36 Correct 2625 ms 21964 KB Output is correct
37 Correct 1691 ms 21964 KB Output is correct
38 Correct 293 ms 21544 KB Output is correct
39 Correct 3980 ms 21712 KB Output is correct
40 Correct 298 ms 21544 KB Output is correct
41 Correct 1882 ms 21868 KB Output is correct
42 Correct 714 ms 21864 KB Output is correct
43 Correct 1831 ms 21236 KB Output is correct
44 Correct 611 ms 21220 KB Output is correct
45 Correct 2636 ms 21892 KB Output is correct
46 Correct 1636 ms 21960 KB Output is correct
47 Correct 296 ms 21440 KB Output is correct
48 Execution timed out 4085 ms 21204 KB Time limit exceeded
49 Halted 0 ms 0 KB -