//https://oj.uz/problem/view/JOI19_dishes
#include<bits/stdc++.h>
const int N = 1e6 + 5;
using namespace std;
int n, m;
long long S, sum[N << 2], a[N], b[N], s[N], t[N], p[N], q[N], per[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] = 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];
}
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;
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];
}
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){
if (r < pos || sum[i] < k) return -1;
if (l == r) return l;
int mid = (l + r) >> 1;
int ans = Find(1, 1, n, pos, k);
if (ans == -1) ans = Find(1, 1, n, pos, k - sum[i << 1]);
return ans;
}
void change(int pos){
if (get(1, 1, n, pos, pos) >= 0) return;
int x = Find(1, 1, n, pos, get(1, 1, n, 1, pos-1));
if (x == -1) x = n+1;
long long sum = get(1, 1, n, pos, x-1);
update(1, 1, n, pos, x-1);
if (x <= n) update_pos(1, 1, n, x, sum);
}
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];
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++){
while (cnt <= n && s[per[cnt]] < b[i]){
int id = per[cnt++];
update_pos(1, 1, n, id, p[id]); p[id] = 0;
change(id);
}
if (b[i] > t[i]) continue;
int l = 0, r = n;
while (l != r){
int mid = (l + r + 1) >> 1;
if (b[i] + 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]);
change(l+1);
}
}
long long ans = S + sum[1];
for (int i = 1; i <= n; i++) ans += p[i];
cout << ans;
}
Compilation message
dishes.cpp: In function 'int Find(int, int, int, int, long long int)':
dishes.cpp:55:9: warning: unused variable 'mid' [-Wunused-variable]
int mid = (l + r) >> 1;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1798 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Runtime error |
863 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Runtime error |
863 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Runtime error |
863 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Runtime error |
863 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Runtime error |
863 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1798 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1798 ms |
1049600 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |