Submission #767854

# Submission time Handle Problem Language Result Execution time Memory
767854 2023-06-27T08:30:38 Z 79brue Two Dishes (JOI19_dishes) C++17
74 / 100
3561 ms 181704 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll INF = 1e18;

void input();
void solve();

int main(){
    input();
    solve();
}

struct Point{
    int x; ll v;
    Point(){}
    Point(int x, ll v): x(x), v(v){}
};

int n, m;
Point a[1000002], b[1000002];

void input(){
    scanf("%d %d", &n, &m);
    vector<ll> A (n+1), S (n+1), P(n+1);
    vector<ll> B (m+1), T (m+1), Q(m+1);
    vector<ll> Asum(n+1), Bsum(m+1);
    for(int i=1; i<=n; i++) scanf("%lld %lld %lld", &A[i], &S[i], &P[i]);
    for(int i=1; i<=m; i++) scanf("%lld %lld %lld", &B[i], &T[i], &Q[i]);
    for(int i=1; i<=n; i++) Asum[i] = Asum[i-1] + A[i];
    for(int i=1; i<=m; i++) Bsum[i] = Bsum[i-1] + B[i];

    for(int i=1; i<=n; i++) a[i] = Point(upper_bound(Bsum.begin(), Bsum.end(), S[i] - Asum[i]) - Bsum.begin() - 1, P[i]);
    for(int i=1; i<=m; i++) b[i] = Point(upper_bound(Asum.begin(), Asum.end(), T[i] - Bsum[i]) - Asum.begin() - 1, Q[i]);
}

struct segTree{
    struct Node{
        ll val, lazy; /// 구간 최대 val, 거기에 lazy
        ll extra, ans; /// extra: 구간 extra의 합, ans: 구간 적당한 점 val + 그 오른쪽 extra 합의 최댓값

        Node(){}
        Node(ll val, ll extra, ll ans): val(val), extra(extra), ans(ans){lazy = 0;}
        Node operator+(const Node &r)const{
            return Node(max(val, r.val), extra + r.extra, max(ans + r.extra, r.ans));
        }
    } tree[1<<21];

    void init(int i, int l, int r){
        tree[i].val = tree[i].lazy = tree[i].extra = tree[i].ans = 0;
        if(l==r) return;
        int m = (l+r)>>1;
        init(i*2, l, m);
        init(i*2+1, m+1, r);
    }

    void propagate(int i, int l, int r){
        tree[i].val += tree[i].lazy;
        tree[i].ans += tree[i].lazy;
        if(l!=r){
            tree[i*2].lazy += tree[i].lazy;
            tree[i*2+1].lazy += tree[i].lazy;
        }
        tree[i].lazy = 0;
    }

    Node query(int i, int l, int r, int s, int e){ /// 구간의 ans를 확인
        propagate(i, l, r);
        if(r<s || e<l) return Node(-INF, 0, -INF);
        if(s<=l && r<=e) return tree[i];
        int m = (l+r)>>1;
        return query(i*2, l, m, s, e) + query(i*2+1, m+1, r, s, e);
    }

    void updateVal(int i, int l, int r, int x, ll v){ /// 한 위치의 val을 수정
        propagate(i, l, r);
        if(x<l || r<x) return;
        if(l==r){
            tree[i].val = tree[i].ans = v;
            return;
        }
        int m = (l+r)>>1;
        updateVal(i*2, l, m, x, v);
        updateVal(i*2+1, m+1, r, x, v);
        tree[i] = tree[i*2] + tree[i*2+1];
    }

    void updateExtra(int i, int l, int r, int x, ll v){ /// 한 위치의 extra를 수정
        propagate(i, l, r);
        if(x<l || r<x) return;
        if(l==r){
            tree[i].extra = v;
            return;
        }
        int m = (l+r)>>1;
        updateExtra(i*2, l, m, x, v);
        updateExtra(i*2+1, m+1, r, x, v);
        tree[i] = tree[i*2] + tree[i*2+1];
    }

    void add(int i, int l, int r, int s, int e, ll v){ /// 구간의 val을 수정
        propagate(i, l, r);
        if(r<s || e<l) return;
        if(s<=l && r<=e){
            tree[i].lazy = v;
            propagate(i, l, r);
            return;
        }
        int m = (l+r)>>1;
        add(i*2, l, m, s, e, v);
        add(i*2+1, m+1, r, s, e, v);
        tree[i] = tree[i*2] + tree[i*2+1];
    }
} tree;

struct Event{
    int type; /// 0: 가로, 1: 세로
    int time; /// 시간
    int x; /// 추가 인자
    ll v; /// 막대 길이

    Event(int type, int time, int x, ll v): type(type), time(time), x(x), v(v){}
    bool operator<(const Event &r)const{
        if(time != r.time) return time < r.time;
        if(type != r.type) return type < r.type;
        return x < r.x;
    }
};

void solve(){
    vector<Event> vec;
    for(int i=1; i<=n; i++){
        if(a[i].x < 0) continue;
        vec.push_back(Event(2, a[i].x, i, a[i].v));
    }
    for(int i=1; i<=m; i++){
        if(b[i].x < 0) continue;
        vec.push_back(Event(0, i, b[i].x, b[i].v));
        if(b[i].x < n) vec.push_back(Event(1, i-1, b[i].x+1, 0));
    }
    sort(vec.begin(), vec.end());

    tree.init(1, 0, n);
    for(int i=1; i<=n; i++){
        if(a[i].x < 0) continue;
        tree.updateExtra(1, 0, n, i, a[i].v);
    }
    for(Event &p: vec){
        if(p.type == 0){ /// 가로 막대
            tree.add(1, 0, n, 0, p.x, p.v);
        }
        else{ /// 세로 막대
//            printf("Query %d %lld: value %lld\n", p.x, p.v, v);
            if(p.type == 2){
                ll v = tree.query(1, 0, n, 0, p.x).ans;
                tree.updateExtra(1, 0, n, p.x, 0);
                tree.updateVal(1, 0, n, p.x, v);
            }
            else{
                ll v = tree.query(1, 0, n, 0, p.x).ans;
                tree.updateVal(1, 0, n, p.x, v);
            }
        }
    }

    printf("%lld", tree.query(1, 0, n, 0, n).ans);
}

Compilation message

