#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
using LL = long long;
template <typename T>
struct Fenwick {
int n;
int bit_num;
vector<T> a;
Fenwick(int n_ = 0) {
init(n_);
}
void init(int n_) {
n = n_;
bit_num = __lg(n_) + 1;
a.assign((1 << bit_num) + 1, T{});
}
void add(int x, const T &v) {
for (int i = x; !(i >> bit_num); i += i & -i) {
a[i] = a[i] + v;
}
}
T sum(int x) {
T ans{};
for (int i = x; i > 0; i -= i & -i) {
ans = ans + a[i];
}
return ans;
}
T rangeSum(int l, int r) {
return sum(r) - sum(l);
}
int select(T k) {
int x = 0;
for (int i = bit_num - 1; i >= 0; i--) {
if (a[x + (1 << i)] < k) {
k -= a[x + (1 << i)];
x = x + (1 << i);
}
}
return x + 1;
}
};
void O_o() {
int n, m;
cin >> n >> m;
vector<vector<int>> p(n + 1);
for (int i = 0; i < m; i++) {
int pos;
cin >> pos;
p[pos].push_back(i + 1);
}
vector<int> need(n + 1);
for (int i = 1; i <= n; i++) {
cin >> need[i];
}
int Q;
cin >> Q;
vector<int> l(Q + 1), r(Q + 1), val(Q + 1);
for (int i = 1; i <= Q; i++) {
cin >> l[i] >> r[i] >> val[i];
}
queue<tuple<int, int, vector<int>>> q;
{
vector<int> id;
for (int i = 0; i < n; i++) {
id.push_back(i + 1);
}
q.emplace(1, Q + 1, id);
}
Fenwick<LL> f(m + 2);
vector<int> res(n + 1);
int ptr = 0;
while (q.size()) {
auto [L, R, id] = q.front();
q.pop();
int M = L + (R - L) / 2;
if (R == L) {
for (auto x : id) {
res[x] = M;
}
continue;
}
while (M < ptr) {
f.init(m + 1);
ptr = 0;
}
while (ptr < M) {
ptr++;
f.add(l[ptr], val[ptr]);
f.add(r[ptr] + 1, -val[ptr]);
if (r[ptr] < l[ptr]) {
f.add(1, val[ptr]);
}
}
vector<int> ok, neg;
for (auto x : id) {
LL now = need[x];
for(auto pos : p[x]) {
now -= f.sum(pos);
if (now <= 0) {
break;
}
}
if (now <= 0) {
ok.push_back(x);
} else {
neg.push_back(x);
}
}
if (ok.size()) {
q.emplace(L, M, ok);
}
if (neg.size()) {
q.emplace(M + 1, R, neg);
}
}
for (int i = 1; i <= n; i++) {
if (res[i] > Q) {
cout << "NIE\n";
} else {
cout << res[i] << '\n';
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while (t--) {
O_o();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
2128 KB |
Output is correct |
2 |
Correct |
71 ms |
3340 KB |
Output is correct |
3 |
Correct |
64 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
2692 KB |
Output is correct |
2 |
Correct |
65 ms |
2640 KB |
Output is correct |
3 |
Correct |
70 ms |
3452 KB |
Output is correct |
4 |
Correct |
20 ms |
2364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
2384 KB |
Output is correct |
2 |
Correct |
90 ms |
3976 KB |
Output is correct |
3 |
Correct |
15 ms |
1084 KB |
Output is correct |
4 |
Correct |
63 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
1900 KB |
Output is correct |
2 |
Correct |
70 ms |
2644 KB |
Output is correct |
3 |
Correct |
45 ms |
1884 KB |
Output is correct |
4 |
Correct |
76 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
580 ms |
16728 KB |
Output is correct |
2 |
Correct |
115 ms |
9320 KB |
Output is correct |
3 |
Correct |
105 ms |
5708 KB |
Output is correct |
4 |
Correct |
930 ms |
38268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
609 ms |
15692 KB |
Output is correct |
2 |
Correct |
422 ms |
9136 KB |
Output is correct |
3 |
Correct |
43 ms |
6228 KB |
Output is correct |
4 |
Correct |
1281 ms |
41680 KB |
Output is correct |