QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#261733 | #1831. Bruteforce | wsyear | ML | 4ms | 74384kb | C++14 | 4.7kb | 2023-11-23 09:44:31 | 2023-11-23 09:44:31 |
Judging History
answer
#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using namespace std;
template <int P>
class mod_int {
using Z = mod_int;
private:
static int mo(int x) { return x < 0 ? x + P : x; }
public:
int x;
int val() const { return x; }
mod_int() : x(0) {}
template <class T>
mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
bool operator==(const Z &rhs) const { return x == rhs.x; }
bool operator!=(const Z &rhs) const { return x != rhs.x; }
Z operator-() const { return Z(x ? P - x : 0); }
Z pow(long long k) const {
Z res = 1, t = *this;
while (k) {
if (k & 1) res *= t;
if (k >>= 1) t *= t;
}
return res;
}
Z &operator++() {
x < P - 1 ? ++x : x = 0;
return *this;
}
Z &operator--() {
x ? --x : x = P - 1;
return *this;
}
Z operator++(int) {
Z ret = x;
x < P - 1 ? ++x : x = 0;
return ret;
}
Z operator--(int) {
Z ret = x;
x ? --x : x = P - 1;
return ret;
}
Z inv() const { return pow(P - 2); }
Z &operator+=(const Z &rhs) {
(x += rhs.x) >= P && (x -= P);
return *this;
}
Z &operator-=(const Z &rhs) {
(x -= rhs.x) < 0 && (x += P);
return *this;
}
Z &operator*=(const Z &rhs) {
x = 1ULL * x * rhs.x % P;
return *this;
}
Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o) \
friend T operator o(const Z &lhs, const Z &rhs) {\
Z res = lhs; \
return res o## = rhs; \
}
setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 998244353;
using Z = mod_int<P>;
const int maxn = 200010;
const int maxm = 6;
mt19937 rnd(114514);
Z C[maxm][maxm];
int n, m, k, w, rt, tot, a[maxn], ls[maxm], rs[maxm];
struct node {
int sz, val, key, cnt[maxm][maxm];
Z sum[maxm];
node() {
sz = 0;
memset(cnt, 0, sizeof(cnt));
rep (i, 0, k) sum[i] = 0;
}
node(int x) {
sz = 1, val = x, key = rnd();
memset(cnt, 0, sizeof(cnt));
rep (i, 0, w - 1) {
int tmp = x;
rep (j, 1, k) tmp = 1ll * tmp * (i + 1) % w;
cnt[i][tmp] = 1;
}
rep (i, 0, k) sum[i] = x;
}
Z calc() {
Z sum = 0;
rep (i, 0, w) sum += Z(i) * cnt[0][i];
return sum;
}
friend node operator+(node x, node y) {
node res = node();
res.sz = x.sz + y.sz;
rep (i, 0, w - 1) {
rep (j, 0, w - 1) res.cnt[i][j] += x.cnt[i][j];
rep (j, 0, w - 1) res.cnt[i][j] += y.cnt[(i + x.sz) % w][j];
}
rep (i, 0, k) res.sum[i] = x.sum[i];
rep (i, 0, k) rep (j, 0, i) res.sum[i] += y.sum[j] * C[i][j] * Z(x.sz).pow(i - j);
return res;
}
friend bool operator<(node x, node y) {
return x.val < y.val;
}
} t[maxn], val[maxn];
int nnd(int x) {
t[++tot] = node(x), val[tot] = t[tot];
return tot;
}
void pushup(int x) {
t[x] = val[x];
if (ls[x]) t[x] = t[ls[x]] + t[x];
if (rs[x]) t[x] = t[x] + t[rs[x]];
}
void split(int x, int v, int &p, int &q) {
if (!x) return p = q = 0, void();
if (val[x].val <= v) p = x, split(rs[x], v, rs[p], q);
else q = x, split(ls[x], v, p, ls[q]);
pushup(x);
}
int merge(int p, int q) {
if (!p || !q) return p ^ q;
if (val[p].key < val[q].key) {
rs[p] = merge(rs[p], q);
return pushup(p), p;
} else {
ls[q] = merge(p, ls[q]);
return pushup(q), q;
}
}
void ins(int x) {
int p, q;
split(rt, x, p, q);
int w = nnd(x);
rt = merge(merge(p, w), q);
}
void del(int x) {
int p, w, q;
split(rt, x - 1, p, q), split(q, x, w, q);
w = merge(ls[w], rs[w]);
rt = merge(merge(p, w), q);
}
int main() {
C[0][0] = 1;
rep (i, 1, 5) {
C[i][0] = 1;
rep (j, 1, i) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
}
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> k >> w;
rep (i, 1, n) cin >> a[i], ins(a[i]);
cin >> m;
while (m--) {
int x, v;
cin >> x >> v, del(a[x]), ins(a[x] = v);
cout << ((t[rt].sum[k] - t[rt].calc()) / w).val() << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 74384kb
input:
3 1 1 2 2 8 2 2 5 3 6
output:
36 30
result:
ok 2 number(s): "36 30"
Test #2:
score: 0
Accepted
time: 4ms
memory: 73904kb
input:
4 2 2 1 3 3 7 4 1 1 2 4 3 8 4 8
output:
75 80 103 108
result:
ok 4 number(s): "75 80 103 108"
Test #3:
score: -100
Memory Limit Exceeded
input:
10 1 1 16251 28898 58179 69362 48663 81443 34949 87167 16552 58931 10 6 89124 8 27159 4 7332 1 15852 9 67405 7 19413 10 97472 7 31114 6 31847 5 43794