This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "candies.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
typedef long long ll;
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx")
const int inf = 1e9+1;
const int N = 200'010;
const int S = 2048;
int a[N], c[N];
void add(int l, int r, int x)
{
Loop (i,l,r)
a[i] = (a[i] + x > c[i]? c[i]: a[i] + x);
}
void sub(int l, int r, int x)
{
Loop (i,l,r)
a[i] = (a[i] - x < 0? 0: a[i] - x);
}
void addsub(int l, int r, int x, int y)
{
Loop (i,l,r) {
a[i] = (a[i] + x > c[i]? c[i]: a[i] + x);
a[i] = (a[i] - y < 0? 0: a[i] - y);
}
}
void subadd(int l, int r, int x, int y)
{
Loop (i,l,r) {
a[i] = (a[i] - x < 0? 0: a[i] - x);
a[i] = (a[i] + y > c[i]? c[i]: a[i] + y);
}
}
void up(int l, int r, int x)
{
if (x > 0)
add(l, r, x);
else
sub(l, r, -x);
}
void upup(int l, int r, int x, int y)
{
if (x > 0 && y > 0)
add(l, r, min(inf, x+y));
if (x <= 0 && y <= 0)
sub(l, r, min(inf, -(x+y)));
if (x > 0 && y <= 0)
addsub(l, r, x, -y);
if (x <= 0 && y > 0)
subadd(l, r, -x, y);
}
std::vector<int> distribute_candies(std::vector<int> _c, std::vector<int> ql,
std::vector<int> qr, std::vector<int> qv) {
int n = _c.size();
int q = ql.size();
for (int &x : qr)
++x;
if (q%2) {
ql.push_back(0);
qr.push_back(1);
qv.push_back(0);
++q;
}
Loop (i,0,n)
c[i] = _c[i];
for (int L = 0; L < n; L += S) {
int R = min<int>(L+S, n);
for (int i = 0; i < q; i += 2) {
int l0 = ql[i], l1 = ql[i+1];
int r0 = qr[i], r1 = qr[i+1];
int v0 = qv[i], v1 = qv[i+1];
if (l0 <= L && R <= r0 && l1 <= L && R <= r1) {
upup(L, R, v0, v1);
} else {
up(max(l0, L), min(r0, R), v0);
up(max(l1, L), min(r1, R), v1);
}
}
}
return vector<int>(a, a+n);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |