Submission #623707

# Submission time Handle Problem Language Result Execution time Memory
623707 2022-08-06T10:52:51 Z Loki_Nguyen Two Dishes (JOI19_dishes) C++14
100 / 100
4777 ms 299276 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "dfss"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
#define ull unsigned long long
using namespace std;
const ll mod = 998244353;
const int N = 2e6 + 55;
const ll inf = 1e18;
vector<pll> adj[N];
ll pw(ll k, ll n)
{
    ll total = 1;
    for (; n; n >>= 1)
    {
        if (n & 1)
            total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
struct node
{
    ll x, s, p;
} a[N], b[N];
struct IT
{
    vector<ll> st;
    vector<pll> lazy;
    int n;
    IT(int _n)
    {
        n = _n;
        st.resize(n * 4 + 4, 0);
        lazy.resize(n * 4 + 4, make_pair(-inf, 0));
    }
    void add(pll& x, pll y)
    {
        if(y.fi != -inf)x.fi = max(x.fi, y.fi-x.se);
        x.se += y.se;
    }
    void down(int id)
    {
        if (lazy[id].fi == -inf && lazy[id].se == 0)return;
        st[id<<1] = max(st[id<<1], lazy[id].fi)+lazy[id].se;
        st[id << 1|1] = max(st[id << 1|1], lazy[id].fi) + lazy[id].se;
        add(lazy[id<<1], lazy[id]);
        add(lazy[id<<1|1], lazy[id]);
        lazy[id].fi = -inf;
        lazy[id].se = 0;
    }
    void update(int id, int l, int r, int u, int v, pll x)
    {
        if (u <= l && r <= v)
        {
            st[id] = max(st[id], x.fi)+x.se;
            add(lazy[id], x);
            return;
        }
        if (u > r || l > v)
            return;
        int mid = (l + r) >> 1;
        down(id);
        update(id << 1, l, mid, u, v, x);
        update(id << 1 | 1, mid + 1, r, u, v, x);
        st[id] = max(st[id << 1 | 1], st[id << 1]);
    }
    void update(int l, int r, pll x)
    {
        update(1, 0, n, l, r, x);
    }
    ll get(int id, int l, int r, int pos)
    {
        if (l == r)
            return st[id];
        int mid = (l + r) >> 1;
        down(id);
        if (pos <= mid)
            return get(id << 1, l, mid, pos);
        return get(id << 1 | 1, mid + 1, r, pos);
    }
    ll get(int pos)
    {
        return get(1, 0, n, pos);
    }
};
int n, m, t, k;
ll ans, c[N], sum;

void sol()
{
    cin >> n >> m;
    a[0].x = 0;
    b[0].x = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].x >> a[i].s >> a[i].p;
        a[i].x += a[i - 1].x;
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> b[i].x >> b[i].s >> b[i].p;
        b[i].x += b[i - 1].x;
        int l = 1, r = n, mid;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (a[mid].x + b[i].x <= b[i].s)
                l = mid + 1;
            else
                r = mid - 1;
        }
        if (a[r].x + b[i].x <= b[i].s)
        {
            ans += b[i].p;
            if (r < n)
            {
                // cout << r + 1 << " " << i - 1 << " " << -b[i].p << '\n';
                adj[r + 1].pb({i - 1, -b[i].p});
            }
        }
    }
    IT it(m);
    for (int i = 1; i <= n; i++)
    {
        int l = 1, r = m, mid;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (b[mid].x + a[i].x <= a[i].s)
                l = mid + 1;
            else
                r = mid - 1;
        }
        if (b[r].x + a[i].x <= a[i].s)
        {
            adj[i].pb({r, a[i].p});
            // cout << i << " " << r << " " << a[i].p << '\n';
        }

        sort(adj[i].begin(), adj[i].end());
        k = adj[i].size();
        for (int j = 0; j < k;)
        {
            int pos = adj[i][j].fi;
            sum = 0;
            while (j < k && adj[i][j].fi == pos)
            {
                sum += adj[i][j].se;
                ++j;
            }
            // cout << i << " " << pos << " " << sum << ' ';
            it.update(0, pos, {-inf, sum});
            it.update(pos, (j == k ? m : adj[i][j].fi), {it.get(pos), 0});
            // cout << it.get(m) <<'\n';
        }
    }
    cout << ans + it.get(m);
}
int main()
{
    if (fopen(task ".inp", "r"))
    {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ntest = 1;
    // cout << (1<<20);
    // cin >> ntest;
    while (ntest-- > 0)
        sol();
}
/*
 */

Compilation message

dishes.cpp: In function 'int main()':
dishes.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
dishes.cpp:169:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 329 ms 86904 KB Output is correct
2 Correct 333 ms 98620 KB Output is correct
3 Correct 291 ms 95132 KB Output is correct
4 Correct 262 ms 95048 KB Output is correct
5 Correct 23 ms 47188 KB Output is correct
6 Correct 332 ms 96496 KB Output is correct
7 Correct 102 ms 77492 KB Output is correct
8 Correct 109 ms 65200 KB Output is correct
9 Correct 295 ms 96332 KB Output is correct
10 Correct 267 ms 92280 KB Output is correct
11 Correct 237 ms 89416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47312 KB Output is correct
2 Correct 23 ms 47188 KB Output is correct
3 Correct 24 ms 47292 KB Output is correct
4 Correct 24 ms 47296 KB Output is correct
5 Correct 25 ms 47260 KB Output is correct
6 Correct 24 ms 47284 KB Output is correct
7 Correct 26 ms 47248 KB Output is correct
8 Correct 24 ms 47200 KB Output is correct
9 Correct 25 ms 47320 KB Output is correct
10 Correct 25 ms 47304 KB Output is correct
11 Correct 26 ms 47292 KB Output is correct
12 Correct 24 ms 47268 KB Output is correct
13 Correct 24 ms 47316 KB Output is correct
14 Correct 24 ms 47288 KB Output is correct
15 Correct 24 ms 47204 KB Output is correct
16 Correct 24 ms 47188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47312 KB Output is correct
2 Correct 23 ms 47188 KB Output is correct
3 Correct 24 ms 47292 KB Output is correct
4 Correct 24 ms 47296 KB Output is correct
5 Correct 25 ms 47260 KB Output is correct
6 Correct 24 ms 47284 KB Output is correct
7 Correct 26 ms 47248 KB Output is correct
8 Correct 24 ms 47200 KB Output is correct
9 Correct 25 ms 47320 KB Output is correct
10 Correct 25 ms 47304 KB Output is correct
11 Correct 26 ms 47292 KB Output is correct
12 Correct 24 ms 47268 KB Output is correct
13 Correct 24 ms 47316 KB Output is correct
14 Correct 24 ms 47288 KB Output is correct
15 Correct 24 ms 47204 KB Output is correct
16 Correct 24 ms 47188 KB Output is correct
17 Correct 26 ms 47692 KB Output is correct
18 Correct 27 ms 47740 KB Output is correct
19 Correct 28 ms 47780 KB Output is correct
20 Correct 29 ms 47676 KB Output is correct
21 Correct 32 ms 47700 KB Output is correct
22 Correct 27 ms 47700 KB Output is correct
23 Correct 27 ms 47616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47312 KB Output is correct
2 Correct 23 ms 47188 KB Output is correct
3 Correct 24 ms 47292 KB Output is correct
4 Correct 24 ms 47296 KB Output is correct
5 Correct 25 ms 47260 KB Output is correct
6 Correct 24 ms 47284 KB Output is correct
7 Correct 26 ms 47248 KB Output is correct
8 Correct 24 ms 47200 KB Output is correct
9 Correct 25 ms 47320 KB Output is correct
10 Correct 25 ms 47304 KB Output is correct
11 Correct 26 ms 47292 KB Output is correct
12 Correct 24 ms 47268 KB Output is correct
13 Correct 24 ms 47316 KB Output is correct
14 Correct 24 ms 47288 KB Output is correct
15 Correct 24 ms 47204 KB Output is correct
16 Correct 24 ms 47188 KB Output is correct
17 Correct 26 ms 47692 KB Output is correct
18 Correct 27 ms 47740 KB Output is correct
19 Correct 28 ms 47780 KB Output is correct
20 Correct 29 ms 47676 KB Output is correct
21 Correct 32 ms 47700 KB Output is correct
22 Correct 27 ms 47700 KB Output is correct
23 Correct 27 ms 47616 KB Output is correct
24 Correct 239 ms 92344 KB Output is correct
25 Correct 314 ms 92248 KB Output is correct
26 Correct 283 ms 92432 KB Output is correct
27 Correct 332 ms 94980 KB Output is correct
28 Correct 359 ms 92600 KB Output is correct
29 Correct 264 ms 92996 KB Output is correct
30 Correct 732 ms 95944 KB Output is correct
31 Correct 149 ms 77728 KB Output is correct
32 Correct 97 ms 63432 KB Output is correct
33 Correct 452 ms 91104 KB Output is correct
34 Correct 570 ms 94928 KB Output is correct
35 Correct 658 ms 89712 KB Output is correct
36 Correct 632 ms 89548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47312 KB Output is correct
2 Correct 23 ms 47188 KB Output is correct
3 Correct 24 ms 47292 KB Output is correct
4 Correct 24 ms 47296 KB Output is correct
5 Correct 25 ms 47260 KB Output is correct
6 Correct 24 ms 47284 KB Output is correct
7 Correct 26 ms 47248 KB Output is correct
8 Correct 24 ms 47200 KB Output is correct
9 Correct 25 ms 47320 KB Output is correct
10 Correct 25 ms 47304 KB Output is correct
11 Correct 26 ms 47292 KB Output is correct
12 Correct 24 ms 47268 KB Output is correct
13 Correct 24 ms 47316 KB Output is correct
14 Correct 24 ms 47288 KB Output is correct
15 Correct 24 ms 47204 KB Output is correct
16 Correct 24 ms 47188 KB Output is correct
17 Correct 26 ms 47692 KB Output is correct
18 Correct 27 ms 47740 KB Output is correct
19 Correct 28 ms 47780 KB Output is correct
20 Correct 29 ms 47676 KB Output is correct
21 Correct 32 ms 47700 KB Output is correct
22 Correct 27 ms 47700 KB Output is correct
23 Correct 27 ms 47616 KB Output is correct
24 Correct 239 ms 92344 KB Output is correct
25 Correct 314 ms 92248 KB Output is correct
26 Correct 283 ms 92432 KB Output is correct
27 Correct 332 ms 94980 KB Output is correct
28 Correct 359 ms 92600 KB Output is correct
29 Correct 264 ms 92996 KB Output is correct
30 Correct 732 ms 95944 KB Output is correct
31 Correct 149 ms 77728 KB Output is correct
32 Correct 97 ms 63432 KB Output is correct
33 Correct 452 ms 91104 KB Output is correct
34 Correct 570 ms 94928 KB Output is correct
35 Correct 658 ms 89712 KB Output is correct
36 Correct 632 ms 89548 KB Output is correct
37 Correct 307 ms 95492 KB Output is correct
38 Correct 355 ms 98016 KB Output is correct
39 Correct 300 ms 95896 KB Output is correct
40 Correct 300 ms 95976 KB Output is correct
41 Correct 24 ms 47264 KB Output is correct
42 Correct 727 ms 99292 KB Output is correct
43 Correct 438 ms 94032 KB Output is correct
44 Correct 583 ms 97504 KB Output is correct
45 Correct 690 ms 92820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47312 KB Output is correct
2 Correct 23 ms 47188 KB Output is correct
3 Correct 24 ms 47292 KB Output is correct
4 Correct 24 ms 47296 KB Output is correct
5 Correct 25 ms 47260 KB Output is correct
6 Correct 24 ms 47284 KB Output is correct
7 Correct 26 ms 47248 KB Output is correct
8 Correct 24 ms 47200 KB Output is correct
9 Correct 25 ms 47320 KB Output is correct
10 Correct 25 ms 47304 KB Output is correct
11 Correct 26 ms 47292 KB Output is correct
12 Correct 24 ms 47268 KB Output is correct
13 Correct 24 ms 47316 KB Output is correct
14 Correct 24 ms 47288 KB Output is correct
15 Correct 24 ms 47204 KB Output is correct
16 Correct 24 ms 47188 KB Output is correct
17 Correct 26 ms 47692 KB Output is correct
18 Correct 27 ms 47740 KB Output is correct
19 Correct 28 ms 47780 KB Output is correct
20 Correct 29 ms 47676 KB Output is correct
21 Correct 32 ms 47700 KB Output is correct
22 Correct 27 ms 47700 KB Output is correct
23 Correct 27 ms 47616 KB Output is correct
24 Correct 239 ms 92344 KB Output is correct
25 Correct 314 ms 92248 KB Output is correct
26 Correct 283 ms 92432 KB Output is correct
27 Correct 332 ms 94980 KB Output is correct
28 Correct 359 ms 92600 KB Output is correct
29 Correct 264 ms 92996 KB Output is correct
30 Correct 732 ms 95944 KB Output is correct
31 Correct 149 ms 77728 KB Output is correct
32 Correct 97 ms 63432 KB Output is correct
33 Correct 452 ms 91104 KB Output is correct
34 Correct 570 ms 94928 KB Output is correct
35 Correct 658 ms 89712 KB Output is correct
36 Correct 632 ms 89548 KB Output is correct
37 Correct 307 ms 95492 KB Output is correct
38 Correct 355 ms 98016 KB Output is correct
39 Correct 300 ms 95896 KB Output is correct
40 Correct 300 ms 95976 KB Output is correct
41 Correct 24 ms 47264 KB Output is correct
42 Correct 727 ms 99292 KB Output is correct
43 Correct 438 ms 94032 KB Output is correct
44 Correct 583 ms 97504 KB Output is correct
45 Correct 690 ms 92820 KB Output is correct
46 Correct 1524 ms 274980 KB Output is correct
47 Correct 1813 ms 288584 KB Output is correct
48 Correct 1482 ms 291164 KB Output is correct
49 Correct 1481 ms 291100 KB Output is correct
50 Correct 4642 ms 291988 KB Output is correct
51 Correct 2648 ms 277140 KB Output is correct
52 Correct 3353 ms 281488 KB Output is correct
53 Correct 4278 ms 272216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 86904 KB Output is correct
2 Correct 333 ms 98620 KB Output is correct
3 Correct 291 ms 95132 KB Output is correct
4 Correct 262 ms 95048 KB Output is correct
5 Correct 23 ms 47188 KB Output is correct
6 Correct 332 ms 96496 KB Output is correct
7 Correct 102 ms 77492 KB Output is correct
8 Correct 109 ms 65200 KB Output is correct
9 Correct 295 ms 96332 KB Output is correct
10 Correct 267 ms 92280 KB Output is correct
11 Correct 237 ms 89416 KB Output is correct
12 Correct 27 ms 47312 KB Output is correct
13 Correct 23 ms 47188 KB Output is correct
14 Correct 24 ms 47292 KB Output is correct
15 Correct 24 ms 47296 KB Output is correct
16 Correct 25 ms 47260 KB Output is correct
17 Correct 24 ms 47284 KB Output is correct
18 Correct 26 ms 47248 KB Output is correct
19 Correct 24 ms 47200 KB Output is correct
20 Correct 25 ms 47320 KB Output is correct
21 Correct 25 ms 47304 KB Output is correct
22 Correct 26 ms 47292 KB Output is correct
23 Correct 24 ms 47268 KB Output is correct
24 Correct 24 ms 47316 KB Output is correct
25 Correct 24 ms 47288 KB Output is correct
26 Correct 24 ms 47204 KB Output is correct
27 Correct 24 ms 47188 KB Output is correct
28 Correct 26 ms 47692 KB Output is correct
29 Correct 27 ms 47740 KB Output is correct
30 Correct 28 ms 47780 KB Output is correct
31 Correct 29 ms 47676 KB Output is correct
32 Correct 32 ms 47700 KB Output is correct
33 Correct 27 ms 47700 KB Output is correct
34 Correct 27 ms 47616 KB Output is correct
35 Correct 239 ms 92344 KB Output is correct
36 Correct 314 ms 92248 KB Output is correct
37 Correct 283 ms 92432 KB Output is correct
38 Correct 332 ms 94980 KB Output is correct
39 Correct 359 ms 92600 KB Output is correct
40 Correct 264 ms 92996 KB Output is correct
41 Correct 732 ms 95944 KB Output is correct
42 Correct 149 ms 77728 KB Output is correct
43 Correct 97 ms 63432 KB Output is correct
44 Correct 452 ms 91104 KB Output is correct
45 Correct 570 ms 94928 KB Output is correct
46 Correct 658 ms 89712 KB Output is correct
47 Correct 632 ms 89548 KB Output is correct
48 Correct 307 ms 95492 KB Output is correct
49 Correct 355 ms 98016 KB Output is correct
50 Correct 300 ms 95896 KB Output is correct
51 Correct 300 ms 95976 KB Output is correct
52 Correct 24 ms 47264 KB Output is correct
53 Correct 727 ms 99292 KB Output is correct
54 Correct 438 ms 94032 KB Output is correct
55 Correct 583 ms 97504 KB Output is correct
56 Correct 690 ms 92820 KB Output is correct
57 Correct 317 ms 95864 KB Output is correct
58 Correct 382 ms 98540 KB Output is correct
59 Correct 307 ms 96968 KB Output is correct
60 Correct 310 ms 96956 KB Output is correct
61 Correct 723 ms 96332 KB Output is correct
62 Correct 25 ms 47288 KB Output is correct
63 Correct 703 ms 99216 KB Output is correct
64 Correct 433 ms 93396 KB Output is correct
65 Correct 578 ms 98340 KB Output is correct
66 Correct 644 ms 92680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 86904 KB Output is correct
2 Correct 333 ms 98620 KB Output is correct
3 Correct 291 ms 95132 KB Output is correct
4 Correct 262 ms 95048 KB Output is correct
5 Correct 23 ms 47188 KB Output is correct
6 Correct 332 ms 96496 KB Output is correct
7 Correct 102 ms 77492 KB Output is correct
8 Correct 109 ms 65200 KB Output is correct
9 Correct 295 ms 96332 KB Output is correct
10 Correct 267 ms 92280 KB Output is correct
11 Correct 237 ms 89416 KB Output is correct
12 Correct 27 ms 47312 KB Output is correct
13 Correct 23 ms 47188 KB Output is correct
14 Correct 24 ms 47292 KB Output is correct
15 Correct 24 ms 47296 KB Output is correct
16 Correct 25 ms 47260 KB Output is correct
17 Correct 24 ms 47284 KB Output is correct
18 Correct 26 ms 47248 KB Output is correct
19 Correct 24 ms 47200 KB Output is correct
20 Correct 25 ms 47320 KB Output is correct
21 Correct 25 ms 47304 KB Output is correct
22 Correct 26 ms 47292 KB Output is correct
23 Correct 24 ms 47268 KB Output is correct
24 Correct 24 ms 47316 KB Output is correct
25 Correct 24 ms 47288 KB Output is correct
26 Correct 24 ms 47204 KB Output is correct
27 Correct 24 ms 47188 KB Output is correct
28 Correct 26 ms 47692 KB Output is correct
29 Correct 27 ms 47740 KB Output is correct
30 Correct 28 ms 47780 KB Output is correct
31 Correct 29 ms 47676 KB Output is correct
32 Correct 32 ms 47700 KB Output is correct
33 Correct 27 ms 47700 KB Output is correct
34 Correct 27 ms 47616 KB Output is correct
35 Correct 239 ms 92344 KB Output is correct
36 Correct 314 ms 92248 KB Output is correct
37 Correct 283 ms 92432 KB Output is correct
38 Correct 332 ms 94980 KB Output is correct
39 Correct 359 ms 92600 KB Output is correct
40 Correct 264 ms 92996 KB Output is correct
41 Correct 732 ms 95944 KB Output is correct
42 Correct 149 ms 77728 KB Output is correct
43 Correct 97 ms 63432 KB Output is correct
44 Correct 452 ms 91104 KB Output is correct
45 Correct 570 ms 94928 KB Output is correct
46 Correct 658 ms 89712 KB Output is correct
47 Correct 632 ms 89548 KB Output is correct
48 Correct 307 ms 95492 KB Output is correct
49 Correct 355 ms 98016 KB Output is correct
50 Correct 300 ms 95896 KB Output is correct
51 Correct 300 ms 95976 KB Output is correct
52 Correct 24 ms 47264 KB Output is correct
53 Correct 727 ms 99292 KB Output is correct
54 Correct 438 ms 94032 KB Output is correct
55 Correct 583 ms 97504 KB Output is correct
56 Correct 690 ms 92820 KB Output is correct
57 Correct 1524 ms 274980 KB Output is correct
58 Correct 1813 ms 288584 KB Output is correct
59 Correct 1482 ms 291164 KB Output is correct
60 Correct 1481 ms 291100 KB Output is correct
61 Correct 4642 ms 291988 KB Output is correct
62 Correct 2648 ms 277140 KB Output is correct
63 Correct 3353 ms 281488 KB Output is correct
64 Correct 4278 ms 272216 KB Output is correct
65 Correct 317 ms 95864 KB Output is correct
66 Correct 382 ms 98540 KB Output is correct
67 Correct 307 ms 96968 KB Output is correct
68 Correct 310 ms 96956 KB Output is correct
69 Correct 723 ms 96332 KB Output is correct
70 Correct 25 ms 47288 KB Output is correct
71 Correct 703 ms 99216 KB Output is correct
72 Correct 433 ms 93396 KB Output is correct
73 Correct 578 ms 98340 KB Output is correct
74 Correct 644 ms 92680 KB Output is correct
75 Correct 1509 ms 284220 KB Output is correct
76 Correct 1810 ms 294292 KB Output is correct
77 Correct 1485 ms 287328 KB Output is correct
78 Correct 1501 ms 287720 KB Output is correct
79 Correct 4777 ms 299276 KB Output is correct
80 Correct 2646 ms 269940 KB Output is correct
81 Correct 3356 ms 284880 KB Output is correct
82 Correct 4531 ms 273624 KB Output is correct
83 Correct 4494 ms 288304 KB Output is correct