dishes.cpp: In function 'void input()':
dishes.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
dishes.cpp:30:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     for(int i=1; i<=n; i++) scanf("%lld %lld %lld", &A[i], &S[i], &P[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dishes.cpp:31:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     for(int i=1; i<=m; i++) scanf("%lld %lld %lld", &B[i], &T[i], &Q[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 440 ms 37900 KB Output is correct
2 Correct 422 ms 41100 KB Output is correct
3 Correct 287 ms 37464 KB Output is correct
4 Correct 335 ms 36476 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 397 ms 41444 KB Output is correct
7 Correct 65 ms 12364 KB Output is correct
8 Correct 231 ms 27356 KB Output is correct
9 Correct 287 ms 37468 KB Output is correct
10 Correct 464 ms 39848 KB Output is correct
11 Correct 283 ms 35116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 788 KB Output is correct
19 Correct 5 ms 788 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 788 KB Output is correct
22 Correct 4 ms 788 KB Output is correct
23 Correct 4 ms 776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 788 KB Output is correct
19 Correct 5 ms 788 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 788 KB Output is correct
22 Correct 4 ms 788 KB Output is correct
23 Correct 4 ms 776 KB Output is correct
24 Correct 273 ms 34024 KB Output is correct
25 Correct 348 ms 38784 KB Output is correct
26 Correct 288 ms 34160 KB Output is correct
27 Correct 331 ms 38976 KB Output is correct
28 Correct 353 ms 36620 KB Output is correct
29 Correct 317 ms 37092 KB Output is correct
30 Correct 600 ms 40804 KB Output is correct
31 Correct 69 ms 18612 KB Output is correct
32 Correct 216 ms 27364 KB Output is correct
33 Correct 375 ms 33572 KB Output is correct
34 Correct 528 ms 39584 KB Output is correct
35 Correct 557 ms 39560 KB Output is correct
36 Correct 542 ms 39164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 788 KB Output is correct
19 Correct 5 ms 788 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 788 KB Output is correct
22 Correct 4 ms 788 KB Output is correct
23 Correct 4 ms 776 KB Output is correct
24 Correct 273 ms 34024 KB Output is correct
25 Correct 348 ms 38784 KB Output is correct
26 Correct 288 ms 34160 KB Output is correct
27 Correct 331 ms 38976 KB Output is correct
28 Correct 353 ms 36620 KB Output is correct
29 Correct 317 ms 37092 KB Output is correct
30 Correct 600 ms 40804 KB Output is correct
31 Correct 69 ms 18612 KB Output is correct
32 Correct 216 ms 27364 KB Output is correct
33 Correct 375 ms 33572 KB Output is correct
34 Correct 528 ms 39584 KB Output is correct
35 Correct 557 ms 39560 KB Output is correct
36 Correct 542 ms 39164 KB Output is correct
37 Correct 305 ms 34752 KB Output is correct
38 Correct 348 ms 39576 KB Output is correct
39 Correct 473 ms 41372 KB Output is correct
40 Correct 488 ms 41096 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 580 ms 41096 KB Output is correct
43 Correct 383 ms 34196 KB Output is correct
44 Correct 545 ms 39900 KB Output is correct
45 Correct 577 ms 39304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 788 KB Output is correct
19 Correct 5 ms 788 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 788 KB Output is correct
22 Correct 4 ms 788 KB Output is correct
23 Correct 4 ms 776 KB Output is correct
24 Correct 273 ms 34024 KB Output is correct
25 Correct 348 ms 38784 KB Output is correct
26 Correct 288 ms 34160 KB Output is correct
27 Correct 331 ms 38976 KB Output is correct
28 Correct 353 ms 36620 KB Output is correct
29 Correct 317 ms 37092 KB Output is correct
30 Correct 600 ms 40804 KB Output is correct
31 Correct 69 ms 18612 KB Output is correct
32 Correct 216 ms 27364 KB Output is correct
33 Correct 375 ms 33572 KB Output is correct
34 Correct 528 ms 39584 KB Output is correct
35 Correct 557 ms 39560 KB Output is correct
36 Correct 542 ms 39164 KB Output is correct
37 Correct 305 ms 34752 KB Output is correct
38 Correct 348 ms 39576 KB Output is correct
39 Correct 473 ms 41372 KB Output is correct
40 Correct 488 ms 41096 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 580 ms 41096 KB Output is correct
43 Correct 383 ms 34196 KB Output is correct
44 Correct 545 ms 39900 KB Output is correct
45 Correct 577 ms 39304 KB Output is correct
46 Correct 1687 ms 147168 KB Output is correct
47 Correct 1968 ms 170484 KB Output is correct
48 Correct 2602 ms 181704 KB Output is correct
49 Correct 2596 ms 181532 KB Output is correct
50 Correct 3561 ms 180888 KB Output is correct
51 Correct 2069 ms 143864 KB Output is correct
52 Correct 2925 ms 170728 KB Output is correct
53 Correct 3444 ms 178064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 440 ms 37900 KB Output is correct
2 Correct 422 ms 41100 KB Output is correct
3 Correct 287 ms 37464 KB Output is correct
4 Correct 335 ms 36476 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 397 ms 41444 KB Output is correct
7 Correct 65 ms 12364 KB Output is correct
8 Correct 231 ms 27356 KB Output is correct
9 Correct 287 ms 37468 KB Output is correct
10 Correct 464 ms 39848 KB Output is correct
11 Correct 283 ms 35116 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 312 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 312 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 3 ms 788 KB Output is correct
30 Correct 5 ms 788 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 4 ms 788 KB Output is correct
33 Correct 4 ms 788 KB Output is correct
34 Correct 4 ms 776 KB Output is correct
35 Correct 273 ms 34024 KB Output is correct
36 Correct 348 ms 38784 KB Output is correct
37 Correct 288 ms 34160 KB Output is correct
38 Correct 331 ms 38976 KB Output is correct
39 Correct 353 ms 36620 KB Output is correct
40 Correct 317 ms 37092 KB Output is correct
41 Correct 600 ms 40804 KB Output is correct
42 Correct 69 ms 18612 KB Output is correct
43 Correct 216 ms 27364 KB Output is correct
44 Correct 375 ms 33572 KB Output is correct
45 Correct 528 ms 39584 KB Output is correct
46 Correct 557 ms 39560 KB Output is correct
47 Correct 542 ms 39164 KB Output is correct
48 Correct 305 ms 34752 KB Output is correct
49 Correct 348 ms 39576 KB Output is correct
50 Correct 473 ms 41372 KB Output is correct
51 Correct 488 ms 41096 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 580 ms 41096 KB Output is correct
54 Correct 383 ms 34196 KB Output is correct
55 Correct 545 ms 39900 KB Output is correct
56 Correct 577 ms 39304 KB Output is correct
57 Correct 302 ms 32544 KB Output is correct
58 Incorrect 352 ms 35608 KB Output isn't correct
59 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 440 ms 37900 KB Output is correct
2 Correct 422 ms 41100 KB Output is correct
3 Correct 287 ms 37464 KB Output is correct
4 Correct 335 ms 36476 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 397 ms 41444 KB Output is correct
7 Correct 65 ms 12364 KB Output is correct
8 Correct 231 ms 27356 KB Output is correct
9 Correct 287 ms 37468 KB Output is correct
10 Correct 464 ms 39848 KB Output is correct
11 Correct 283 ms 35116 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 312 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 312 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 3 ms 788 KB Output is correct
30 Correct 5 ms 788 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 4 ms 788 KB Output is correct
33 Correct 4 ms 788 KB Output is correct
34 Correct 4 ms 776 KB Output is correct
35 Correct 273 ms 34024 KB Output is correct
36 Correct 348 ms 38784 KB Output is correct
37 Correct 288 ms 34160 KB Output is correct
38 Correct 331 ms 38976 KB Output is correct
39 Correct 353 ms 36620 KB Output is correct
40 Correct 317 ms 37092 KB Output is correct
41 Correct 600 ms 40804 KB Output is correct
42 Correct 69 ms 18612 KB Output is correct
43 Correct 216 ms 27364 KB Output is correct
44 Correct 375 ms 33572 KB Output is correct
45 Correct 528 ms 39584 KB Output is correct
46 Correct 557 ms 39560 KB Output is correct
47 Correct 542 ms 39164 KB Output is correct
48 Correct 305 ms 34752 KB Output is correct
49 Correct 348 ms 39576 KB Output is correct
50 Correct 473 ms 41372 KB Output is correct
51 Correct 488 ms 41096 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 580 ms 41096 KB Output is correct
54 Correct 383 ms 34196 KB Output is correct
55 Correct 545 ms 39900 KB Output is correct
56 Correct 577 ms 39304 KB Output is correct
57 Correct 1687 ms 147168 KB Output is correct
58 Correct 1968 ms 170484 KB Output is correct
59 Correct 2602 ms 181704 KB Output is correct
60 Correct 2596 ms 181532 KB Output is correct
61 Correct 3561 ms 180888 KB Output is correct
62 Correct 2069 ms 143864 KB Output is correct
63 Correct 2925 ms 170728 KB Output is correct
64 Correct 3444 ms 178064 KB Output is correct
65 Correct 302 ms 32544 KB Output is correct
66 Incorrect 352 ms 35608 KB Output isn't correct
67 Halted 0 ms 0 KB -