题解:CF1540D Inverse Inversions

· · 题解

对于一个区间,定义 $t(x)$ 表示 $x$ 经过区间之后会变成的数,这是一个分段函数,并且每一段的斜率都是 1。这个东西不好维护,转而维护 $f(i)$ 表示表示如果 $x\geq f(i)$ 那么 $x\rightarrow x+i$,考虑合并两个区间的函数 $f_l$ 和 $f_r$,考虑一共增加了 $k$,枚举分别加了 $i,j$,那么 $f(k)\geq f_l(i),x+i\geq f_r(j)$,于是 $f(k)=\min_{i+j=k}\{\max(f_l(i),f_r(j)-i)\}$。发现这样合并有很好的性质:如果 $k$ 是由 $(i,j)$ 转移而来的,那么 $k+1$ 就只能从 $(i+1,j)$ 或者 $(i,j+1)$ 转移过来!原因是 $f$ 单调不降,$(i,j)$ 能多贡献就多贡献,不会回退,就得到了 $O(len)$ 合并信息的做法。线段树维护这个信息就能做到 $O(n\log^2 n)$。但是这样维护单调修改的话就是 $O(n)$ 的,考虑分块平衡复杂度,分成 $B$ 块,每块开线段树,这样修改时间复杂度 $O(B)$,查询时间复杂度 $O(\frac{n}{B}\log n)$,平衡一下令 $B=\sqrt{n\log n}$ 就可以做到 $O(n\sqrt{n\log n})$ 了。 代码: ```cpp #include <bits/stdc++.h> #define INF 0x3f3f3f3f3f3f3f3f #define inf 0x3f3f3f3f #define eps 1e-8 #define fi first #define se second #define pn putchar(10) #define pp putchar(' ') #define pii pair<int, int> #define pdi pair<ll, ll> #define mem(aa, bb) memset(aa, bb, sizeof(aa)) #define fo(i, a, b) for (int i = (a); i <= (b); ++i) #define Fo(i, a, b) for (int i = (a); i >= (b); --i) #define pb push_back #define reg register #define eb emplace_back #define bct __builtin_popcount #define mk make_pair #define IT iterator #define all(x) x.begin(), x.end() #define lbd lower_bound #define ubd upper_bound #define lowbit(x) ((x) & (-(x))) #define gb(x, i) ((x >> i) & 1) #define IOS cin.tie(0)->sync_with_stdio(false), cout.tie(0) #define FILE(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout) using namespace std; typedef int64_t i64; typedef uint32_t u32; typedef uint64_t u64; typedef __int128 i128; typedef __uint128_t u128; typedef double db; typedef long long ll; // #define getchar getchar_unlocked // #define putchar putchar_unlocked template <class T> inline void read(T &s) { s = 0; reg int f = 1; reg char c = getchar(); while (!isdigit(c)) f ^= (c == '-'), c = getchar(); while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar(); s = f ? s : -s; } template <class T> inline void wr(T x) { if (x < 0) putchar('-'), x = -x; int buf[21], top = 0; while (x) buf[++top] = x % 10, x /= 10; if (!top) buf[++top] = 0; while (top) putchar(buf[top--] ^ '0'); } template <class T> inline bool ckmn(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool ckmx(T &a, T b) { return a < b ? a = b, 1 : 0; } template <class T, class... A> inline void read(T &x, A &...a) { read(x), read(a...); } template <class T, class... A> inline void ckmn(T &x, T y, A &...a) { return ckmn(x, y) | ckmn(x, a...); } template <class T, class... A> inline void ckmx(T &x, T y, A &...a) { return ckmx(x, y) | ckmx(x, a...); } const ll NN = 1e6 + 5, mod = 1e9 + 9; ll Pre[NN], Inv[NN]; inline ll qpow(ll a, ll b) { return !b ? 1ll : qpow(a * a % mod, b >> 1) * ((b & 1) ? a : 1ll) % mod; } inline ll C(ll n, ll m) { return (n < m || m < 0) ? 0 : Pre[n] * Inv[m] % mod * Inv[n - m] % mod; } inline void InitC(int n) { Pre[0] = Inv[0] = 1; fo(i, 1, n) Pre[i] = Pre[i - 1] * i % mod; Inv[n] = qpow(Pre[n], mod - 2); Fo(i, n - 1, 1) Inv[i] = Inv[i + 1] * (i + 1) % mod; } const int N = 2e5 + 5, B = 800; int n, b[N], len, bc, st[N], ed[N], pos[N]; struct seg { #define rt tr[p] #define lc p << 1 #define rc p << 1 | 1 #define lson tr[p << 1] #define rson tr[p << 1 | 1] vector<int> tr[(B + 5) << 2]; inline void up(int p) { rt.clear(); int pl = 0, pr = 0; while (pl < lson.size() && pr < rson.size()) { if (rson[pr] - pl <= lson[pl]) rt.pb(rson[pr] - pl), ++pr; else rt.pb(lson[pl]), ++pl; } while (pl < lson.size()) rt.pb(lson[pl]), ++pl; while (pr < rson.size()) rt.pb(rson[pr] - pl), ++pr; } inline void build(int p, int pl, int pr) { if (pl == pr) return rt.pb(pl - b[pl]), void(); int mid = (pl + pr) >> 1; build(lc, pl, mid), build(rc, mid + 1, pr), up(p); } inline void update(int p, int pl, int pr, int x) { if (pl == pr) return rt[0] = pl - b[pl], void(); int mid = (pl + pr) >> 1; x <= mid ? update(lc, pl, mid, x) : update(rc, mid + 1, pr, x); up(p); } #undef rt #undef lc #undef rc #undef lson #undef rson } tr[N / 800 + 1]; signed main() { read(n), bc = (n - 1) / B + 1; fo(i, 1, n) pos[i] = (i - 1) / B + 1, ed[pos[i]] = i; Fo(i, n, 1) st[pos[i]] = i; fo(i, 1, n) read(b[i]); fo(i, 1, bc) tr[i].build(1, st[i], ed[i]); int q; read(q); while (q--) { int op, i, x; read(op, i); if (op == 1) { read(x); b[i] = x; tr[pos[i]].update(1, st[pos[i]], ed[pos[i]], i); } else { x = i - b[i]; fo(j, i + 1, ed[pos[i]]) if (x >= j - b[j]) ++x; fo(j, pos[i] + 1, bc) { vector<int> &vec = tr[j].tr[1]; x += ubd(all(vec), x) - vec.begin(); } wr(x), pn; } } return 0; } ```