#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
using int64 = long long;
using ld = long double;
constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
template<typename T>
class FenwickTree {
size_t n;
vector<T> bit;
public:
explicit FenwickTree(const size_t N) : n(N), bit(N, T{}) {}
explicit FenwickTree(const vector<T> &a) : FenwickTree(a.size()) {
n = a.size();
for (int i = 0; i < n; i++) {
add(i, a[i]);
}
}
[[nodiscard]] T sum(int r) const {
T ans{};
for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
ans += bit[i];
}
return ans;
}
[[nodiscard]] T sum(const int l, const int r) const {
return sum(r) - sum(l - 1);
}
void add(const int i, const T delta) {
for (int j = i; j < n; j |= j + 1) {
bit[j] += delta;
}
}
};
vector<int> subtask1(const vector<int> &C, const vector<int> &L, const vector<int> &R, const vector<int> &V) {
const int n = C.size();
const int q = L.size();
vector<int> box(n);
for (int query = 0; query < q; query++) {
for (int i = L[query]; i <= R[query]; i++) {
box[i] += V[query];
box[i] = max(box[i], 0);
box[i] = min(box[i], C[i]);
}
}
return box;
}
vector<int> subtask2(const vector<int> &C, const vector<int> &L, const vector<int> &R, const vector<int> &V) {
const int n = C.size();
const int q = L.size();
FenwickTree<int64> bit(n + 2);
for (int i = 0; i < q; i++) {
const int l = L[i] + 1, r = R[i] + 1, v = V[i];
bit.add(l, v);
bit.add(r + 1, -v);
}
vector<int> ans(n);
for (int i = 1; i <= n; i++) {
ans[i - 1] = (int) min(1LL * C[i - 1], bit.sum(i));
}
return ans;
}
vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V) {
const int n = C.size();
const int q = L.size();
if (max(n, q) <= 2000) return subtask1(C, L, R, V);
return subtask2(C, L, R, V);
}
Compilation message
candies.cpp: In instantiation of 'void FenwickTree<T>::add(int, T) [with T = long long int]':
candies.cpp:71:17: required from here
candies.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int j = i; j < n; j |= j + 1) {
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
13768 KB |
Output is correct |
2 |
Correct |
105 ms |
12880 KB |
Output is correct |
3 |
Correct |
110 ms |
12716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
60 ms |
7964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Incorrect |
49 ms |
7624 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
112 ms |
13768 KB |
Output is correct |
7 |
Correct |
105 ms |
12880 KB |
Output is correct |
8 |
Correct |
110 ms |
12716 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Incorrect |
60 ms |
7964 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |