cc-strquer
#include <bits/stdc++.h>
template <typename T>
T imin(T x, T y) {
return x > y ? y : x;
}
template <typename T>
T imax(T x, T y) {
return x > y ? x : y;
}
template <typename T, typename... Args>
T imin(T x, T y, Args... args) {
return imin(x, imin(y, args...));
}
template <typename T, typename... Args>
T imax(T x, T y, Args... args) {
return imax(x, imax(y, args...));
}
template <typename T>
void Min(T &x, T y) {
x = imin(x, y);
}
template <typename T, typename... Args>
void Min(T &x, T y, Args... args) {
x = imin(x, imin(y, args...));
}
template <typename T>
void Max(T &x, T y) {
x = imax(x, y);
}
template <typename T, typename... Args>
void Max(T &x, T y, Args... args) {
x = imax(x, imax(y, args...));
}
const long long inf = 1e18;
namespace 😅😅 {
struct node {
int ls, rs, num;
long long l, r, mx, mn;
long long dp[2][2];
long long *const operator[](const long long i) { return dp[i]; }
} 😅[2 * 200000 * 60];
int tot;
int fuck😅(long long l, long long r) {
int shit = ++tot;
😅[shit].ls = 😅[shit].rs = 0;
😅[shit].l = l, 😅[shit].r = r;
😅[shit].num = 0;
return shit;
}
void Merge(node &$😅, node x, node y) {
node ap;
bool flag = 0;
if (!x.num) {
ap = y;
flag = 1;
}
if (!y.num) {
ap = x;
flag = 1;
}
if (flag) {
$😅.mn = ap.mn;
$😅.mx = ap.mx;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) $😅[i][j] = ap[i][j];
}
return;
}
$😅.mn = x.mn;
$😅.mx = y.mx;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
$😅[i][j] = -inf;
for (int a = 0; a < 2; ++a) {
for (int b = 0; b < 2; ++b)
Max($😅[i][j], x[i][a] + y[b][j] + a * b * (y.mn - x.mx));
}
}
}
}
void add(int p, long long x, int v) {
auto &l = 😅[p].l, &r = 😅[p].r;
if (!p) p = ++tot;
😅[p].num += v;
if (l == r) {
😅[p].mx = 😅[p].mn = x;
😅[p][0][0] = 😅[p][1][1] = -inf;
😅[p][0][1] = 😅[p][1][0] = 0;
return;
}
long long mid = (l + r) >> 1;
if (mid >= x) {
if (!😅[p].ls) 😅[p].ls = fuck😅(l, mid);
add(😅[p].ls, x, v);
} else {
if (!😅[p].rs) 😅[p].rs = fuck😅(mid + 1, r);
add(😅[p].rs, x, v);
}
Merge(😅[p], 😅[😅[p].ls], 😅[😅[p].rs]);
}
} // namespace 😅😅
#include <cstdio>
#include <cstring>
#include <iostream>
#define d isdigit(c)
#define g c = t.get();
#define L return
#define K break
#define A(c, a, b) \
if (c) a else b;
#define I(c, a) \
if (c) a;
#define Y goto E
struct MI {
private:
char bb[4096];
FILE *f;
char *bs, *be;
char e;
bool o, l;
public:
MI() : f(stdin) {}
MI(FILE *f) : f(f), bs(0), be(0) {}
#ifdef __linux__
MI(const char *s) : f(fmemopen(const_cast<char *>(s), strlen(s), "r")) {}
#endif
inline operator bool() { L !l; }
inline char get() {
if (o) {
o = 0;
L e;
}
#ifdef MIVIK
char c = fgetc(f);
I(c == -1, l = 1) L c;
#else
I(bs == be, be = (bs = bb) + fread(bb, 1, sizeof(bb), f))
I(bs == be, {
l = 1;
L - 1;
}) L *bs++;
#endif
}
inline void unget(char c) {
o = 1;
e = c;
}
template <typename T>
inline T read() {
T r;
*this > r;
L r;
}
template <typename T>
inline MI &operator>(T &);
};
template <typename T>
struct Q {
const static bool U = T(-1) >= T(0);
inline void operator()(MI &t, T &r) const {
r = 0;
char c;
bool y = 0;
A(U, for (;;){g I(c == -1, Y) I(d, K)},
for (;;){g I(c == -1, Y) A(c == '-',
{g I(d,
{
y = 1;
K;
})},
I(d, K))})
for (;;) {
I(c == -1, Y) A(d, r = r * 10 + (c ^ 48);, K) g
}
t.unget(c);
E:;
I(y, r = -r)
}
};
template <>
struct Q<char> {
inline void operator()(MI &t, char &r) {
int c;
for (;;) {
g I(c == -1, {
r = -1;
L;
}) I(!isspace(c), {
r = c;
L;
})
}
}
};
template <typename T>
inline MI &MI::operator>(T &t) {
Q<T>()(*this, t);
L *this;
}
#undef d
#undef g
#undef L
#undef K
#undef A
#undef I
#undef Y
template <typename T>
inline std::ostream &operator<(std::ostream &out, const T &t) {
return out << t;
}
#define endl ('\n')
#define P(x) cout < #x " = " < (x) < endl
#define R (cin.read<int>())
using std::cout;
MI cin;
signed main() {
int T;
for (cin > T; T; --T) {
int n, q;
cin > n > q;
😅😅::fuck😅(0, inf);
for (long long x; n; --n) {
cin > x;
😅😅::add(1, x, 1);
}
for (int t; q; --q) {
long long x;
cin > t > x;
if (t == 0)
😅😅::add(1, x, 1);
else
😅😅::add(1, x, -1);
cout < 😅😅::😅[1].mx - 😅😅::😅[1].mn - imax(😅😅::😅[1][1][1], 0LL) < '\n';
}
😅😅::tot = 0;
}
return 0;
}
acl-k
#include <bits/stdc++.h>
#include <atcoder/lazysegtree>
#include <atcoder/modint>
using mint = atcoder::modint;
namespace OP {
struct S {
mint sum;
int sz;
};
struct F {
mint mul, add;
};
S e() { return S{0, 0}; }
S op(S x, S y) { return S{x.sum + y.sum, x.sz + y.sz}; }
S mapping(F t, S x) { return S{x.sum * t.mul + x.sz * t.add, x.sz}; }
F id() { return F{1, 0}; }
F composition(F t, F x) { return F{x.mul * t.mul, x.add * t.mul + t.add}; }
}; // namespace OP
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr), std::cout.tie(nullptr);
int n, q, P;
std::cin >> n >> q >> P;
mint::set_mod(P);
std::vector<OP::S> a(n);
for (auto &x : a) {
int v;
std::cin >> v;
x = OP::S{v, 1};
}
atcoder::lazy_segtree<OP::S, OP::op, OP::e, OP::F, OP::mapping,
OP::composition, OP::id>
seg(a);
for (int t, l, r, x; q; --q) {
std::cin >> t >> l >> r;
l--;
if (t == 1) {
std::cin >> x;
seg.apply(l, r, OP::F{x, 0});
} else if (t == 2) {
std::cin >> x;
seg.apply(l, r, OP::F{1, x});
} else
std::cout << seg.prod(l, r).sum.val() << '\n';
}
return 0;
}
cf868f
#include <bits/stdc++.h>
template <class T> inline void chmax(T& a, const T b) { a = a > b ? a : b; }
template <class T> inline void chmin(T& a, const T b) { a = a < b ? a : b; }
inline long long rd() {
long long x = 0; bool f = 0; char ch = getchar();
while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + (ch & 15), ch = getchar();
return f ? -x : x;
}
template <class T>
constexpr T kInf = std::numeric_limits<T>::max();
int n, k, a[100100]; long long dp[100100][30];
namespace sm {
long long Res = 0; int app[100100], L = 1, R;
inline long long res() { return Res; }
inline long long cal(int x) { return 1ll * x * (x - 1) / 2; }
void prog(int l, int r) {
auto upd = [&](int p, int d) -> void {
Res -= cal(app[a[p]]);
app[a[p]] += d;
Res += cal(app[a[p]]);
};
while (L > l) upd(--L, 1);
while (R < r) upd(++R, 1);
while (L < l) upd(L++, -1);
while (R > r) upd(R--, -1);
}
} // namespace Sweepline Mo
void Rawgrass(int l, int r, int lg, int rg, int K) {
if (l > r) return;
int mid = (l + r) >> 1, pos = 0, rrg = std::min(rg, mid - 1);
dp[mid][K] = kInf<long long>;
for (int t = lg; t <= rrg; ++t) {
sm::prog(t + 1, mid);
if (dp[t][K - 1] != kInf<long long> && dp[mid][K] > dp[t][K - 1] + sm::res())
dp[mid][K] = dp[t][K - 1] + sm::res(), pos = t;
}
Rawgrass(l, mid - 1, lg, pos, K);
Rawgrass(mid + 1, r, pos, rg, K);
}
signed main() {
n = rd(), k = rd();
for (int i = 1; i <= n; ++i) a[i] = rd();
for (int i = 1; i <= n; ++i) sm::prog(1, i), dp[i][1] = sm::res();
for (int i = 2; i <= k; ++i) Rawgrass(1, n, 1, n, i);
printf("%lld\n", dp[n][k]);
return 0;
}
next