Submission #365849

# Submission time Handle Problem Language Result Execution time Memory
365849 2021-02-12T12:53:58 Z vaaven Two Dishes (JOI19_dishes) C++14
5 / 100
443 ms 77676 KB
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>
#include <bitset>
#include <map>
#include <cassert>
#include <array>
#include <queue>
#include <cstring>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
#define int long  long

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;

const int inf = 1e9;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-14;


void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen("cbs.in", "r", stdin);
//    freopen("cbs.out", "w", stdout);
}

const int maxn = 1e6 + 228;

vpii kek[maxn];
int t[maxn*4];
int cnt[maxn*4];
int A[maxn], B[maxn], S[maxn], T[maxn], P[maxn], Q[maxn];

void push(int v, int l, int r){
    if(cnt[v] != 0){
        t[v] += cnt[v];
        if(l != r){
            cnt[v*2] += cnt[v];
            cnt[v*2+1] += cnt[v];
        }
        cnt[v] = 0;
    }
}

int get(int v, int l, int r, int ql, int qr){
    push(v, l, r);
    if(ql <= l && r <= qr){
        return t[v];
    } else if(l > qr || ql > r){
        return -1e18;
    } else{
        int mid = (l + r)/2;
        int ans = max(get(v*2, l, mid, ql, qr), get(v*2+1, mid+1, r, ql, qr));
        t[v] = max(t[v*2], t[v*2+1]);
        return ans;
    }
}

void add(int v, int l, int r, int ql, int qr, int k){
    push(v, l, r);
    if(ql <= l && r <= qr){
        cnt[v] += k;
        push(v, l, r);
    } else if(l > qr || ql > r){
        return;
    } else{
        int mid = (l+r)/2;
        add(v*2, l, mid, ql, qr, k);
        add(v*2+1, mid+1, r, ql, qr, k);
        t[v] = max(t[v*2], t[v*2+1]);
    }
}

void upd(int v, int l, int r, int k, int k_new){
    push(v, l, r);
    if(l==r){
        t[v] = max(k_new, t[v]);
    } else{
        int mid = (l+r)/2;
        if(k <= mid){
            upd(v*2, l, mid, k, k_new);
        } else{
            upd(v*2+1, mid+1, r, k, k_new);
        }
        t[v] = max(t[v*2], t[v*2+1]);
    }
}

void solve(){
    int n, m;
    cin >> n >> m;
    for(int &i:t)
        i = -1e18;
//    cout << t[0] << endl;
    upd(1, 0, maxn, 0, 0);
    for(int i=1; i<=n; i++){
        cin >> A[i] >> S[i] >> P[i];
        A[i] += A[i-1];
    }
    for(int i=1; i<=m; i++){
        cin >> B[i] >> T[i] >> Q[i];
        B[i] += B[i-1];
    }
    int ans = 0;
    for(int i=1; i<=n; i++){
        if(A[i] > S[i]){
            continue;
        }
        if(A[i] + B[m] <= S[i]){
            ans += P[i];
            continue;
        }
        int l = 0, r = m;
        int j = 0;
        while(l<r){
            int mid = (l+r+1)/2;
            if(B[mid] <= S[i]-A[i]){
                l = mid;
                j = mid;
            } else{
                r = mid-1;
            }
        }
        kek[i].pb(make_pair(j, P[i]));
    }
    for(int i=1; i<=m; i++){
        if(B[i] > T[i]){
            continue;
        }
        int l = 0, r = n;
        int j = 0;
        while(l<r){
            int mid = (l+r+1)/2;
            if(A[mid] <= T[i]-B[i]){
                l = mid;
                j = mid;
            } else{
                r = mid-1;
            }
        }
        ans += Q[i];
        if(j < n){
            kek[j+1].pb(make_pair(i-1, -Q[i]));
        }
    }
    for(int i=0; i<=n; i++){
        sort(all(kek[i]));
        while(!kek[i].empty()){
            while(kek[i].size() > 1){
                if(kek[i][kek[i].size()-1].first == kek[i][kek[i].size()-2].first){
                    kek[i][kek[i].size()-2].second += kek[i][kek[i].size()-1].second;
                    kek[i].pop_back();
                } else{
                    break;
                }
            }
            int lol = get(1, 0, maxn, 0, kek[i].back().first);
            upd(1, 0, maxn, kek[i].back().first+1, lol);
            add(1, 0, maxn, 0, kek[i].back().first, kek[i].back().second);
            kek[i].pop_back();
        }
    }
    ans += t[1];
    cout << ans << endl;
}

/*
4 3
2 1 1
3 8 1
2 13 1
1 13 1
3 6 1
2 11 1
2 15 1
======
5 7
16 73 16
17 73 10
20 73 1
14 73 16
18 73 10
3 73 2
10 73 7
16 73 19
12 73 4
15 73 15
20 73 14
15 73 8
=========
 9 11
 86 565 58
 41 469 -95
 73 679 28
 91 585 -78
 17 513 -63
 48 878 -66
 66 901 59
 72 983 -70
 68 1432 11
 42 386 -87
 36 895 57
 100 164 10
 96 812 -6
 23 961 -66
 54 193 51
 37 709 82
 62 148 -36
 28 853 22
 15 44 53
 77 660 -19
 */

signed main(){
     fast_io();
//  srand(time(NULL));
    cout << fixed << setprecision(10);
    int q = 1;
//    cin >> q;
    while(q--)
        solve();
}

# Verdict Execution time Memory Grader output
1 Correct 443 ms 77676 KB Output is correct
2 Correct 434 ms 76980 KB Output is correct
3 Correct 204 ms 64620 KB Output is correct
4 Correct 329 ms 72556 KB Output is correct
5 Correct 41 ms 55148 KB Output is correct
6 Correct 385 ms 74604 KB Output is correct
7 Correct 145 ms 59884 KB Output is correct
8 Correct 124 ms 59888 KB Output is correct
9 Correct 212 ms 64620 KB Output is correct
10 Correct 358 ms 77420 KB Output is correct
11 Correct 160 ms 64652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 55196 KB Output is correct
2 Correct 38 ms 55148 KB Output is correct
3 Correct 42 ms 55148 KB Output is correct
4 Correct 37 ms 55148 KB Output is correct
5 Correct 38 ms 55276 KB Output is correct
6 Correct 41 ms 55148 KB Output is correct
7 Correct 37 ms 55276 KB Output is correct
8 Correct 37 ms 55148 KB Output is correct
9 Correct 37 ms 55276 KB Output is correct
10 Incorrect 37 ms 55276 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 55196 KB Output is correct
2 Correct 38 ms 55148 KB Output is correct
3 Correct 42 ms 55148 KB Output is correct
4 Correct 37 ms 55148 KB Output is correct
5 Correct 38 ms 55276 KB Output is correct
6 Correct 41 ms 55148 KB Output is correct
7 Correct 37 ms 55276 KB Output is correct
8 Correct 37 ms 55148 KB Output is correct
9 Correct 37 ms 55276 KB Output is correct
10 Incorrect 37 ms 55276 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 55196 KB Output is correct
2 Correct 38 ms 55148 KB Output is correct
3 Correct 42 ms 55148 KB Output is correct
4 Correct 37 ms 55148 KB Output is correct
5 Correct 38 ms 55276 KB Output is correct
6 Correct 41 ms 55148 KB Output is correct
7 Correct 37 ms 55276 KB Output is correct
8 Correct 37 ms 55148 KB Output is correct
9 Correct 37 ms 55276 KB Output is correct
10 Incorrect 37 ms 55276 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 55196 KB Output is correct
2 Correct 38 ms 55148 KB Output is correct
3 Correct 42 ms 55148 KB Output is correct
4 Correct 37 ms 55148 KB Output is correct
5 Correct 38 ms 55276 KB Output is correct
6 Correct 41 ms 55148 KB Output is correct
7 Correct 37 ms 55276 KB Output is correct
8 Correct 37 ms 55148 KB Output is correct
9 Correct 37 ms 55276 KB Output is correct
10 Incorrect 37 ms 55276 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 55196 KB Output is correct
2 Correct 38 ms 55148 KB Output is correct
3 Correct 42 ms 55148 KB Output is correct
4 Correct 37 ms 55148 KB Output is correct
5 Correct 38 ms 55276 KB Output is correct
6 Correct 41 ms 55148 KB Output is correct
7 Correct 37 ms 55276 KB Output is correct
8 Correct 37 ms 55148 KB Output is correct
9 Correct 37 ms 55276 KB Output is correct
10 Incorrect 37 ms 55276 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 443 ms 77676 KB Output is correct
2 Correct 434 ms 76980 KB Output is correct
3 Correct 204 ms 64620 KB Output is correct
4 Correct 329 ms 72556 KB Output is correct
5 Correct 41 ms 55148 KB Output is correct
6 Correct 385 ms 74604 KB Output is correct
7 Correct 145 ms 59884 KB Output is correct
8 Correct 124 ms 59888 KB Output is correct
9 Correct 212 ms 64620 KB Output is correct
10 Correct 358 ms 77420 KB Output is correct
11 Correct 160 ms 64652 KB Output is correct
12 Correct 37 ms 55196 KB Output is correct
13 Correct 38 ms 55148 KB Output is correct
14 Correct 42 ms 55148 KB Output is correct
15 Correct 37 ms 55148 KB Output is correct
16 Correct 38 ms 55276 KB Output is correct
17 Correct 41 ms 55148 KB Output is correct
18 Correct 37 ms 55276 KB Output is correct
19 Correct 37 ms 55148 KB Output is correct
20 Correct 37 ms 55276 KB Output is correct
21 Incorrect 37 ms 55276 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 443 ms 77676 KB Output is correct
2 Correct 434 ms 76980 KB Output is correct
3 Correct 204 ms 64620 KB Output is correct
4 Correct 329 ms 72556 KB Output is correct
5 Correct 41 ms 55148 KB Output is correct
6 Correct 385 ms 74604 KB Output is correct
7 Correct 145 ms 59884 KB Output is correct
8 Correct 124 ms 59888 KB Output is correct
9 Correct 212 ms 64620 KB Output is correct
10 Correct 358 ms 77420 KB Output is correct
11 Correct 160 ms 64652 KB Output is correct
12 Correct 37 ms 55196 KB Output is correct
13 Correct 38 ms 55148 KB Output is correct
14 Correct 42 ms 55148 KB Output is correct
15 Correct 37 ms 55148 KB Output is correct
16 Correct 38 ms 55276 KB Output is correct
17 Correct 41 ms 55148 KB Output is correct
18 Correct 37 ms 55276 KB Output is correct
19 Correct 37 ms 55148 KB Output is correct
20 Correct 37 ms 55276 KB Output is correct
21 Incorrect 37 ms 55276 KB Output isn't correct
22 Halted 0 ms 0 KB -