#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
using ull = unsigned long long;
struct stree {
#define lc v*2
#define rc v*2+1
vector <ull> tree;
stree() {}
stree (int s) {
tree.resize(4*s);
}
void pushdown(int v) {
// since we're doing point query, we don't need to maintain
// a separate pd variable
if (tree[v] == 0)
return;
tree[lc] += tree[v];
tree[rc] += tree[v];
tree[v] = 0;
}
void update(int v, int tl, int tr, int l, int r, int val) {
if (tl > r || tr < l)
return;
if (tl >= l && tr <= r) {
tree[v] += val;
return;
}
pushdown(v);
int tm = (tl + tr) / 2;
update(lc, tl, tm, l, r, val);
update(rc, tm+1, tr, l, r, val);
}
ull query(int v, int tl, int tr, int p) {
if (tl > p || tr < p)
return 0;
if (tl == tr)
return tree[v];
pushdown(v);
int tm = (tl + tr) / 2;
return max(query(lc, tl, tm, p), query(rc, tm+1, tr, p));
}
};
const int maxnmk = 3 * 1e5 + 1;
int n, m, k;
int owners[maxnmk], req[maxnmk], ans[maxnmk];
pair <pair <int, int>, int> updates[maxnmk];
queue <pair <int, int>> q;
vector <vector <int>> subsets, stations;
int prev_mid = -1;
stree tree;
void search(int lo, int hi) {
int mid = (lo + hi) / 2;
if (prev_mid < mid) {
for (int i = prev_mid+1; i <= mid; i++) {
int l = updates[i].first.first;
int r = updates[i].first.second;
int val = updates[i].second;
if (l <= r)
tree.update(1, 0, m, l, r, val);
else {
tree.update(1, 0, m, l, m-1, val);
tree.update(1, 0, m, 0, r, val);
}
}
} else {
for (int i = mid+1; i <= prev_mid; i++) {
int l = updates[i].first.first;
int r = updates[i].first.second;
int val = updates[i].second;
if (l <= r)
tree.update(1, 0, m, l, r, -val);
else {
tree.update(1, 0, m, l, m-1, -val);
tree.update(1, 0, m, 0, r, -val);
}
}
}
// check if any members have met their requirements
bool added_lo = false, added_hi = false;
for (auto i : subsets[mid]) {
ull tot = 0;
for (auto j : stations[i])
tot += tree.query(1, 0, m, j);
if (tot >= req[i]) {
if (ans[i] == -1)
ans[i] = mid;
else
ans[i] = min(ans[i], mid);
if (lo != mid) {
added_lo = true;
subsets[(lo + mid) / 2].push_back(i);
}
} else {
if (mid != hi) {
added_hi = true;
subsets[(mid+1 + hi) / 2].push_back(i);
}
}
}
if (added_lo)
q.push({lo, mid});
if (added_hi)
q.push({mid+1, hi});
prev_mid = mid;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
fill(ans, ans + n, -1);
stations.resize(n);
for (int i = 0; i < m; i++) {
cin >> owners[i];
owners[i]--;
stations[owners[i]].push_back(i);
}
for (int i = 0; i < n; i++)
cin >> req[i];
cin >> k;
for (int i = 0; i < k; i++) {
int l, r;
ull val;
cin >> l >> r >> val;
l--; r--;
updates[i].first.first = l;
updates[i].first.second = r;
updates[i].second = val;
}
q.push({0, k-1});
subsets.resize(k);
for (int i = 0; i < n; i++)
subsets[(k-1)/2].push_back(i);
tree = stree(m+1);
while (!q.empty()) {
int l = q.front().first;
int r = q.front().second;
q.pop();
search(l, r);
}
for (int i = 0; i < n; i++) {
if (ans[i] == -1)
cout << "NIE\n";
else
cout << ans[i]+1 << '\n';
}
}
Compilation message
met.cpp: In function 'void search(int, int)':
met.cpp:90:17: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
90 | if (tot >= req[i]) {
| ~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4444 KB |
Output is correct |
2 |
Correct |
5 ms |
4444 KB |
Output is correct |
3 |
Correct |
5 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4444 KB |
Output is correct |
2 |
Correct |
5 ms |
4664 KB |
Output is correct |
3 |
Correct |
7 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
486 ms |
10424 KB |
Output is correct |
2 |
Correct |
607 ms |
12628 KB |
Output is correct |
3 |
Correct |
581 ms |
12284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
538 ms |
11348 KB |
Output is correct |
2 |
Correct |
554 ms |
11356 KB |
Output is correct |
3 |
Correct |
643 ms |
12628 KB |
Output is correct |
4 |
Correct |
99 ms |
8144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
10836 KB |
Output is correct |
2 |
Correct |
351 ms |
13256 KB |
Output is correct |
3 |
Correct |
104 ms |
8560 KB |
Output is correct |
4 |
Correct |
527 ms |
12544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
9816 KB |
Output is correct |
2 |
Correct |
512 ms |
11344 KB |
Output is correct |
3 |
Correct |
358 ms |
10064 KB |
Output is correct |
4 |
Correct |
595 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1710 ms |
38704 KB |
Output is correct |
2 |
Correct |
666 ms |
25256 KB |
Output is correct |
3 |
Correct |
853 ms |
12628 KB |
Output is correct |
4 |
Execution timed out |
6085 ms |
59204 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1803 ms |
37392 KB |
Output is correct |
2 |
Correct |
1221 ms |
25452 KB |
Output is correct |
3 |
Correct |
69 ms |
11356 KB |
Output is correct |
4 |
Execution timed out |
6036 ms |
65536 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |