//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] = 0;
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);
}
void change(int pos){
if (get(1, 1, n, pos, pos) >= 0) return;
int x = -1;
if (get(1, 1, n, pos, n) < 0) x = n + 1;
else{
int l = pos, r = n;
while (l != r){
int mid = (l + r + 1) >> 1;
if (get(1, 1, n, pos, mid) >= 0) l = mid;
else r = mid-1;
}
x = l;
}
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);
}
int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
15992 KB |
Output is correct |
2 |
Correct |
531 ms |
15440 KB |
Output is correct |
3 |
Correct |
224 ms |
16684 KB |
Output is correct |
4 |
Correct |
427 ms |
20728 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
728 ms |
19708 KB |
Output is correct |
7 |
Correct |
97 ms |
10364 KB |
Output is correct |
8 |
Correct |
98 ms |
12044 KB |
Output is correct |
9 |
Correct |
202 ms |
16616 KB |
Output is correct |
10 |
Correct |
777 ms |
21452 KB |
Output is correct |
11 |
Correct |
154 ms |
16760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
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 |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
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 |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
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 |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
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 |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
15992 KB |
Output is correct |
2 |
Correct |
531 ms |
15440 KB |
Output is correct |
3 |
Correct |
224 ms |
16684 KB |
Output is correct |
4 |
Correct |
427 ms |
20728 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
728 ms |
19708 KB |
Output is correct |
7 |
Correct |
97 ms |
10364 KB |
Output is correct |
8 |
Correct |
98 ms |
12044 KB |
Output is correct |
9 |
Correct |
202 ms |
16616 KB |
Output is correct |
10 |
Correct |
777 ms |
21452 KB |
Output is correct |
11 |
Correct |
154 ms |
16760 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
15992 KB |
Output is correct |
2 |
Correct |
531 ms |
15440 KB |
Output is correct |
3 |
Correct |
224 ms |
16684 KB |
Output is correct |
4 |
Correct |
427 ms |
20728 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
728 ms |
19708 KB |
Output is correct |
7 |
Correct |
97 ms |
10364 KB |
Output is correct |
8 |
Correct |
98 ms |
12044 KB |
Output is correct |
9 |
Correct |
202 ms |
16616 KB |
Output is correct |
10 |
Correct |
777 ms |
21452 KB |
Output is correct |
11 |
Correct |
154 ms |
16760 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |