Submission #493422

# Submission time Handle Problem Language Result Execution time Memory
493422 2021-12-11T09:21:07 Z blue Food Court (JOI21_foodcourt) C++17
68 / 100
1000 ms 115668 KB
#include <iostream>
#include <vector>
using namespace std;

using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;

const int mx = 250'000;
const ll INF = 1'000'000'000'000'000'000LL;

vi L(1+mx), R(1+mx), C(1+mx), A(1+mx);
vll K(1+mx), B(1+mx);
vi T(1+mx);

vi st[1+mx], en[1+mx], qr[1+mx];

vi ans(1+mx, 0);



struct segtree
{
    int l;
    int r;

    pair<ll, int> mn;
    pair<ll, int> mx;
    ll lp = 0;

    segtree* left = NULL;
    segtree* right = NULL;
    segtree* parent = NULL;


    segtree()
    {
        ;
    }

    segtree(int L, int R, segtree* par)
    {
        l = L;
        r = R;
        parent = par;
        mn = {0, l};
        mx = {0, l};
        if(l == r) return;
        int m = (l+r)/2;
        left = new segtree(l, m, this);
        right = new segtree(m+1, r, this);
    }

    void add(int L, int R, ll V)
    {
        if(r < L || R < l) return;
        else if(L <= l && r <= R)
        {
            lp += V;
            mn.first += V;
            mx.first += V;
        }
        else
        {
            left->add(L, R, V);
            right->add(L, R, V);

            mn = min(left->mn, right->mn);
            mn.first += lp;

            mx = max(left->mx, right->mx);
            mx.first += lp;
        }
    }

    pair<ll, int> rangemin(int L, int R)
    {
        if(R < l || r < L) return {INF, -1};
        else if(L <= l && r <= R) return mn;
        else
        {
            auto res = min(left->rangemin(L, R), right->rangemin(L, R));
            res.first += lp;
            return res;
        }
    }
};

int locate(segtree& S, int L, ll V) //find the first position > L with a value >= V, or return -1 if it doesn't exist
{
    segtree* pos = &S;
    ll above_lp = 0;
    while(!(pos->l == L && pos->r == L))
    {
        above_lp += pos->lp;
        if(L <= (pos->l + pos->r)/2)
            pos = pos->left;
        else
            pos = pos->right;
    }

    while(1)
    {
        if(pos->parent == NULL) return -1;

        if(above_lp + pos->parent->right->mx.first >= V)
        {
            pos = pos->parent->right;
            break;
        }
        pos = pos->parent;
        above_lp -= pos->lp;
    }

    while(!(pos->l == pos->r))
    {
        above_lp += pos->lp;
        if(above_lp + pos->left->mx.first >= V)
            pos = pos->left;
        else
            pos = pos->right;
    }

    return pos->l;
}

struct BIT
{
    int Z;
    vll arr;

    BIT()
    {
        ;
    }

    BIT(int z)
    {
        Z = z;
        arr = vll(1+z, 0);
    }

    void add(int i, ll v)
    {
        for(int j = i; j <= Z; j += j&-j)
            arr[j] += v;
    }

    ll prefsum(int i)
    {
        ll ans = 0;
        for(int j = i; j >= 1; j -= j&-j)
            ans += arr[j];
        return ans;
    }
};



int main()
{
    int N, M, Q;
    cin >> N >> M >> Q;

    for(int j = 1; j <= Q; j++)
    {
        cin >> T[j];
        if(T[j] == 1) cin >> L[j] >> R[j] >> C[j] >> K[j];
        else if(T[j] == 2)
        {
            cin >> L[j] >> R[j] >> K[j];
            K[j] *= -1;
        }
        else cin >> A[j] >> B[j];

        if(T[j] <= 2)
        {
            st[L[j]].push_back(j);
            en[R[j]].push_back(j);
        }
        else qr[A[j]].push_back(j);
    }

    vll S(1+Q, 0);

    vi ans(1+Q, 0);

    segtree ST(0, Q, NULL);
    segtree ST_plus(0, Q, NULL);
    BIT plus_sum(1+Q), minus_sum(1+Q);

    for(int i = 1; i <= N; i++)
    {
        for(int o: st[i])
        {
            S[o] = K[o];
            ST.add(o, Q, +K[o]);
            if(K[o] > 0)
                plus_sum.add(o, +K[o]);
            else
                minus_sum.add(o, -K[o]);

            if(K[o] > 0)
                ST_plus.add(o, Q, +K[o]);
        }

        for(int q: qr[i])
        {
            int p = ST.rangemin(0, q-1).second;

            ll min_sum = minus_sum.prefsum(q) - minus_sum.prefsum(p);

            // cerr << "p = " << p << '\n';
            // cerr << "total loss = " << minus_sum << '\n';

            ll plus_sub = plus_sum.prefsum(p);


            // cerr << "query = " << q << "\n";
            // for(int f = 1; f <= Q; f++) cerr << ST.rangemin(f, f).first << ' ';
            // cerr << '\n';

            // int lo = p+1;
            // int hi = q+1;
            // while(lo != hi)
            // {
            //     int mid = (lo+hi)/2;
            //     if(plus_sum.prefsum(mid) - plus_sub >= min_sum + B[q])
            //         hi = mid;
            //     else
            //         lo = mid+1;
            // }

            int lo = locate(ST_plus, p, min_sum + B[q] + plus_sub);
            if(lo != -1 && lo <= q)
            {
                ans[q] = C[lo];
            }
        }

        for(int o: en[i])
        {
            S[o] = 0;
            ST.add(o, Q, -K[o]);
            if(K[o] > 0)
                plus_sum.add(o, -K[o]);
            else
                minus_sum.add(o, +K[o]);

            if(K[o] > 0)
                ST_plus.add(o, Q, -K[o]);
        }
    }



    for(int q = 1; q <= Q; q++)
        if(T[q] == 3)
            cout << ans[q] << '\n';
    // cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28236 KB Output is correct
2 Correct 18 ms 28388 KB Output is correct
3 Correct 15 ms 28236 KB Output is correct
4 Correct 18 ms 28512 KB Output is correct
5 Correct 15 ms 28236 KB Output is correct
6 Correct 15 ms 28236 KB Output is correct
7 Correct 19 ms 28492 KB Output is correct
8 Correct 21 ms 28492 KB Output is correct
9 Correct 19 ms 28372 KB Output is correct
10 Correct 24 ms 28380 KB Output is correct
11 Correct 22 ms 28492 KB Output is correct
12 Correct 16 ms 28400 KB Output is correct
13 Correct 15 ms 28284 KB Output is correct
14 Correct 15 ms 28364 KB Output is correct
15 Correct 19 ms 28220 KB Output is correct
16 Correct 16 ms 28364 KB Output is correct
17 Correct 15 ms 28364 KB Output is correct
18 Correct 21 ms 28416 KB Output is correct
19 Correct 17 ms 28372 KB Output is correct
20 Correct 18 ms 28492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28236 KB Output is correct
2 Correct 18 ms 28388 KB Output is correct
3 Correct 15 ms 28236 KB Output is correct
4 Correct 18 ms 28512 KB Output is correct
5 Correct 15 ms 28236 KB Output is correct
6 Correct 15 ms 28236 KB Output is correct
7 Correct 19 ms 28492 KB Output is correct
8 Correct 21 ms 28492 KB Output is correct
9 Correct 19 ms 28372 KB Output is correct
10 Correct 24 ms 28380 KB Output is correct
11 Correct 22 ms 28492 KB Output is correct
12 Correct 16 ms 28400 KB Output is correct
13 Correct 15 ms 28284 KB Output is correct
14 Correct 15 ms 28364 KB Output is correct
15 Correct 19 ms 28220 KB Output is correct
16 Correct 16 ms 28364 KB Output is correct
17 Correct 15 ms 28364 KB Output is correct
18 Correct 21 ms 28416 KB Output is correct
19 Correct 17 ms 28372 KB Output is correct
20 Correct 18 ms 28492 KB Output is correct
21 Correct 19 ms 28384 KB Output is correct
22 Correct 18 ms 28436 KB Output is correct
23 Correct 17 ms 28464 KB Output is correct
24 Correct 16 ms 28492 KB Output is correct
25 Correct 15 ms 28236 KB Output is correct
26 Correct 15 ms 28364 KB Output is correct
27 Correct 17 ms 28492 KB Output is correct
28 Correct 19 ms 28492 KB Output is correct
29 Correct 17 ms 28440 KB Output is correct
30 Correct 18 ms 28492 KB Output is correct
31 Correct 20 ms 28492 KB Output is correct
32 Correct 20 ms 28460 KB Output is correct
33 Correct 21 ms 28404 KB Output is correct
34 Correct 21 ms 28432 KB Output is correct
35 Correct 17 ms 28380 KB Output is correct
36 Correct 17 ms 28492 KB Output is correct
37 Correct 15 ms 28232 KB Output is correct
38 Correct 17 ms 28492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 206 ms 53528 KB Output is correct
2 Correct 199 ms 53652 KB Output is correct
3 Correct 202 ms 53464 KB Output is correct
4 Correct 190 ms 53544 KB Output is correct
5 Correct 198 ms 53640 KB Output is correct
6 Correct 252 ms 53720 KB Output is correct
7 Correct 104 ms 49424 KB Output is correct
8 Correct 118 ms 49956 KB Output is correct
9 Correct 210 ms 53516 KB Output is correct
10 Correct 231 ms 53548 KB Output is correct
11 Correct 185 ms 53568 KB Output is correct
12 Correct 207 ms 53524 KB Output is correct
13 Correct 213 ms 50584 KB Output is correct
14 Correct 186 ms 53440 KB Output is correct
15 Correct 213 ms 53084 KB Output is correct
16 Correct 230 ms 53576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1018 ms 115668 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28236 KB Output is correct
2 Correct 18 ms 28388 KB Output is correct
3 Correct 15 ms 28236 KB Output is correct
4 Correct 18 ms 28512 KB Output is correct
5 Correct 15 ms 28236 KB Output is correct
6 Correct 15 ms 28236 KB Output is correct
7 Correct 19 ms 28492 KB Output is correct
8 Correct 21 ms 28492 KB Output is correct
9 Correct 19 ms 28372 KB Output is correct
10 Correct 24 ms 28380 KB Output is correct
11 Correct 22 ms 28492 KB Output is correct
12 Correct 16 ms 28400 KB Output is correct
13 Correct 15 ms 28284 KB Output is correct
14 Correct 15 ms 28364 KB Output is correct
15 Correct 19 ms 28220 KB Output is correct
16 Correct 16 ms 28364 KB Output is correct
17 Correct 15 ms 28364 KB Output is correct
18 Correct 21 ms 28416 KB Output is correct
19 Correct 17 ms 28372 KB Output is correct
20 Correct 18 ms 28492 KB Output is correct
21 Correct 206 ms 53528 KB Output is correct
22 Correct 199 ms 53652 KB Output is correct
23 Correct 202 ms 53464 KB Output is correct
24 Correct 190 ms 53544 KB Output is correct
25 Correct 198 ms 53640 KB Output is correct
26 Correct 252 ms 53720 KB Output is correct
27 Correct 104 ms 49424 KB Output is correct
28 Correct 118 ms 49956 KB Output is correct
29 Correct 210 ms 53516 KB Output is correct
30 Correct 231 ms 53548 KB Output is correct
31 Correct 185 ms 53568 KB Output is correct
32 Correct 207 ms 53524 KB Output is correct
33 Correct 213 ms 50584 KB Output is correct
34 Correct 186 ms 53440 KB Output is correct
35 Correct 213 ms 53084 KB Output is correct
36 Correct 230 ms 53576 KB Output is correct
37 Correct 208 ms 50624 KB Output is correct
38 Correct 166 ms 48088 KB Output is correct
39 Correct 94 ms 46672 KB Output is correct
40 Correct 108 ms 49304 KB Output is correct
41 Correct 242 ms 53212 KB Output is correct
42 Correct 213 ms 53196 KB Output is correct
43 Correct 220 ms 53160 KB Output is correct
44 Correct 242 ms 53196 KB Output is correct
45 Correct 222 ms 53136 KB Output is correct
46 Correct 255 ms 53120 KB Output is correct
47 Correct 140 ms 51716 KB Output is correct
48 Correct 193 ms 52060 KB Output is correct
49 Correct 146 ms 45712 KB Output is correct
50 Correct 200 ms 49476 KB Output is correct
51 Correct 212 ms 53316 KB Output is correct
52 Correct 217 ms 53232 KB Output is correct
53 Correct 171 ms 48044 KB Output is correct
54 Correct 222 ms 53488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 50848 KB Output is correct
2 Correct 275 ms 52944 KB Output is correct
3 Correct 267 ms 53444 KB Output is correct
4 Correct 158 ms 46160 KB Output is correct
5 Correct 204 ms 50000 KB Output is correct
6 Correct 241 ms 53280 KB Output is correct
7 Correct 122 ms 49144 KB Output is correct
8 Correct 117 ms 47632 KB Output is correct
9 Correct 163 ms 52140 KB Output is correct
10 Correct 182 ms 44996 KB Output is correct
11 Correct 221 ms 52276 KB Output is correct
12 Correct 255 ms 52328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28236 KB Output is correct
2 Correct 18 ms 28388 KB Output is correct
3 Correct 15 ms 28236 KB Output is correct
4 Correct 18 ms 28512 KB Output is correct
5 Correct 15 ms 28236 KB Output is correct
6 Correct 15 ms 28236 KB Output is correct
7 Correct 19 ms 28492 KB Output is correct
8 Correct 21 ms 28492 KB Output is correct
9 Correct 19 ms 28372 KB Output is correct
10 Correct 24 ms 28380 KB Output is correct
11 Correct 22 ms 28492 KB Output is correct
12 Correct 16 ms 28400 KB Output is correct
13 Correct 15 ms 28284 KB Output is correct
14 Correct 15 ms 28364 KB Output is correct
15 Correct 19 ms 28220 KB Output is correct
16 Correct 16 ms 28364 KB Output is correct
17 Correct 15 ms 28364 KB Output is correct
18 Correct 21 ms 28416 KB Output is correct
19 Correct 17 ms 28372 KB Output is correct
20 Correct 18 ms 28492 KB Output is correct
21 Correct 19 ms 28384 KB Output is correct
22 Correct 18 ms 28436 KB Output is correct
23 Correct 17 ms 28464 KB Output is correct
24 Correct 16 ms 28492 KB Output is correct
25 Correct 15 ms 28236 KB Output is correct
26 Correct 15 ms 28364 KB Output is correct
27 Correct 17 ms 28492 KB Output is correct
28 Correct 19 ms 28492 KB Output is correct
29 Correct 17 ms 28440 KB Output is correct
30 Correct 18 ms 28492 KB Output is correct
31 Correct 20 ms 28492 KB Output is correct
32 Correct 20 ms 28460 KB Output is correct
33 Correct 21 ms 28404 KB Output is correct
34 Correct 21 ms 28432 KB Output is correct
35 Correct 17 ms 28380 KB Output is correct
36 Correct 17 ms 28492 KB Output is correct
37 Correct 15 ms 28232 KB Output is correct
38 Correct 17 ms 28492 KB Output is correct
39 Correct 206 ms 53528 KB Output is correct
40 Correct 199 ms 53652 KB Output is correct
41 Correct 202 ms 53464 KB Output is correct
42 Correct 190 ms 53544 KB Output is correct
43 Correct 198 ms 53640 KB Output is correct
44 Correct 252 ms 53720 KB Output is correct
45 Correct 104 ms 49424 KB Output is correct
46 Correct 118 ms 49956 KB Output is correct
47 Correct 210 ms 53516 KB Output is correct
48 Correct 231 ms 53548 KB Output is correct
49 Correct 185 ms 53568 KB Output is correct
50 Correct 207 ms 53524 KB Output is correct
51 Correct 213 ms 50584 KB Output is correct
52 Correct 186 ms 53440 KB Output is correct
53 Correct 213 ms 53084 KB Output is correct
54 Correct 230 ms 53576 KB Output is correct
55 Correct 208 ms 50624 KB Output is correct
56 Correct 166 ms 48088 KB Output is correct
57 Correct 94 ms 46672 KB Output is correct
58 Correct 108 ms 49304 KB Output is correct
59 Correct 242 ms 53212 KB Output is correct
60 Correct 213 ms 53196 KB Output is correct
61 Correct 220 ms 53160 KB Output is correct
62 Correct 242 ms 53196 KB Output is correct
63 Correct 222 ms 53136 KB Output is correct
64 Correct 255 ms 53120 KB Output is correct
65 Correct 140 ms 51716 KB Output is correct
66 Correct 193 ms 52060 KB Output is correct
67 Correct 146 ms 45712 KB Output is correct
68 Correct 200 ms 49476 KB Output is correct
69 Correct 212 ms 53316 KB Output is correct
70 Correct 217 ms 53232 KB Output is correct
71 Correct 171 ms 48044 KB Output is correct
72 Correct 222 ms 53488 KB Output is correct
73 Correct 210 ms 50848 KB Output is correct
74 Correct 275 ms 52944 KB Output is correct
75 Correct 267 ms 53444 KB Output is correct
76 Correct 158 ms 46160 KB Output is correct
77 Correct 204 ms 50000 KB Output is correct
78 Correct 241 ms 53280 KB Output is correct
79 Correct 122 ms 49144 KB Output is correct
80 Correct 117 ms 47632 KB Output is correct
81 Correct 163 ms 52140 KB Output is correct
82 Correct 182 ms 44996 KB Output is correct
83 Correct 221 ms 52276 KB Output is correct
84 Correct 255 ms 52328 KB Output is correct
85 Correct 242 ms 51128 KB Output is correct
86 Correct 243 ms 53500 KB Output is correct
87 Correct 231 ms 50232 KB Output is correct
88 Correct 240 ms 53296 KB Output is correct
89 Correct 166 ms 45288 KB Output is correct
90 Correct 230 ms 53200 KB Output is correct
91 Correct 188 ms 48964 KB Output is correct
92 Correct 197 ms 47824 KB Output is correct
93 Correct 221 ms 53204 KB Output is correct
94 Correct 286 ms 53276 KB Output is correct
95 Correct 232 ms 52648 KB Output is correct
96 Correct 234 ms 53224 KB Output is correct
97 Correct 241 ms 53280 KB Output is correct
98 Correct 189 ms 49816 KB Output is correct
99 Correct 152 ms 51900 KB Output is correct
100 Correct 185 ms 48604 KB Output is correct
101 Correct 215 ms 52188 KB Output is correct
102 Correct 188 ms 53832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28236 KB Output is correct
2 Correct 18 ms 28388 KB Output is correct
3 Correct 15 ms 28236 KB Output is correct
4 Correct 18 ms 28512 KB Output is correct
5 Correct 15 ms 28236 KB Output is correct
6 Correct 15 ms 28236 KB Output is correct
7 Correct 19 ms 28492 KB Output is correct
8 Correct 21 ms 28492 KB Output is correct
9 Correct 19 ms 28372 KB Output is correct
10 Correct 24 ms 28380 KB Output is correct
11 Correct 22 ms 28492 KB Output is correct
12 Correct 16 ms 28400 KB Output is correct
13 Correct 15 ms 28284 KB Output is correct
14 Correct 15 ms 28364 KB Output is correct
15 Correct 19 ms 28220 KB Output is correct
16 Correct 16 ms 28364 KB Output is correct
17 Correct 15 ms 28364 KB Output is correct
18 Correct 21 ms 28416 KB Output is correct
19 Correct 17 ms 28372 KB Output is correct
20 Correct 18 ms 28492 KB Output is correct
21 Correct 19 ms 28384 KB Output is correct
22 Correct 18 ms 28436 KB Output is correct
23 Correct 17 ms 28464 KB Output is correct
24 Correct 16 ms 28492 KB Output is correct
25 Correct 15 ms 28236 KB Output is correct
26 Correct 15 ms 28364 KB Output is correct
27 Correct 17 ms 28492 KB Output is correct
28 Correct 19 ms 28492 KB Output is correct
29 Correct 17 ms 28440 KB Output is correct
30 Correct 18 ms 28492 KB Output is correct
31 Correct 20 ms 28492 KB Output is correct
32 Correct 20 ms 28460 KB Output is correct
33 Correct 21 ms 28404 KB Output is correct
34 Correct 21 ms 28432 KB Output is correct
35 Correct 17 ms 28380 KB Output is correct
36 Correct 17 ms 28492 KB Output is correct
37 Correct 15 ms 28232 KB Output is correct
38 Correct 17 ms 28492 KB Output is correct
39 Correct 206 ms 53528 KB Output is correct
40 Correct 199 ms 53652 KB Output is correct
41 Correct 202 ms 53464 KB Output is correct
42 Correct 190 ms 53544 KB Output is correct
43 Correct 198 ms 53640 KB Output is correct
44 Correct 252 ms 53720 KB Output is correct
45 Correct 104 ms 49424 KB Output is correct
46 Correct 118 ms 49956 KB Output is correct
47 Correct 210 ms 53516 KB Output is correct
48 Correct 231 ms 53548 KB Output is correct
49 Correct 185 ms 53568 KB Output is correct
50 Correct 207 ms 53524 KB Output is correct
51 Correct 213 ms 50584 KB Output is correct
52 Correct 186 ms 53440 KB Output is correct
53 Correct 213 ms 53084 KB Output is correct
54 Correct 230 ms 53576 KB Output is correct
55 Execution timed out 1018 ms 115668 KB Time limit exceeded
56 Halted 0 ms 0 KB -