#include <bits/stdc++.h>
#include "candies.h"
#define pb push_back
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll oo = 1e9 + 7;
const ll N = 1e6;
const ll M = 21;
const ll M1 = 1e6;
int a[N];
struct tree
{
tree *L = NULL;
tree *R = NULL;
ll l, r;
ll sum = 0;
ll mdl;
tree(ll _l, ll _r) : l(_l), r(_r)
{
mdl = (l + r) >> 1;
if (l == r) return;
L = new tree(l, mdl);
R = new tree(mdl + 1, r);
}
void upd(ll _l, ll _r, ll vl)
{
if (_l > _r) return;
if (_l == l && _r == r)
{
sum += vl;
return;
}
L -> upd(_l, min(mdl, _r), vl);
R -> upd(max(mdl + 1, _l), _r, vl);
}
ll val(ll x)
{
if (l == r) return sum;
L -> sum += sum;
R -> sum += sum;
sum = 0;
if (mdl < x) return R -> val(x);
else return L -> val(x);
}
};
std::vector<int> distribute_candies (std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = c.size();
int q = l.size();
if (n <= 2000 && q <= 2000)
{
for (int i = 0; i < q; i++)
{
for (int j = l[i]; j <= r[i]; j++) a[j] += v[i];
for (int j = 0; j < n; j++)
{
if (a[j] < 0) a[j] = 0;
if (a[j] > c[j]) a[j] = c[j];
}
}
vector <int> ans(n);
for (int i = 0; i < n; i++) ans[i] = a[i];
return ans;
}
else
{
tree *root = new tree(0, n - 1);
for (int i = 0; i < q; i++) root -> upd(l[i], r[i], v[i]);\
vector <int> ans(n);
for (int i = 0; i < n; i++) ans[i] = min((ll)v[i], root -> val(i));
return ans;
}
}
//int main()
//{
// ios_base::sync_with_stdio(0);
// iostream::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
// int n;
// cin >> n;
// vector <int> c(n);
// for (int i = 0; i < n; i++) cin >> c[i];
// int q;
// cin >> q;
// vector <int> l(q), r(q), v(q);
// for (int i = 0; i < q; i++) cin >> l[i] >> r[i] >> v[i];
// vector <int> ans = distribute_candies(c, l, r, v);
// for (int i = 0; i < n; i++) cout << ans[i] << " ";
//}
/*
5 3
2 1 1 2 2 3
2 3 3 4 4 5
4 1 1 2 2 3 3 4 4 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
328 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
20 ms |
412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
378 ms |
37256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
107 ms |
8292 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
61 ms |
7892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
328 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
20 ms |
412 KB |
Output is correct |
6 |
Incorrect |
378 ms |
37256 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |