#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) {
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 |
344 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 |
39 ms |
2016 KB |
Output is correct |
2 |
Correct |
73 ms |
3348 KB |
Output is correct |
3 |
Correct |
64 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
2648 KB |
Output is correct |
2 |
Correct |
65 ms |
2652 KB |
Output is correct |
3 |
Correct |
72 ms |
3452 KB |
Output is correct |
4 |
Correct |
17 ms |
2360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
2392 KB |
Output is correct |
2 |
Correct |
91 ms |
3952 KB |
Output is correct |
3 |
Correct |
15 ms |
1112 KB |
Output is correct |
4 |
Correct |
62 ms |
3428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
1880 KB |
Output is correct |
2 |
Correct |
63 ms |
2688 KB |
Output is correct |
3 |
Correct |
45 ms |
1880 KB |
Output is correct |
4 |
Correct |
71 ms |
4220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
518 ms |
16728 KB |
Output is correct |
2 |
Incorrect |
838 ms |
9308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
627 ms |
15664 KB |
Output is correct |
2 |
Incorrect |
514 ms |
9176 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |