Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define F first
  5. #define S second
  6. #define ll long long
  7. #define int ll
  8. #define ld long double
  9. #define endl '\n'
  10. #define TIME 1.0*clock()/CLOCKS_PER_SEC
  11.  
  12. using namespace std;
  13.  
  14. mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
  15.  
  16. const int M = 1e9 + 7;
  17. const int FFTM = 998244353;
  18. const int N = 2e6 + 7;
  19.  
  20. bool fl = 0;
  21.  
  22. struct node{
  23. ll res;
  24. ll pref;
  25. ll suff;
  26. ll y;
  27. ll L;
  28. ll R;
  29.  
  30. node(){y = -1;}
  31.  
  32. node operator+(node x){
  33. if (y == -1) return x;
  34. if (x.y == -1) return *this;
  35. node ret;
  36. ret.res = min(res, x.res);
  37. ret.res = min(ret.res, suff + x.pref);
  38. ret.res = min(ret.res, suff + x.pref);
  39. ret.L = L;
  40. ret.R = x.R;
  41. ret.y = y + x.y;
  42. ret.res = min(ret.res, ret.R - ret.L - ret.y);
  43. ret.res = min(ret.res, R + suff);
  44. ret.res = min(ret.res, x.pref - x.L);
  45. ret.pref = min(pref, x.pref - y);
  46. ret.suff = min(suff - x.y, x.suff);
  47. // if (fl) cerr << "mem " << ret.res << ' ' << suff << ' ' << x.pref << endl;
  48. return ret;
  49. }
  50. };
  51.  
  52.  
  53. struct segtree{
  54. int tn = 1;
  55. vector<node> t;
  56.  
  57. void resize(int n, vector<pair<ll,ll> > p){
  58. while (tn <= n) tn <<= 1;
  59. t.resize(tn<<1|1);
  60. 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;
  61. for (int i = tn - 1; i > 0; i--) t[i] = t[i<<1] + t[i<<1|1];
  62. }
  63.  
  64. segtree(){}
  65.  
  66. void upd(int p){
  67. if (p == 4) fl = 1;
  68. // cerr << "in " << p << ' ';
  69. int p2 = p + tn;
  70. 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]);
  71. // cerr << t[p2].res << ' ' << t[p2].pref << ' ' << t[p2].suff << endl;
  72. if (p2 - tn == 4) fl = 0;
  73. }
  74.  
  75. int q(){
  76. return t[1].res;
  77. }
  78. };
  79.  
  80. int n, t, k[N];
  81. ll p[N];
  82. vector<pair<ll,ll> > P;
  83. vector<pair<pair<int,int>,pair<int,int> > > x;
  84. segtree T;
  85.  
  86. main(){
  87. ios_base::sync_with_stdio(0);
  88. cin.tie(0);
  89. cout.tie(0);
  90. //#ifdef LOCAL
  91. freopen("input.txt", "r", stdin);
  92. freopen("output.txt", "w", stdout);
  93. //#endif // LOCAL
  94. cin >> n >> t;x.resize(n);
  95. for (int i = 1; i <= t; i++) cin >> k[i], p[i] = k[i] + (i ? p[i - 1] : 0);
  96. 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;
  97. sort(x.rbegin(), x.rend());
  98. P.resize(n);
  99. for (int i = 0; i < n; i++) P[x[i].F.S] = {p[x[i].S.F - 1], p[x[i].S.S]};
  100. T.resize(n, P);
  101. ll res = 0;
  102. for (int i = 0; i < n; i++){
  103. // cerr << T.q() << endl;
  104. T.upd(x[i].F.S);
  105. // 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;
  106. // cerr << T.q() << endl;
  107. if (T.q() >= 0) res += x[i].F.F;
  108. // , cerr << x[i].F.F << ' ' << x[i].F.S << endl;
  109. else T.upd(x[i].F.S);
  110. }
  111. cout << res;
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement