답안 #365857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
365857 2021-02-12T13:01:54 Z vaaven Two Dishes (JOI19_dishes) C++14
0 / 100
473 ms 81516 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];
ll lzy[4*maxn];
ll mx[4*maxn];
void push(int n) {
    lzy[2*n] += lzy[n];
    lzy[2*n+1] += lzy[n];
    mx[2*n] += lzy[n];
    mx[2*n+1] += lzy[n];
    lzy[n] = 0;
}
void add(int n, int tl, int tr, int l, int r, ll ad) {
    if (tr < l || r < tl) {
        return;
    }
    if (l <= tl && tr <= r) {
        lzy[n] += ad;
        mx[n] += ad;
    }
    else {
        push(n);
        int tm = (tl + tr)/2;
        add(2*n, tl, tm, l, r, ad);
        add(2*n+1, tm+1, tr, l, r, ad);
        mx[n] = max(mx[2*n], mx[2*n+1]);
    }
}
ll get(int n, int tl, int tr, int l, int r) {
   if (tr < l || r < tl) {
        return ll(-1e18);
    }
    if (l <= tl && tr <= r) {
        return mx[n];
    }
    else {
        push(n);
        int tm = (tl + tr)/2;
        return max(get(2*n, tl, tm, l, r), get(2*n+1, tm+1, tr, l, r));
    }
}
void upd(int n, int tl, int tr, int i, ll val) {
    if (tl == tr) {
        mx[n] = max(mx[n], val);
        lzy[n] = 0;
    }
    else {
        push(n);
        int tm = (tl + tr)/2;
        if (i <= tm) {
            upd(2*n, tl, tm, i, val);
        }
        else {
            upd(2*n+1, tm+1, tr, i, val);
        }
        mx[n] = max(mx[2*n], mx[2*n+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();
}

# 결과 실행 시간 메모리 Grader output
1 Incorrect 473 ms 81516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 55380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 55380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 55380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 55380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 55380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 473 ms 81516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 473 ms 81516 KB Output isn't correct
2 Halted 0 ms 0 KB -