Accoders 7.14 NOI模拟
garlic
单调栈瞎搞搞即可。
// ubsan: undefined
// accoder
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
typedef long long ll;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
const int kN = 2e6 + 5;
int n, x, a[kN], lm[kN][2], rm[kN][2], d[22][kN];
int st[kN], top = 0;
inline int qmax(int l, int r) {
int k = std::__lg(r - l + 1);
return std::max(d[k][l], d[k][r - (1 << k) + 1]);
}
inline int qmin(int l, int r) {
int k = std::__lg(r - l + 1);
return std::min(d[k][l], d[k][r - (1 << k) + 1]);
}
int main() {
freopen("garlic.in", "r", stdin);
freopen("garlic.out", "w", stdout);
scanf("%d%d", &n, &x);
For(i, 1, n) scanf("%d", &a[i]);
For(i, 1, n) {
lm[i][0] = lm[i][1] = i;
while (lm[i][0] > 1 && a[i] < a[lm[i][0] - 1]) lm[i][0] = lm[lm[i][0] - 1][0];
while (lm[i][1] > 1 && a[i] >= a[lm[i][1] - 1]) lm[i][1] = lm[lm[i][1] - 1][1];
}
rFor(i, n, 1) {
rm[i][0] = rm[i][1] = i;
while (rm[i][0] < n && a[i] <= a[rm[i][0] + 1]) rm[i][0] = rm[rm[i][0] + 1][0];
while (rm[i][1] < n && a[i] > a[rm[i][1] + 1]) rm[i][1] = rm[rm[i][1] + 1][1];
}
top = 0;
For(i, 1, n) d[0][i] = a[i];
For(i, 1, 21) {
for (int j = 1; j + (1 << i) - 1 <= n; ++j) {
d[i][j] = std::min(d[i - 1][j], d[i - 1][j + (1 << (i - 1))]);
}
}
ll ans = 0;
// min - max
For(i, 1, n) {
while (top && a[i] <= a[st[top]]) --top;
st[++top] = i;
int l = 1, r = top, pos = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[st[mid]] == x - a[i]) {
pos = mid;
break;
}
if (a[st[mid]] < x - a[i]) l = mid + 1;
else r = mid - 1;
}
if (pos && st[pos] >= lm[i][1]) {
l = i, r = rm[i][1];
while (l < r) {
int mid = (l + r + 1) >> 1;
if (qmin(i, mid) >= x - a[i]) l = mid;
else r = mid - 1;
}
ans += (ll)(r - i + 1) * (st[pos] - std::max(st[pos - 1], lm[i][1] - 1));
}
}
top = 0;
For(i, 1, n) d[0][i] = a[i];
For(i, 1, 21) {
for (int j = 1; j + (1 << i) - 1 <= n; ++j) {
d[i][j] = std::max(d[i - 1][j], d[i - 1][j + (1 << (i - 1))]);
}
}
// max - min
For(i, 1, n) {
int l = 1, r = top, pos = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[st[mid]] == x - a[i]) {
pos = mid;
break;
}
if (a[st[mid]] > x - a[i]) l = mid + 1;
else r = mid - 1;
}
if (pos && st[pos] >= lm[i][0]) {
l = i, r = rm[i][0];
while (l < r) {
int mid = (l + r + 1) >> 1;
if (qmax(i, mid) < x - a[i]) l = mid;
else r = mid - 1;
}
ans += (ll)(r - i + 1) * (st[pos] - std::max(st[pos - 1], lm[i][0] - 1));
}
while (top && a[i] >= a[st[top]]) --top;
st[++top] = i;
}
printf("%lld", ans);
return 0;
}
cow
经典错误:联想到某个很难维护的东西,但那个东西其实完全没有必要维护。
先预处理出
对于每次查询,再处理出
看似是
// ubsan: undefined
// accoder
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
const int kN = 65;
const int P = 1e9 + 7;
int n, m, q;
ull d[kN][kN][kN];
int pre[kN][kN], suf[kN][kN], sum[kN][kN];
char e[kN][kN];
inline int norm(int x) {
return x >= P ? x - P : x;
}
int main() {
freopen("cow.in", "r", stdin);
freopen("cow.out", "w", stdout);
double tim = clock();
scanf("%d%d%d", &n, &m, &q);
For(i, 1, n) scanf("%s", e[i] + 1);
For(i, 1, n) For(j, 1, n) e[i][j] -= '0';
// d(u, v, lim): from u to v, <= lim, except both ends
For(lim, 0, m) {
For(u, 1, n) For(v, 1, n) {
if (lim) d[u][v][lim] = norm(d[u][v][lim] + d[u][v][lim - 1]);
else if (e[u][v]) d[u][v][lim] = 1;
if (!lim) continue;
ull x = 0;
For(mid, 1, n) {
x += d[u][mid][lim - 1] * d[mid][v][lim - 1];
x %= P;
}
d[u][v][lim] = norm(d[u][v][lim] + x);
}
}
fprintf(stderr, "pre %lf\n", (clock() - tim) / CLOCKS_PER_SEC);
// pre(u, x), suf(u, x)
int visc = 0;
double td = 0;
while (q--) {
int bs, s, bt, t;
scanf("%d%d%d%d", &bs, &s, &bt, &t);
memset(pre, 0, sizeof(pre));
memset(suf, 0, sizeof(suf));
double tt = clock();
pre[s][bs] = 1;
memset(sum, 0, sizeof(sum));
For(x, bs, m) For(u, 1, n) {
sum[u][x] = norm(sum[u][x] + sum[u][x - 1]);
pre[u][x] = norm(pre[u][x] + sum[u][x]);
if (!pre[u][x]) continue;
For(v, 1, n) {
sum[v][x + 1] = (sum[v][x + 1] + (ll)pre[u][x] * d[u][v][x - 1]) % P;
}
}
suf[t][bt] = 1;
memset(sum, 0, sizeof(sum));
For(x, bt, m) For(u, 1, n) {
sum[u][x] = norm(sum[u][x] + sum[u][x - 1]);
suf[u][x] = norm(suf[u][x] + sum[u][x]);
For(v, 1, n) {
sum[v][x + 1] = (sum[v][x + 1] + (ll)suf[u][x] * d[v][u][x - 1]) % P;
}
}
// fprintf(stderr, "visc = %d\n", visc);
ull ans = 0;
For(x, 1, m) {
For(u, 1, n) {
ans += (ull)pre[u][x] * suf[u][x];
if ((u & 15) == 15) ans %= P;
}
ans %= P;
}
printf("%llu\n", ans);
td += clock() - tt;
}
fprintf(stderr, "%lf\n", td / CLOCKS_PER_SEC);
return 0;
}
frog
考虑先缩右边,再缩左边,即对于询问
考虑当
注意我们只求了改变量,还需单独求一次
卡常需要优化线段树写法。
// ubsan: undefined
// accoder
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
typedef long long ll;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
const int kN = 2e6 + 5;
int n, q, a[kN], pre[kN], nxt[kN], ans[kN], tmp[kN], rlim[kN], qc[kN], qlb[kN], qrb[kN];
std::vector<pii> qry[kN];
#define mid ((l + r) >> 1)
int minv[kN << 3], M;
#define ls (o << 1)
#define rs (o << 1 | 1)
void pushUp(int o) {
minv[o] = std::min(minv[ls], minv[rs]);
}
void modify(int k, int v) {
int o = M + k;
minv[o] = v;
for (o >>= 1; o; o >>= 1) pushUp(o);
}
int qmin(int ql, int qr) {
int res = 0x3f3f3f3f;
for (int s = ql + M - 1, t = qr + M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
// fprintf(stderr, "%d %d\n", s, t);
if (!(s & 1)) res = std::min(res, minv[s ^ 1]);
if (t & 1) res = std::min(res, minv[t ^ 1]);
}
return res;
}
struct DSU {
int f[kN];
void init() {
For(i, 1, n + 1) f[i] = i;
}
int getf(int x) {
return x == f[x] ? x : (f[x] = getf(f[x]));
}
void merge(int x, int y) {
f[getf(x)] = getf(y);
}
} prv, suc;
std::vector<pii> ins[kN], del[kN];
template <typename _T>
void read(_T &x) {
x = 0;
char s = getchar();
int f = 1;
while (!('0' <= s && s <= '9')) {
f = 1;
if (s == '-')
f = -1;
s = getchar();
}
while ('0' <= s && s <= '9') {
x = (x << 3) + (x << 1) + (s - '0'), s = getchar();
}
x *= f;
}
template <typename _T>
void write(_T x) {
if (x < 0)
putchar('-'), x = -x;
if (9 < x)
write(x / 10);
putchar(x % 10 + '0');
}
int main() {
freopen("frog.in", "r", stdin);
freopen("frog.out", "w", stdout);
read(n);
For(i, 1, n) read(a[i]);
M = 1;
while (M < n) M <<= 1;
For(i, 1, n) {
pre[i] = tmp[a[i]];
tmp[a[i]] = i;
}
memset(tmp, 0, sizeof(tmp));
rFor(i, n, 1) {
if (tmp[a[i]]) nxt[i] = tmp[a[i]];
else nxt[i] = n + 1;
tmp[a[i]] = i;
}
read(q);
For(i, 1, q) {
scanf("%d%d", &qlb[i], &qrb[i]);
ans[i] = 0x3f3f3f3f;
++qc[qlb[i]];
// fprintf(stderr, "%d (%d) ", rlim[i], ans[i]);
}
For(i, 1, n) qry[i].reserve(qc[i]);
For(i, 1, q) {
int l = qlb[i], r = qrb[i];
qry[l].emplace_back(r, i);
}
// fprintf(stderr, "%lf\n", (double)clock() / CLOCKS_PER_SEC);
// fprintf(stderr, "%d\n", visc);
memset(minv, 0x3f, sizeof(minv));
For(i, 1, n) if (pre[i] == 0) {
modify(i, n - i);
}
For(l, 1, n) {
if (l > 1 && nxt[l - 1] <= n) {
modify(nxt[l - 1], n - nxt[l - 1]);
}
for (auto x : qry[l]) {
rlim[x.se] = n - qmin(l, x.fi);
}
}
// fprintf(stderr, "%lf\n", (double)clock() / CLOCKS_PER_SEC);
memset(minv, 0x3f, sizeof(minv));
for (int l = n, cmin = n + 1; l; --l) {
if (nxt[l] == n + 1) cmin = l;
else modify(nxt[l], l);
for (auto x : qry[l]) {
int pos = cmin;
if (rlim[x.se] < n) pos = std::min(pos, qmin(rlim[x.se] + 1, n)); // ERR
ans[x.se] = std::min(ans[x.se], rlim[x.se] - pos + 1);
}
}
// fprintf(stderr, "%lf\n", (double)clock() / CLOCKS_PER_SEC);
prv.init(), suc.init();
For(i, 1, n) {
if (!pre[i]) continue;
prv.merge(pre[i], pre[i] - 1), suc.merge(pre[i], pre[i] + 1);
int lb = prv.getf(pre[i]) + 1, rb = pre[i];
ins[lb].emplace_back(i, std::max(0, i - suc.getf(pre[i]) + 1));
del[rb + 1].emplace_back(i, std::max(0, i - suc.getf(pre[i]) + 1));
}
// fprintf(stderr, "%lf\n", (double)clock() / CLOCKS_PER_SEC);
memset(minv, 0x3f, sizeof(minv));
For(l, 1, n) {
for (auto x : ins[l]) modify(x.fi, x.se);
for (auto x : del[l]) modify(x.fi, 0x3f3f3f3f);
for (auto x : qry[l]) ans[x.se] = std::min(ans[x.se], qmin(rlim[x.se], x.fi));
}
For(i, 1, q) printf("%d\n", ans[i]);
return 0;
}