Submission #493423

# Submission time Handle Problem Language Result Execution time Memory
493423 2021-12-11T09:24:54 Z blue Food Court (JOI21_foodcourt) C++17
68 / 100
1000 ms 114400 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) 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 = ST_plus.rangemin(p, p).first;

            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) 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 15 ms 28236 KB Output is correct
2 Correct 17 ms 28364 KB Output is correct
3 Correct 15 ms 28204 KB Output is correct
4 Correct 17 ms 28364 KB Output is correct
5 Correct 16 ms 28324 KB Output is correct
6 Correct 16 ms 28320 KB Output is correct
7 Correct 18 ms 28408 KB Output is correct
8 Correct 17 ms 28356 KB Output is correct
9 Correct 17 ms 28364 KB Output is correct
10 Correct 20 ms 28400 KB Output is correct
11 Correct 16 ms 28476 KB Output is correct
12 Correct 19 ms 28364 KB Output is correct
13 Correct 15 ms 28188 KB Output is correct
14 Correct 16 ms 28380 KB Output is correct
15 Correct 15 ms 28144 KB Output is correct
16 Correct 16 ms 28380 KB Output is correct
17 Correct 17 ms 28236 KB Output is correct
18 Correct 19 ms 28404 KB Output is correct
19 Correct 17 ms 28388 KB Output is correct
20 Correct 17 ms 28404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28236 KB Output is correct
2 Correct 17 ms 28364 KB Output is correct
3 Correct 15 ms 28204 KB Output is correct
4 Correct 17 ms 28364 KB Output is correct
5 Correct 16 ms 28324 KB Output is correct
6 Correct 16 ms 28320 KB Output is correct
7 Correct 18 ms 28408 KB Output is correct
8 Correct 17 ms 28356 KB Output is correct
9 Correct 17 ms 28364 KB Output is correct
10 Correct 20 ms 28400 KB Output is correct
11 Correct 16 ms 28476 KB Output is correct
12 Correct 19 ms 28364 KB Output is correct
13 Correct 15 ms 28188 KB Output is correct
14 Correct 16 ms 28380 KB Output is correct
15 Correct 15 ms 28144 KB Output is correct
16 Correct 16 ms 28380 KB Output is correct
17 Correct 17 ms 28236 KB Output is correct
18 Correct 19 ms 28404 KB Output is correct
19 Correct 17 ms 28388 KB Output is correct
20 Correct 17 ms 28404 KB Output is correct
21 Correct 19 ms 28416 KB Output is correct
22 Correct 17 ms 28472 KB Output is correct
23 Correct 18 ms 28312 KB Output is correct
24 Correct 21 ms 28364 KB Output is correct
25 Correct 19 ms 28364 KB Output is correct
26 Correct 16 ms 28236 KB Output is correct
27 Correct 17 ms 28364 KB Output is correct
28 Correct 18 ms 28380 KB Output is correct
29 Correct 16 ms 28364 KB Output is correct
30 Correct 21 ms 28448 KB Output is correct
31 Correct 21 ms 28388 KB Output is correct
32 Correct 20 ms 28364 KB Output is correct
33 Correct 16 ms 28364 KB Output is correct
34 Correct 16 ms 28360 KB Output is correct
35 Correct 16 ms 28364 KB Output is correct
36 Correct 16 ms 28380 KB Output is correct
37 Correct 16 ms 28236 KB Output is correct
38 Correct 17 ms 28388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 52368 KB Output is correct
2 Correct 217 ms 52412 KB Output is correct
3 Correct 207 ms 52292 KB Output is correct
4 Correct 222 ms 52408 KB Output is correct
5 Correct 213 ms 52396 KB Output is correct
6 Correct 214 ms 52432 KB Output is correct
7 Correct 121 ms 48700 KB Output is correct
8 Correct 117 ms 49144 KB Output is correct
9 Correct 206 ms 52332 KB Output is correct
10 Correct 202 ms 52336 KB Output is correct
11 Correct 227 ms 52320 KB Output is correct
12 Correct 213 ms 52396 KB Output is correct
13 Correct 170 ms 49756 KB Output is correct
14 Correct 203 ms 52420 KB Output is correct
15 Correct 216 ms 51812 KB Output is correct
16 Correct 222 ms 52352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1043 ms 114400 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28236 KB Output is correct
2 Correct 17 ms 28364 KB Output is correct
3 Correct 15 ms 28204 KB Output is correct
4 Correct 17 ms 28364 KB Output is correct
5 Correct 16 ms 28324 KB Output is correct
6 Correct 16 ms 28320 KB Output is correct
7 Correct 18 ms 28408 KB Output is correct
8 Correct 17 ms 28356 KB Output is correct
9 Correct 17 ms 28364 KB Output is correct
10 Correct 20 ms 28400 KB Output is correct
11 Correct 16 ms 28476 KB Output is correct
12 Correct 19 ms 28364 KB Output is correct
13 Correct 15 ms 28188 KB Output is correct
14 Correct 16 ms 28380 KB Output is correct
15 Correct 15 ms 28144 KB Output is correct
16 Correct 16 ms 28380 KB Output is correct
17 Correct 17 ms 28236 KB Output is correct
18 Correct 19 ms 28404 KB Output is correct
19 Correct 17 ms 28388 KB Output is correct
20 Correct 17 ms 28404 KB Output is correct
21 Correct 200 ms 52368 KB Output is correct
22 Correct 217 ms 52412 KB Output is correct
23 Correct 207 ms 52292 KB Output is correct
24 Correct 222 ms 52408 KB Output is correct
25 Correct 213 ms 52396 KB Output is correct
26 Correct 214 ms 52432 KB Output is correct
27 Correct 121 ms 48700 KB Output is correct
28 Correct 117 ms 49144 KB Output is correct
29 Correct 206 ms 52332 KB Output is correct
30 Correct 202 ms 52336 KB Output is correct
31 Correct 227 ms 52320 KB Output is correct
32 Correct 213 ms 52396 KB Output is correct
33 Correct 170 ms 49756 KB Output is correct
34 Correct 203 ms 52420 KB Output is correct
35 Correct 216 ms 51812 KB Output is correct
36 Correct 222 ms 52352 KB Output is correct
37 Correct 203 ms 49708 KB Output is correct
38 Correct 158 ms 47172 KB Output is correct
39 Correct 88 ms 46140 KB Output is correct
40 Correct 128 ms 48720 KB Output is correct
41 Correct 186 ms 52116 KB Output is correct
42 Correct 190 ms 52136 KB Output is correct
43 Correct 211 ms 52132 KB Output is correct
44 Correct 216 ms 52076 KB Output is correct
45 Correct 203 ms 52104 KB Output is correct
46 Correct 228 ms 52128 KB Output is correct
47 Correct 134 ms 50868 KB Output is correct
48 Correct 180 ms 51012 KB Output is correct
49 Correct 152 ms 44968 KB Output is correct
50 Correct 188 ms 48476 KB Output is correct
51 Correct 217 ms 52300 KB Output is correct
52 Correct 229 ms 52292 KB Output is correct
53 Correct 238 ms 47044 KB Output is correct
54 Correct 227 ms 52424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 49584 KB Output is correct
2 Correct 221 ms 51836 KB Output is correct
3 Correct 226 ms 52328 KB Output is correct
4 Correct 173 ms 45100 KB Output is correct
5 Correct 256 ms 48900 KB Output is correct
6 Correct 239 ms 52280 KB Output is correct
7 Correct 119 ms 47804 KB Output is correct
8 Correct 143 ms 46580 KB Output is correct
9 Correct 158 ms 51024 KB Output is correct
10 Correct 143 ms 43892 KB Output is correct
11 Correct 215 ms 51216 KB Output is correct
12 Correct 202 ms 51220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28236 KB Output is correct
2 Correct 17 ms 28364 KB Output is correct
3 Correct 15 ms 28204 KB Output is correct
4 Correct 17 ms 28364 KB Output is correct
5 Correct 16 ms 28324 KB Output is correct
6 Correct 16 ms 28320 KB Output is correct
7 Correct 18 ms 28408 KB Output is correct
8 Correct 17 ms 28356 KB Output is correct
9 Correct 17 ms 28364 KB Output is correct
10 Correct 20 ms 28400 KB Output is correct
11 Correct 16 ms 28476 KB Output is correct
12 Correct 19 ms 28364 KB Output is correct
13 Correct 15 ms 28188 KB Output is correct
14 Correct 16 ms 28380 KB Output is correct
15 Correct 15 ms 28144 KB Output is correct
16 Correct 16 ms 28380 KB Output is correct
17 Correct 17 ms 28236 KB Output is correct
18 Correct 19 ms 28404 KB Output is correct
19 Correct 17 ms 28388 KB Output is correct
20 Correct 17 ms 28404 KB Output is correct
21 Correct 19 ms 28416 KB Output is correct
22 Correct 17 ms 28472 KB Output is correct
23 Correct 18 ms 28312 KB Output is correct
24 Correct 21 ms 28364 KB Output is correct
25 Correct 19 ms 28364 KB Output is correct
26 Correct 16 ms 28236 KB Output is correct
27 Correct 17 ms 28364 KB Output is correct
28 Correct 18 ms 28380 KB Output is correct
29 Correct 16 ms 28364 KB Output is correct
30 Correct 21 ms 28448 KB Output is correct
31 Correct 21 ms 28388 KB Output is correct
32 Correct 20 ms 28364 KB Output is correct
33 Correct 16 ms 28364 KB Output is correct
34 Correct 16 ms 28360 KB Output is correct
35 Correct 16 ms 28364 KB Output is correct
36 Correct 16 ms 28380 KB Output is correct
37 Correct 16 ms 28236 KB Output is correct
38 Correct 17 ms 28388 KB Output is correct
39 Correct 200 ms 52368 KB Output is correct
40 Correct 217 ms 52412 KB Output is correct
41 Correct 207 ms 52292 KB Output is correct
42 Correct 222 ms 52408 KB Output is correct
43 Correct 213 ms 52396 KB Output is correct
44 Correct 214 ms 52432 KB Output is correct
45 Correct 121 ms 48700 KB Output is correct
46 Correct 117 ms 49144 KB Output is correct
47 Correct 206 ms 52332 KB Output is correct
48 Correct 202 ms 52336 KB Output is correct
49 Correct 227 ms 52320 KB Output is correct
50 Correct 213 ms 52396 KB Output is correct
51 Correct 170 ms 49756 KB Output is correct
52 Correct 203 ms 52420 KB Output is correct
53 Correct 216 ms 51812 KB Output is correct
54 Correct 222 ms 52352 KB Output is correct
55 Correct 203 ms 49708 KB Output is correct
56 Correct 158 ms 47172 KB Output is correct
57 Correct 88 ms 46140 KB Output is correct
58 Correct 128 ms 48720 KB Output is correct
59 Correct 186 ms 52116 KB Output is correct
60 Correct 190 ms 52136 KB Output is correct
61 Correct 211 ms 52132 KB Output is correct
62 Correct 216 ms 52076 KB Output is correct
63 Correct 203 ms 52104 KB Output is correct
64 Correct 228 ms 52128 KB Output is correct
65 Correct 134 ms 50868 KB Output is correct
66 Correct 180 ms 51012 KB Output is correct
67 Correct 152 ms 44968 KB Output is correct
68 Correct 188 ms 48476 KB Output is correct
69 Correct 217 ms 52300 KB Output is correct
70 Correct 229 ms 52292 KB Output is correct
71 Correct 238 ms 47044 KB Output is correct
72 Correct 227 ms 52424 KB Output is correct
73 Correct 213 ms 49584 KB Output is correct
74 Correct 221 ms 51836 KB Output is correct
75 Correct 226 ms 52328 KB Output is correct
76 Correct 173 ms 45100 KB Output is correct
77 Correct 256 ms 48900 KB Output is correct
78 Correct 239 ms 52280 KB Output is correct
79 Correct 119 ms 47804 KB Output is correct
80 Correct 143 ms 46580 KB Output is correct
81 Correct 158 ms 51024 KB Output is correct
82 Correct 143 ms 43892 KB Output is correct
83 Correct 215 ms 51216 KB Output is correct
84 Correct 202 ms 51220 KB Output is correct
85 Correct 229 ms 50020 KB Output is correct
86 Correct 223 ms 52420 KB Output is correct
87 Correct 227 ms 49220 KB Output is correct
88 Correct 250 ms 52184 KB Output is correct
89 Correct 164 ms 44320 KB Output is correct
90 Correct 238 ms 52112 KB Output is correct
91 Correct 184 ms 47740 KB Output is correct
92 Correct 188 ms 46732 KB Output is correct
93 Correct 228 ms 52164 KB Output is correct
94 Correct 221 ms 52084 KB Output is correct
95 Correct 232 ms 51528 KB Output is correct
96 Correct 260 ms 52132 KB Output is correct
97 Correct 218 ms 52168 KB Output is correct
98 Correct 193 ms 48568 KB Output is correct
99 Correct 167 ms 50884 KB Output is correct
100 Correct 170 ms 47376 KB Output is correct
101 Correct 217 ms 51052 KB Output is correct
102 Correct 231 ms 52784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28236 KB Output is correct
2 Correct 17 ms 28364 KB Output is correct
3 Correct 15 ms 28204 KB Output is correct
4 Correct 17 ms 28364 KB Output is correct
5 Correct 16 ms 28324 KB Output is correct
6 Correct 16 ms 28320 KB Output is correct
7 Correct 18 ms 28408 KB Output is correct
8 Correct 17 ms 28356 KB Output is correct
9 Correct 17 ms 28364 KB Output is correct
10 Correct 20 ms 28400 KB Output is correct
11 Correct 16 ms 28476 KB Output is correct
12 Correct 19 ms 28364 KB Output is correct
13 Correct 15 ms 28188 KB Output is correct
14 Correct 16 ms 28380 KB Output is correct
15 Correct 15 ms 28144 KB Output is correct
16 Correct 16 ms 28380 KB Output is correct
17 Correct 17 ms 28236 KB Output is correct
18 Correct 19 ms 28404 KB Output is correct
19 Correct 17 ms 28388 KB Output is correct
20 Correct 17 ms 28404 KB Output is correct
21 Correct 19 ms 28416 KB Output is correct
22 Correct 17 ms 28472 KB Output is correct
23 Correct 18 ms 28312 KB Output is correct
24 Correct 21 ms 28364 KB Output is correct
25 Correct 19 ms 28364 KB Output is correct
26 Correct 16 ms 28236 KB Output is correct
27 Correct 17 ms 28364 KB Output is correct
28 Correct 18 ms 28380 KB Output is correct
29 Correct 16 ms 28364 KB Output is correct
30 Correct 21 ms 28448 KB Output is correct
31 Correct 21 ms 28388 KB Output is correct
32 Correct 20 ms 28364 KB Output is correct
33 Correct 16 ms 28364 KB Output is correct
34 Correct 16 ms 28360 KB Output is correct
35 Correct 16 ms 28364 KB Output is correct
36 Correct 16 ms 28380 KB Output is correct
37 Correct 16 ms 28236 KB Output is correct
38 Correct 17 ms 28388 KB Output is correct
39 Correct 200 ms 52368 KB Output is correct
40 Correct 217 ms 52412 KB Output is correct
41 Correct 207 ms 52292 KB Output is correct
42 Correct 222 ms 52408 KB Output is correct
43 Correct 213 ms 52396 KB Output is correct
44 Correct 214 ms 52432 KB Output is correct
45 Correct 121 ms 48700 KB Output is correct
46 Correct 117 ms 49144 KB Output is correct
47 Correct 206 ms 52332 KB Output is correct
48 Correct 202 ms 52336 KB Output is correct
49 Correct 227 ms 52320 KB Output is correct
50 Correct 213 ms 52396 KB Output is correct
51 Correct 170 ms 49756 KB Output is correct
52 Correct 203 ms 52420 KB Output is correct
53 Correct 216 ms 51812 KB Output is correct
54 Correct 222 ms 52352 KB Output is correct
55 Execution timed out 1043 ms 114400 KB Time limit exceeded
56 Halted 0 ms 0 KB -