Submission #621697

# Submission time Handle Problem Language Result Execution time Memory
621697 2022-08-04T01:52:51 Z czhang2718 Two Dishes (JOI19_dishes) C++17
100 / 100
7181 ms 339436 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef vector<int> vi;
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define rep(i,a,b) for(int i=a; i<=b; i++)

const int N=1e6+1;
int n, m;
ll a[N], b[N], p[N], q[N], s[N], t[N];
map<int,ll> qu[N];

bool st[4*N];
ll seg[4*N];
ll mn[4*N];

void push(int x, int lx, int rx){
    if(rx-lx==1) return;
    if(st[x]){
        seg[2*x+1]=seg[2*x+2]=seg[x];
        st[2*x+1]=st[2*x+2]=1;
        mn[2*x+1]=mn[2*x+2]=seg[x];
        seg[x]=0;
        st[x]=0;
        return;
    }
    seg[2*x+1]+=seg[x];
    seg[2*x+2]+=seg[x];
    mn[2*x+1]+=seg[x];
    mn[2*x+2]+=seg[x];
    seg[x]=0;
}

void add(int l, int r, int x, int lx, int rx, ll v){
    // cout << "add " << l << r << " " << v << "\n";
    push(x, lx, rx);
    if(lx>=l && rx<=r){
        seg[x]+=v;
        mn[x]+=v;
        return;
    }
    if(lx>=r || rx<=l) return;
    int mid=(lx+rx)/2;
    add(l, r, 2*x+1, lx, mid, v);
    add(l, r, 2*x+2, mid, rx, v);
    mn[x]=min(mn[2*x+1], mn[2*x+2]);
}

void add(int l, int r, ll v){
    add(l, r, 0, 0, m+1, v);
}

void assign(int l, int r, ll v, int x, int lx, int rx){
    push(x, lx, rx);
    if(lx>=l && rx<=r){
        // cout << "seg " << lx << " " << rx << " = " << v << "\n";
        seg[x]=v;
        st[x]=1;
        mn[x]=v;
        return;
    }
    if(lx>=r || rx<=l) return;
    int mid=(lx+rx)/2;
    assign(l, r, v, 2*x+1, lx, mid);
    assign(l, r, v, 2*x+2, mid, rx);
    mn[x]=min(mn[2*x+1], mn[2*x+2]);
}

void assign(int l, int r, ll v){
    if(r<=l) return;
    assign(l, r, v, 0, 0, m+1);
}

void assign(int i, ll v){
    assign(i, i+1, v);
}


int walk(ll v, int x, int lx, int rx){
    if(rx-lx==1) return rx;
    push(x, lx, rx);
    int mid=(lx+rx)/2;
    if(mn[2*x+2]<=v) return walk(v, 2*x+2, mid, rx);
    return walk(v, 2*x+1, lx, mid);
}

int walk(ll v){
    return walk(v, 0, 0, m+1);
}

ll qry(int i, int x, int lx, int rx){
    if(rx-lx==1) return mn[x];
    push(x, lx, rx);
    int mid=(lx+rx)/2;
    if(i<mid) return qry(i, 2*x+1, lx, mid);
    return qry(i, 2*x+2, mid, rx);
}
 
ll qry(int i){
    return qry(i, 0, 0, m+1);
}

int main(){
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> m;
    ll ans=0;
    vector<vi> pts;
    vector<ll> A(n+1), B(m+1);

    rep(i,1,n){
        cin >> a[i] >> s[i] >> p[i];
        A[i]=A[i-1]+a[i];
    }
    

    A.erase(A.begin());
    rep(i,1,m){
        cin >> b[i] >> t[i] >> q[i];
        B[i]=B[i-1]+b[i];
        int j=upper_bound(all(A), t[i]-B[i])-A.begin();
        if(B[i]<=t[i]) qu[j][i]+=q[i];
    }


    B.erase(B.begin());
    rep(i,1,n){
        int j=upper_bound(all(B), s[i]-A[i-1])-B.begin();
        if(A[i-1]<=s[i]){
            ans+=p[i];
            qu[i-1][j+1]-=p[i];
        }
    }

    rep(i,0,n){
        vector<pii> pts;
        for(auto p:qu[i]) pts.push_back(p);
        reverse(all(pts)); 
        // cout << "x " << i << ":\n";
        for(auto p:pts){
            int y=p.f;
            int v=p.s;
            // cout << y << " " << v << "\n";
            add(y, m+1, v);
            // rep(i,0,m) cout << qry(i) << " ";
            // cout << "\n";
            if(i!=n){
                ll prv=qry(y-1);
                int j=walk(prv); // rightmost <=prv
                assign(y, j, prv);
                // rep(i,0,m) cout << qry(i) << " ";
                // cout << "\n";
            }
        }
    }
    cout << ans+qry(m);
}

// A: (x, <=y)
// B: (x, >=y)

# Verdict Execution time Memory Grader output
1 Correct 428 ms 85544 KB Output is correct
2 Correct 585 ms 89076 KB Output is correct
3 Correct 493 ms 99184 KB Output is correct
4 Correct 391 ms 75732 KB Output is correct
5 Correct 27 ms 47332 KB Output is correct
6 Correct 433 ms 89684 KB Output is correct
7 Correct 266 ms 78712 KB Output is correct
8 Correct 143 ms 69472 KB Output is correct
9 Correct 506 ms 99108 KB Output is correct
10 Correct 375 ms 84516 KB Output is correct
11 Correct 383 ms 99828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47332 KB Output is correct
2 Correct 31 ms 47320 KB Output is correct
3 Correct 27 ms 47316 KB Output is correct
4 Correct 25 ms 47308 KB Output is correct
5 Correct 33 ms 47276 KB Output is correct
6 Correct 29 ms 47336 KB Output is correct
7 Correct 31 ms 47304 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47316 KB Output is correct
10 Correct 25 ms 47328 KB Output is correct
11 Correct 32 ms 47236 KB Output is correct
12 Correct 27 ms 47296 KB Output is correct
13 Correct 23 ms 47308 KB Output is correct
14 Correct 22 ms 47316 KB Output is correct
15 Correct 32 ms 47320 KB Output is correct
16 Correct 29 ms 47340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47332 KB Output is correct
2 Correct 31 ms 47320 KB Output is correct
3 Correct 27 ms 47316 KB Output is correct
4 Correct 25 ms 47308 KB Output is correct
5 Correct 33 ms 47276 KB Output is correct
6 Correct 29 ms 47336 KB Output is correct
7 Correct 31 ms 47304 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47316 KB Output is correct
10 Correct 25 ms 47328 KB Output is correct
11 Correct 32 ms 47236 KB Output is correct
12 Correct 27 ms 47296 KB Output is correct
13 Correct 23 ms 47308 KB Output is correct
14 Correct 22 ms 47316 KB Output is correct
15 Correct 32 ms 47320 KB Output is correct
16 Correct 29 ms 47340 KB Output is correct
17 Correct 29 ms 47836 KB Output is correct
18 Correct 32 ms 47836 KB Output is correct
19 Correct 29 ms 47828 KB Output is correct
20 Correct 29 ms 47636 KB Output is correct
21 Correct 30 ms 47916 KB Output is correct
22 Correct 35 ms 47808 KB Output is correct
23 Correct 28 ms 47784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47332 KB Output is correct
2 Correct 31 ms 47320 KB Output is correct
3 Correct 27 ms 47316 KB Output is correct
4 Correct 25 ms 47308 KB Output is correct
5 Correct 33 ms 47276 KB Output is correct
6 Correct 29 ms 47336 KB Output is correct
7 Correct 31 ms 47304 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47316 KB Output is correct
10 Correct 25 ms 47328 KB Output is correct
11 Correct 32 ms 47236 KB Output is correct
12 Correct 27 ms 47296 KB Output is correct
13 Correct 23 ms 47308 KB Output is correct
14 Correct 22 ms 47316 KB Output is correct
15 Correct 32 ms 47320 KB Output is correct
16 Correct 29 ms 47340 KB Output is correct
17 Correct 29 ms 47836 KB Output is correct
18 Correct 32 ms 47836 KB Output is correct
19 Correct 29 ms 47828 KB Output is correct
20 Correct 29 ms 47636 KB Output is correct
21 Correct 30 ms 47916 KB Output is correct
22 Correct 35 ms 47808 KB Output is correct
23 Correct 28 ms 47784 KB Output is correct
24 Correct 481 ms 92132 KB Output is correct
25 Correct 397 ms 93112 KB Output is correct
26 Correct 510 ms 92456 KB Output is correct
27 Correct 418 ms 89756 KB Output is correct
28 Correct 537 ms 95556 KB Output is correct
29 Correct 417 ms 99220 KB Output is correct
30 Correct 1049 ms 96112 KB Output is correct
31 Correct 255 ms 81428 KB Output is correct
32 Correct 151 ms 69512 KB Output is correct
33 Correct 540 ms 84076 KB Output is correct
34 Correct 724 ms 96316 KB Output is correct
35 Correct 938 ms 96856 KB Output is correct
36 Correct 955 ms 96580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47332 KB Output is correct
2 Correct 31 ms 47320 KB Output is correct
3 Correct 27 ms 47316 KB Output is correct
4 Correct 25 ms 47308 KB Output is correct
5 Correct 33 ms 47276 KB Output is correct
6 Correct 29 ms 47336 KB Output is correct
7 Correct 31 ms 47304 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47316 KB Output is correct
10 Correct 25 ms 47328 KB Output is correct
11 Correct 32 ms 47236 KB Output is correct
12 Correct 27 ms 47296 KB Output is correct
13 Correct 23 ms 47308 KB Output is correct
14 Correct 22 ms 47316 KB Output is correct
15 Correct 32 ms 47320 KB Output is correct
16 Correct 29 ms 47340 KB Output is correct
17 Correct 29 ms 47836 KB Output is correct
18 Correct 32 ms 47836 KB Output is correct
19 Correct 29 ms 47828 KB Output is correct
20 Correct 29 ms 47636 KB Output is correct
21 Correct 30 ms 47916 KB Output is correct
22 Correct 35 ms 47808 KB Output is correct
23 Correct 28 ms 47784 KB Output is correct
24 Correct 481 ms 92132 KB Output is correct
25 Correct 397 ms 93112 KB Output is correct
26 Correct 510 ms 92456 KB Output is correct
27 Correct 418 ms 89756 KB Output is correct
28 Correct 537 ms 95556 KB Output is correct
29 Correct 417 ms 99220 KB Output is correct
30 Correct 1049 ms 96112 KB Output is correct
31 Correct 255 ms 81428 KB Output is correct
32 Correct 151 ms 69512 KB Output is correct
33 Correct 540 ms 84076 KB Output is correct
34 Correct 724 ms 96316 KB Output is correct
35 Correct 938 ms 96856 KB Output is correct
36 Correct 955 ms 96580 KB Output is correct
37 Correct 601 ms 92288 KB Output is correct
38 Correct 525 ms 89648 KB Output is correct
39 Correct 411 ms 83760 KB Output is correct
40 Correct 425 ms 84028 KB Output is correct
41 Correct 30 ms 47332 KB Output is correct
42 Correct 997 ms 96056 KB Output is correct
43 Correct 537 ms 94320 KB Output is correct
44 Correct 825 ms 106568 KB Output is correct
45 Correct 1006 ms 100716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47332 KB Output is correct
2 Correct 31 ms 47320 KB Output is correct
3 Correct 27 ms 47316 KB Output is correct
4 Correct 25 ms 47308 KB Output is correct
5 Correct 33 ms 47276 KB Output is correct
6 Correct 29 ms 47336 KB Output is correct
7 Correct 31 ms 47304 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47316 KB Output is correct
10 Correct 25 ms 47328 KB Output is correct
11 Correct 32 ms 47236 KB Output is correct
12 Correct 27 ms 47296 KB Output is correct
13 Correct 23 ms 47308 KB Output is correct
14 Correct 22 ms 47316 KB Output is correct
15 Correct 32 ms 47320 KB Output is correct
16 Correct 29 ms 47340 KB Output is correct
17 Correct 29 ms 47836 KB Output is correct
18 Correct 32 ms 47836 KB Output is correct
19 Correct 29 ms 47828 KB Output is correct
20 Correct 29 ms 47636 KB Output is correct
21 Correct 30 ms 47916 KB Output is correct
22 Correct 35 ms 47808 KB Output is correct
23 Correct 28 ms 47784 KB Output is correct
24 Correct 481 ms 92132 KB Output is correct
25 Correct 397 ms 93112 KB Output is correct
26 Correct 510 ms 92456 KB Output is correct
27 Correct 418 ms 89756 KB Output is correct
28 Correct 537 ms 95556 KB Output is correct
29 Correct 417 ms 99220 KB Output is correct
30 Correct 1049 ms 96112 KB Output is correct
31 Correct 255 ms 81428 KB Output is correct
32 Correct 151 ms 69512 KB Output is correct
33 Correct 540 ms 84076 KB Output is correct
34 Correct 724 ms 96316 KB Output is correct
35 Correct 938 ms 96856 KB Output is correct
36 Correct 955 ms 96580 KB Output is correct
37 Correct 601 ms 92288 KB Output is correct
38 Correct 525 ms 89648 KB Output is correct
39 Correct 411 ms 83760 KB Output is correct
40 Correct 425 ms 84028 KB Output is correct
41 Correct 30 ms 47332 KB Output is correct
42 Correct 997 ms 96056 KB Output is correct
43 Correct 537 ms 94320 KB Output is correct
44 Correct 825 ms 106568 KB Output is correct
45 Correct 1006 ms 100716 KB Output is correct
46 Correct 2932 ms 252232 KB Output is correct
47 Correct 1985 ms 241692 KB Output is correct
48 Correct 1795 ms 210576 KB Output is correct
49 Correct 2931 ms 211368 KB Output is correct
50 Correct 7019 ms 274540 KB Output is correct
51 Correct 3936 ms 271872 KB Output is correct
52 Correct 5294 ms 328256 KB Output is correct
53 Correct 6696 ms 305928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 428 ms 85544 KB Output is correct
2 Correct 585 ms 89076 KB Output is correct
3 Correct 493 ms 99184 KB Output is correct
4 Correct 391 ms 75732 KB Output is correct
5 Correct 27 ms 47332 KB Output is correct
6 Correct 433 ms 89684 KB Output is correct
7 Correct 266 ms 78712 KB Output is correct
8 Correct 143 ms 69472 KB Output is correct
9 Correct 506 ms 99108 KB Output is correct
10 Correct 375 ms 84516 KB Output is correct
11 Correct 383 ms 99828 KB Output is correct
12 Correct 27 ms 47332 KB Output is correct
13 Correct 31 ms 47320 KB Output is correct
14 Correct 27 ms 47316 KB Output is correct
15 Correct 25 ms 47308 KB Output is correct
16 Correct 33 ms 47276 KB Output is correct
17 Correct 29 ms 47336 KB Output is correct
18 Correct 31 ms 47304 KB Output is correct
19 Correct 28 ms 47360 KB Output is correct
20 Correct 28 ms 47316 KB Output is correct
21 Correct 25 ms 47328 KB Output is correct
22 Correct 32 ms 47236 KB Output is correct
23 Correct 27 ms 47296 KB Output is correct
24 Correct 23 ms 47308 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 32 ms 47320 KB Output is correct
27 Correct 29 ms 47340 KB Output is correct
28 Correct 29 ms 47836 KB Output is correct
29 Correct 32 ms 47836 KB Output is correct
30 Correct 29 ms 47828 KB Output is correct
31 Correct 29 ms 47636 KB Output is correct
32 Correct 30 ms 47916 KB Output is correct
33 Correct 35 ms 47808 KB Output is correct
34 Correct 28 ms 47784 KB Output is correct
35 Correct 481 ms 92132 KB Output is correct
36 Correct 397 ms 93112 KB Output is correct
37 Correct 510 ms 92456 KB Output is correct
38 Correct 418 ms 89756 KB Output is correct
39 Correct 537 ms 95556 KB Output is correct
40 Correct 417 ms 99220 KB Output is correct
41 Correct 1049 ms 96112 KB Output is correct
42 Correct 255 ms 81428 KB Output is correct
43 Correct 151 ms 69512 KB Output is correct
44 Correct 540 ms 84076 KB Output is correct
45 Correct 724 ms 96316 KB Output is correct
46 Correct 938 ms 96856 KB Output is correct
47 Correct 955 ms 96580 KB Output is correct
48 Correct 601 ms 92288 KB Output is correct
49 Correct 525 ms 89648 KB Output is correct
50 Correct 411 ms 83760 KB Output is correct
51 Correct 425 ms 84028 KB Output is correct
52 Correct 30 ms 47332 KB Output is correct
53 Correct 997 ms 96056 KB Output is correct
54 Correct 537 ms 94320 KB Output is correct
55 Correct 825 ms 106568 KB Output is correct
56 Correct 1006 ms 100716 KB Output is correct
57 Correct 524 ms 101120 KB Output is correct
58 Correct 502 ms 99416 KB Output is correct
59 Correct 584 ms 93064 KB Output is correct
60 Correct 355 ms 92576 KB Output is correct
61 Correct 1025 ms 104224 KB Output is correct
62 Correct 35 ms 47336 KB Output is correct
63 Correct 915 ms 105280 KB Output is correct
64 Correct 724 ms 92848 KB Output is correct
65 Correct 1032 ms 105896 KB Output is correct
66 Correct 845 ms 100640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 428 ms 85544 KB Output is correct
2 Correct 585 ms 89076 KB Output is correct
3 Correct 493 ms 99184 KB Output is correct
4 Correct 391 ms 75732 KB Output is correct
5 Correct 27 ms 47332 KB Output is correct
6 Correct 433 ms 89684 KB Output is correct
7 Correct 266 ms 78712 KB Output is correct
8 Correct 143 ms 69472 KB Output is correct
9 Correct 506 ms 99108 KB Output is correct
10 Correct 375 ms 84516 KB Output is correct
11 Correct 383 ms 99828 KB Output is correct
12 Correct 27 ms 47332 KB Output is correct
13 Correct 31 ms 47320 KB Output is correct
14 Correct 27 ms 47316 KB Output is correct
15 Correct 25 ms 47308 KB Output is correct
16 Correct 33 ms 47276 KB Output is correct
17 Correct 29 ms 47336 KB Output is correct
18 Correct 31 ms 47304 KB Output is correct
19 Correct 28 ms 47360 KB Output is correct
20 Correct 28 ms 47316 KB Output is correct
21 Correct 25 ms 47328 KB Output is correct
22 Correct 32 ms 47236 KB Output is correct
23 Correct 27 ms 47296 KB Output is correct
24 Correct 23 ms 47308 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 32 ms 47320 KB Output is correct
27 Correct 29 ms 47340 KB Output is correct
28 Correct 29 ms 47836 KB Output is correct
29 Correct 32 ms 47836 KB Output is correct
30 Correct 29 ms 47828 KB Output is correct
31 Correct 29 ms 47636 KB Output is correct
32 Correct 30 ms 47916 KB Output is correct
33 Correct 35 ms 47808 KB Output is correct
34 Correct 28 ms 47784 KB Output is correct
35 Correct 481 ms 92132 KB Output is correct
36 Correct 397 ms 93112 KB Output is correct
37 Correct 510 ms 92456 KB Output is correct
38 Correct 418 ms 89756 KB Output is correct
39 Correct 537 ms 95556 KB Output is correct
40 Correct 417 ms 99220 KB Output is correct
41 Correct 1049 ms 96112 KB Output is correct
42 Correct 255 ms 81428 KB Output is correct
43 Correct 151 ms 69512 KB Output is correct
44 Correct 540 ms 84076 KB Output is correct
45 Correct 724 ms 96316 KB Output is correct
46 Correct 938 ms 96856 KB Output is correct
47 Correct 955 ms 96580 KB Output is correct
48 Correct 601 ms 92288 KB Output is correct
49 Correct 525 ms 89648 KB Output is correct
50 Correct 411 ms 83760 KB Output is correct
51 Correct 425 ms 84028 KB Output is correct
52 Correct 30 ms 47332 KB Output is correct
53 Correct 997 ms 96056 KB Output is correct
54 Correct 537 ms 94320 KB Output is correct
55 Correct 825 ms 106568 KB Output is correct
56 Correct 1006 ms 100716 KB Output is correct
57 Correct 2932 ms 252232 KB Output is correct
58 Correct 1985 ms 241692 KB Output is correct
59 Correct 1795 ms 210576 KB Output is correct
60 Correct 2931 ms 211368 KB Output is correct
61 Correct 7019 ms 274540 KB Output is correct
62 Correct 3936 ms 271872 KB Output is correct
63 Correct 5294 ms 328256 KB Output is correct
64 Correct 6696 ms 305928 KB Output is correct
65 Correct 524 ms 101120 KB Output is correct
66 Correct 502 ms 99416 KB Output is correct
67 Correct 584 ms 93064 KB Output is correct
68 Correct 355 ms 92576 KB Output is correct
69 Correct 1025 ms 104224 KB Output is correct
70 Correct 35 ms 47336 KB Output is correct
71 Correct 915 ms 105280 KB Output is correct
72 Correct 724 ms 92848 KB Output is correct
73 Correct 1032 ms 105896 KB Output is correct
74 Correct 845 ms 100640 KB Output is correct
75 Correct 2256 ms 316180 KB Output is correct
76 Correct 2820 ms 310404 KB Output is correct
77 Correct 2377 ms 266276 KB Output is correct
78 Correct 1605 ms 264176 KB Output is correct
79 Correct 6511 ms 339436 KB Output is correct
80 Correct 3035 ms 271044 KB Output is correct
81 Correct 5048 ms 334476 KB Output is correct
82 Correct 7181 ms 306644 KB Output is correct
83 Correct 6619 ms 324312 KB Output is correct