This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://oj.uz/problem/view/JOI19_dishes
#include<bits/stdc++.h>
const int N = 1e6 + 5;
using namespace std;
int n, m, per[N], p[N], q[N];
long long S, sum[N << 2], maxsum[N << 2], a[N], b[N], s[N], t[N];
bool lazy[N << 2];
void dolazy(int i, int l, int r){
if (!lazy[i]) return;
if (l != r){
lazy[i << 1] = lazy[i << 1 | 1] = 1;
}
sum[i] = maxsum[i] = lazy[i] = 0;
}
void update(int i, int l, int r, int L, int R){
dolazy(i, l, r);
if (L > r || l > R || L > R) return;
if (L <= l && r <= R){
lazy[i] = 1;
dolazy(i, l, r);
return;
}
int mid = (l + r) >> 1;
update(i << 1, l, mid, L, R); update(i << 1 | 1, mid+1, r, L, R);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
maxsum[i] = max(maxsum[i << 1], sum[i << 1] + maxsum[i << 1 | 1]);
}
void update_pos(int i, int l, int r, int pos, long long val){
dolazy(i, l, r);
if (l > pos || pos > r) return;
if (l == r){
sum[i] += val;
maxsum[i] += val;
return;
}
int mid = (l + r) >> 1;
update_pos(i << 1, l, mid, pos, val); update_pos(i << 1 | 1, mid+1, r, pos, val);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
maxsum[i] = max(maxsum[i << 1], sum[i << 1] + maxsum[i << 1 | 1]);
}
long long get(int i, int l, int r, int L, int R){
dolazy(i, l, r);
if (L > r || l > R || L > R) return 0;
if (L <= l && r <= R) return sum[i];
int mid = (l + r) >> 1;
return get(i << 1, l, mid, L, R) + get(i << 1 | 1, mid+1, r, L, R);
}
int Find(int i, int l, int r, int pos, long long k){
dolazy(i, l, r);
if (r < pos || maxsum[i] < k) return n+1;
if (l == r) return l;
int mid = (l + r) >> 1;
int ans = Find(i << 1, l, mid, pos, k);
if (ans == n+1) ans = Find(i << 1 | 1, mid+1, r, pos, k - sum[i << 1]);
return ans;
}
void change(int pos){
int x = Find(1, 1, n, pos, get(1, 1, n, 1, pos-1));
if (x == pos) return;
if (x <= n) update_pos(1, 1, n, x, get(1, 1, n, pos, x-1));
update(1, 1, n, pos, x-1);
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i] >> s[i] >> p[i], a[i] += a[i-1], s[i] -= a[i], per[i] = i;
for (int i = 1; i <= m; i++) cin >> b[i] >> t[i] >> q[i], b[i] += b[i-1], t[i] -= b[i];
sort(per+1, per+1+n, [](int x, int y){return s[x] < s[y];});
int cnt = 1;
while (cnt <= n && s[per[cnt]] < 0) p[per[cnt++]] = 0;
for (int i = 1; i <= m; i++){
vector <int> mv;
while (cnt <= n && s[per[cnt]] < b[i]){
int id = per[cnt++];
update_pos(1, 1, n, id, p[id]); p[id] = 0;
mv.push_back(id);
}
if (t[i] >= 0) {
int l = 0, r = n;
while (l != r){
int mid = (l + r + 1) >> 1;
if (a[mid] <= t[i]) l = mid;
else r = mid-1;
}
S += q[i];
if (l != n){
update_pos(1, 1, n, l+1, -q[i]);
mv.push_back(l+1);
}
}
sort(mv.begin(), mv.end());
for (auto id : mv) change(id);
}
long long ans = S + sum[1];
for (int i = 1; i <= n; i++) ans += p[i];
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |