Submission #876332

# Submission time Handle Problem Language Result Execution time Memory
876332 2023-11-21T14:41:48 Z danikoynov Fish 2 (JOI22_fish2) C++14
25 / 100
4000 ms 16784 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;

void solve_query(int left, int right)
{

    //for (pair < int, int > cur : ranges)
      //  cout << cur.first << " : " << cur.second << endl;


    int lb = left, rb = right;
    for (int i = left; i <= right; i ++)
    {
        //if (a[i] > pref[i - 1] - pref[left - 1])
          //  lb = max(lb, i);
        if (a[i] > pref[right] - pref[i])
            rb = min(rb, i);
    }
    lb = left_tree.walk_right(1, 1, n, left, right, - pref[left - 1]);

    /**cout << lb << " " << a[left + 2] - pref[left + 1] << " " << " " << -pref[left - 1] << endl;
    for (int i = 1; i <= n; i ++)
        cout << pref[i] << " ";
    cout << endl;*/

    /**int minx = 1e9;
    for (int i = lb; i <= rb; i ++)
        minx = min(minx, b[i]);

    int ans = 0;
    for (int i = lb; i <= rb; i ++)
        if (b[i] == minx)
        ans ++;*/

    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] - pref[i - 1];
    }
    left_tree.build_tree(1, 1, n);
}
void simulate()
{
    restructure();
    for (int i = 1; i <= q; i ++)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int idx;
            ll x;
            cin >> idx >> x;
            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

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 8 ms 8536 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 8 ms 8540 KB Output is correct
8 Correct 5 ms 8540 KB Output is correct
9 Correct 3 ms 8540 KB Output is correct
10 Correct 6 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 8 ms 8540 KB Output is correct
13 Correct 3 ms 8536 KB Output is correct
14 Correct 8 ms 8536 KB Output is correct
15 Correct 6 ms 8540 KB Output is correct
16 Correct 3 ms 8536 KB Output is correct
17 Correct 7 ms 8676 KB Output is correct
18 Correct 2 ms 8668 KB Output is correct
19 Correct 6 ms 8728 KB Output is correct
20 Correct 3 ms 8716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 13 ms 14036 KB Output is correct
3 Correct 12 ms 14060 KB Output is correct
4 Correct 14 ms 14036 KB Output is correct
5 Correct 16 ms 14036 KB Output is correct
6 Correct 17 ms 13816 KB Output is correct
7 Correct 12 ms 13776 KB Output is correct
8 Correct 15 ms 13728 KB Output is correct
9 Correct 12 ms 13780 KB Output is correct
10 Correct 13 ms 14132 KB Output is correct
11 Correct 12 ms 14292 KB Output is correct
12 Correct 12 ms 13780 KB Output is correct
13 Correct 12 ms 13776 KB Output is correct
14 Correct 14 ms 14052 KB Output is correct
15 Correct 14 ms 14040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 8 ms 8536 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 8 ms 8540 KB Output is correct
8 Correct 5 ms 8540 KB Output is correct
9 Correct 3 ms 8540 KB Output is correct
10 Correct 6 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 8 ms 8540 KB Output is correct
13 Correct 3 ms 8536 KB Output is correct
14 Correct 8 ms 8536 KB Output is correct
15 Correct 6 ms 8540 KB Output is correct
16 Correct 3 ms 8536 KB Output is correct
17 Correct 7 ms 8676 KB Output is correct
18 Correct 2 ms 8668 KB Output is correct
19 Correct 6 ms 8728 KB Output is correct
20 Correct 3 ms 8716 KB Output is correct
21 Correct 1 ms 8540 KB Output is correct
22 Correct 13 ms 14036 KB Output is correct
23 Correct 12 ms 14060 KB Output is correct
24 Correct 14 ms 14036 KB Output is correct
25 Correct 16 ms 14036 KB Output is correct
26 Correct 17 ms 13816 KB Output is correct
27 Correct 12 ms 13776 KB Output is correct
28 Correct 15 ms 13728 KB Output is correct
29 Correct 12 ms 13780 KB Output is correct
30 Correct 13 ms 14132 KB Output is correct
31 Correct 12 ms 14292 KB Output is correct
32 Correct 12 ms 13780 KB Output is correct
33 Correct 12 ms 13776 KB Output is correct
34 Correct 14 ms 14052 KB Output is correct
35 Correct 14 ms 14040 KB Output is correct
36 Correct 1984 ms 14872 KB Output is correct
37 Correct 1354 ms 14792 KB Output is correct
38 Correct 243 ms 14504 KB Output is correct
39 Correct 3060 ms 14796 KB Output is correct
40 Correct 273 ms 14500 KB Output is correct
41 Correct 1501 ms 14824 KB Output is correct
42 Correct 590 ms 14820 KB Output is correct
43 Correct 1481 ms 14176 KB Output is correct
44 Correct 500 ms 14168 KB Output is correct
45 Correct 2088 ms 14844 KB Output is correct
46 Correct 1309 ms 14760 KB Output is correct
47 Correct 254 ms 14288 KB Output is correct
48 Correct 3501 ms 14132 KB Output is correct
49 Correct 537 ms 14136 KB Output is correct
50 Correct 1931 ms 14708 KB Output is correct
51 Correct 1555 ms 14712 KB Output is correct
52 Correct 277 ms 14552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 13 ms 14036 KB Output is correct
3 Correct 12 ms 14060 KB Output is correct
4 Correct 14 ms 14036 KB Output is correct
5 Correct 16 ms 14036 KB Output is correct
6 Correct 17 ms 13816 KB Output is correct
7 Correct 12 ms 13776 KB Output is correct
8 Correct 15 ms 13728 KB Output is correct
9 Correct 12 ms 13780 KB Output is correct
10 Correct 13 ms 14132 KB Output is correct
11 Correct 12 ms 14292 KB Output is correct
12 Correct 12 ms 13780 KB Output is correct
13 Correct 12 ms 13776 KB Output is correct
14 Correct 14 ms 14052 KB Output is correct
15 Correct 14 ms 14040 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 2349 ms 14492 KB Output is correct
18 Correct 2404 ms 16556 KB Output is correct
19 Correct 2357 ms 16508 KB Output is correct
20 Correct 2373 ms 16256 KB Output is correct
21 Correct 2447 ms 16432 KB Output is correct
22 Correct 2321 ms 16564 KB Output is correct
23 Correct 2377 ms 16500 KB Output is correct
24 Correct 2318 ms 16292 KB Output is correct
25 Correct 2497 ms 16388 KB Output is correct
26 Correct 2298 ms 16304 KB Output is correct
27 Correct 2425 ms 16784 KB Output is correct
28 Correct 2360 ms 16584 KB Output is correct
29 Correct 2439 ms 16708 KB Output is correct
30 Correct 2364 ms 15624 KB Output is correct
31 Correct 2430 ms 15672 KB Output is correct
32 Correct 2317 ms 16276 KB Output is correct
33 Correct 2308 ms 16432 KB Output is correct
34 Correct 2285 ms 16228 KB Output is correct
35 Correct 2348 ms 16308 KB Output is correct
36 Correct 2334 ms 16508 KB Output is correct
37 Correct 2614 ms 15672 KB Output is correct
38 Correct 2265 ms 15784 KB Output is correct
39 Correct 2326 ms 16764 KB Output is correct
40 Execution timed out 4054 ms 16336 KB Time limit exceeded
41 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 13 ms 14036 KB Output is correct
3 Correct 12 ms 14060 KB Output is correct
4 Correct 14 ms 14036 KB Output is correct
5 Correct 16 ms 14036 KB Output is correct
6 Correct 17 ms 13816 KB Output is correct
7 Correct 12 ms 13776 KB Output is correct
8 Correct 15 ms 13728 KB Output is correct
9 Correct 12 ms 13780 KB Output is correct
10 Correct 13 ms 14132 KB Output is correct
11 Correct 12 ms 14292 KB Output is correct
12 Correct 12 ms 13780 KB Output is correct
13 Correct 12 ms 13776 KB Output is correct
14 Correct 14 ms 14052 KB Output is correct
15 Correct 14 ms 14040 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Execution timed out 4075 ms 14124 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 8 ms 8536 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 8 ms 8540 KB Output is correct
8 Correct 5 ms 8540 KB Output is correct
9 Correct 3 ms 8540 KB Output is correct
10 Correct 6 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 8 ms 8540 KB Output is correct
13 Correct 3 ms 8536 KB Output is correct
14 Correct 8 ms 8536 KB Output is correct
15 Correct 6 ms 8540 KB Output is correct
16 Correct 3 ms 8536 KB Output is correct
17 Correct 7 ms 8676 KB Output is correct
18 Correct 2 ms 8668 KB Output is correct
19 Correct 6 ms 8728 KB Output is correct
20 Correct 3 ms 8716 KB Output is correct
21 Correct 1 ms 8540 KB Output is correct
22 Correct 13 ms 14036 KB Output is correct
23 Correct 12 ms 14060 KB Output is correct
24 Correct 14 ms 14036 KB Output is correct
25 Correct 16 ms 14036 KB Output is correct
26 Correct 17 ms 13816 KB Output is correct
27 Correct 12 ms 13776 KB Output is correct
28 Correct 15 ms 13728 KB Output is correct
29 Correct 12 ms 13780 KB Output is correct
30 Correct 13 ms 14132 KB Output is correct
31 Correct 12 ms 14292 KB Output is correct
32 Correct 12 ms 13780 KB Output is correct
33 Correct 12 ms 13776 KB Output is correct
34 Correct 14 ms 14052 KB Output is correct
35 Correct 14 ms 14040 KB Output is correct
36 Correct 1984 ms 14872 KB Output is correct
37 Correct 1354 ms 14792 KB Output is correct
38 Correct 243 ms 14504 KB Output is correct
39 Correct 3060 ms 14796 KB Output is correct
40 Correct 273 ms 14500 KB Output is correct
41 Correct 1501 ms 14824 KB Output is correct
42 Correct 590 ms 14820 KB Output is correct
43 Correct 1481 ms 14176 KB Output is correct
44 Correct 500 ms 14168 KB Output is correct
45 Correct 2088 ms 14844 KB Output is correct
46 Correct 1309 ms 14760 KB Output is correct
47 Correct 254 ms 14288 KB Output is correct
48 Correct 3501 ms 14132 KB Output is correct
49 Correct 537 ms 14136 KB Output is correct
50 Correct 1931 ms 14708 KB Output is correct
51 Correct 1555 ms 14712 KB Output is correct
52 Correct 277 ms 14552 KB Output is correct
53 Correct 1 ms 8540 KB Output is correct
54 Correct 2349 ms 14492 KB Output is correct
55 Correct 2404 ms 16556 KB Output is correct
56 Correct 2357 ms 16508 KB Output is correct
57 Correct 2373 ms 16256 KB Output is correct
58 Correct 2447 ms 16432 KB Output is correct
59 Correct 2321 ms 16564 KB Output is correct
60 Correct 2377 ms 16500 KB Output is correct
61 Correct 2318 ms 16292 KB Output is correct
62 Correct 2497 ms 16388 KB Output is correct
63 Correct 2298 ms 16304 KB Output is correct
64 Correct 2425 ms 16784 KB Output is correct
65 Correct 2360 ms 16584 KB Output is correct
66 Correct 2439 ms 16708 KB Output is correct
67 Correct 2364 ms 15624 KB Output is correct
68 Correct 2430 ms 15672 KB Output is correct
69 Correct 2317 ms 16276 KB Output is correct
70 Correct 2308 ms 16432 KB Output is correct
71 Correct 2285 ms 16228 KB Output is correct
72 Correct 2348 ms 16308 KB Output is correct
73 Correct 2334 ms 16508 KB Output is correct
74 Correct 2614 ms 15672 KB Output is correct
75 Correct 2265 ms 15784 KB Output is correct
76 Correct 2326 ms 16764 KB Output is correct
77 Execution timed out 4054 ms 16336 KB Time limit exceeded
78 Halted 0 ms 0 KB -