Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define F first
- #define S second
- #define ll long long
- #define int ll
- #define ld long double
- #define endl '\n'
- #define TIME 1.0*clock()/CLOCKS_PER_SEC
- using namespace std;
- mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
- const int M = 1e9 + 7;
- const int FFTM = 998244353;
- const int N = 2e6 + 7;
- bool fl = 0;
- struct node{
- ll res;
- ll pref;
- ll suff;
- ll y;
- ll L;
- ll R;
- node(){y = -1;}
- node operator+(node x){
- if (y == -1) return x;
- if (x.y == -1) return *this;
- node ret;
- ret.res = min(res, x.res);
- ret.res = min(ret.res, suff + x.pref);
- ret.res = min(ret.res, suff + x.pref);
- ret.L = L;
- ret.R = x.R;
- ret.y = y + x.y;
- ret.res = min(ret.res, ret.R - ret.L - ret.y);
- ret.res = min(ret.res, R + suff);
- ret.res = min(ret.res, x.pref - x.L);
- ret.pref = min(pref, x.pref - y);
- ret.suff = min(suff - x.y, x.suff);
- // if (fl) cerr << "mem " << ret.res << ' ' << suff << ' ' << x.pref << endl;
- return ret;
- }
- };
- struct segtree{
- int tn = 1;
- vector<node> t;
- void resize(int n, vector<pair<ll,ll> > p){
- while (tn <= n) tn <<= 1;
- t.resize(tn<<1|1);
- for (int i = 0; i < n; i++) t[i + tn].res = 0, t[i + tn].y = 0, t[i + tn].pref = p[i].S, t[i + tn].suff = -p[i].F, t[i + tn].L = -t[i + tn].suff, t[i + tn].R = t[i + tn].pref;
- for (int i = tn - 1; i > 0; i--) t[i] = t[i<<1] + t[i<<1|1];
- }
- segtree(){}
- void upd(int p){
- if (p == 4) fl = 1;
- // cerr << "in " << p << ' ';
- int p2 = p + tn;
- for (t[p += tn].y ^= 1, t[p].res = t[p].R - t[p].L - t[p].y, t[p].suff -= (t[p].y == 1 ? 1 : -1), t[p].pref -= (t[p].y == 1 ? 1 : -1); p > 1; p >>= 1) t[p>>1] = ((p&1) ? t[p^1] + t[p] : t[p] + t[p^1]);
- // cerr << t[p2].res << ' ' << t[p2].pref << ' ' << t[p2].suff << endl;
- if (p2 - tn == 4) fl = 0;
- }
- int q(){
- return t[1].res;
- }
- };
- int n, t, k[N];
- ll p[N];
- vector<pair<ll,ll> > P;
- vector<pair<pair<int,int>,pair<int,int> > > x;
- segtree T;
- main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- //#ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- //#endif // LOCAL
- cin >> n >> t;x.resize(n);
- for (int i = 1; i <= t; i++) cin >> k[i], p[i] = k[i] + (i ? p[i - 1] : 0);
- for (int i = 0; i < n; i++) cin >> x[i].S.F >> x[i].S.S >> x[i].F.F, x[i].F.S = i;
- sort(x.rbegin(), x.rend());
- P.resize(n);
- for (int i = 0; i < n; i++) P[x[i].F.S] = {p[x[i].S.F - 1], p[x[i].S.S]};
- T.resize(n, P);
- ll res = 0;
- for (int i = 0; i < n; i++){
- // cerr << T.q() << endl;
- T.upd(x[i].F.S);
- // cerr << T.t[2].res << ' ' << T.t[2].pref << ' ' << T.t[2].suff << " " << T.t[4].res << ' ' << T.t[4].pref << ' ' << T.t[4].suff << " " << T.t[5].res << ' ' << T.t[5].pref << ' ' << T.t[5].suff << endl;
- // cerr << T.q() << endl;
- if (T.q() >= 0) res += x[i].F.F;
- // , cerr << x[i].F.F << ' ' << x[i].F.S << endl;
- else T.upd(x[i].F.S);
- }
- cout << res;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement