跳到正文

题解

题解:CF1540D Inverse Inversions

lhc0707约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年8月14日
收录
2026年6月20日
更新
2026年11月11日
篇幅
约 9 分钟
题目
CF1540D

版本

  1. v1当前

ci=i−bic_i=i-b_i 的含义是 bib_i 的前缀中 pip_i 的排名,扫 i:1→ni:1\rightarrow n,由定义设 pi=cip_i=c_i 然后把 pj≥cip_j\geq c_i 的 pjp_j 加一,于是可以构造出唯一的 pp。这样可以做到 O(qn)O(qn)。

对于一个区间,定义 t(x)t(x) 表示 xx 经过区间之后会变成的数,这是一个分段函数,并且每一段的斜率都是 1。这个东西不好维护,转而维护 f(i)f(i) 表示表示如果 x≥f(i)x\geq f(i) 那么 x→x+ix\rightarrow x+i,考虑合并两个区间的函数 flf_l 和 frf_r,考虑一共增加了 kk,枚举分别加了 i,ji,j,那么 f(k)≥fl(i),x+i≥fr(j)f(k)\geq f_l(i),x+i\geq f_r(j),于是 f(k)=min⁡i+j=k{max⁡(fl(i),fr(j)−i)}f(k)=\min_{i+j=k}\{\max(f_l(i),f_r(j)-i)\}。发现这样合并有很好的性质:如果 kk 是由 (i,j)(i,j) 转移而来的,那么 k+1k+1 就只能从 (i+1,j)(i+1,j) 或者 (i,j+1)(i,j+1) 转移过来!原因是 ff 单调不降,(i,j)(i,j) 能多贡献就多贡献,不会回退,就得到了 O(len)O(len) 合并信息的做法。线段树维护这个信息就能做到 O(nlog⁡2n)O(n\log^2 n)。但是这样维护单调修改的话就是 O(n)O(n) 的,考虑分块平衡复杂度,分成 BB 块,每块开线段树,这样修改时间复杂度 O(B)O(B),查询时间复杂度 O(nBlog⁡n)O(\frac{n}{B}\log n),平衡一下令 B=nlog⁡nB=\sqrt{n\log n} 就可以做到 O(nnlog⁡n)O(n\sqrt{n\log n}) 了。

代码:

C++
#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;
}
原文