跳到正文

题解

[NOI2022] 众数

pitiless0514约 9 分钟
原文

这是 2025年10月31日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2022年8月29日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 9 分钟
题目
P8496

版本

  1. v2当前
  2. v1

Statement

好像无法简化,已经很简洁了,所以看原题面吧。

Solution

考虑对于这道题目众数的定义,显然如果一个数列有众数,最终这个众数会是一个中位数。

于是对于众数的询问,我们可以考虑使用一颗值域线段树维护每个数出现的次数,然后每次这个线段树上二分找出中位数,然后再看找到的这个数的出现次数是否严格大于序列一半就好了。

这是对单个序列的查询,33 操作中是 mm 个序列,于是可以在这 mm 个线段树上一起二分。

而对于序列的维护,我们可以考虑使用 STL\text{STL} 中的 list\text{list} 函数进行维护,对于 1,21,2 操作就是 push_back() 和 pop_back()。

对于 44 操作,首先要将两个序列对应的线段树进行线段树合并,然后对于 list\text{list} 的合并,我们可以考虑使用启发式合并。

因为在 c++11\text{c++11} 以后 list\text{list} 中的 swap\text{swap} 的复杂度为 O(1)O(1),所以我们将元素更多的那个序列和还没用过的序列 zz 进行互换。

然后对于剩下的元素较少的序列,如果他是序列 xx,那么就从后往前让他的元素 push_front() 到序列 zz 中,如果他是序列 yy,那么就从前往后让他的元素 push_back() 到序列 zz 中。

总复杂度为 O(nlog⁡n)O(n \log n)。

C++
// 德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱
// 德丽莎的可爱在于德丽莎很可爱,德丽莎为什么很可爱呢,这是因为德丽莎很可爱!
// 没有力量的理想是戏言,没有理想的力量是空虚
#include <bits/stdc++.h>
#define LL long long
#define int long long
using namespace std;
char ibuf[1 << 15], *p1, *p2;
#define getchar() (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 15, stdin), p1==p2) ? EOF : *p1++)
inline int read() {
  char ch = getchar();  int x = 0, f = 1;
  while (ch < '0' || ch > '9')  {  if (ch == '-')  f = -1;  ch = getchar();  }
  while (ch >= '0' && ch <= '9')  x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x * f;
}
void print(LL x) {
  if (x > 9)  print(x / 10);
  putchar(x % 10 + '0');
}
template<class T> bool chkmin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool chkmax(T &a, T b) { return a < b ? (a = b, true) : false; }
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define repd(i, l, r) for (int i = (l); i >= (r); i--)
#define REP(i, l, r)  for (int i = (l); i < (r); i++)
const int N = 2e6 + 5;
bool o1;
int n, q, lim, cnt, rt[N], lc[N * 20], rc[N * 20], s[N * 20];
list <int> a[N];
void insert(int &p,int l,int r,int pos,int val) {
  if (!p)  p = ++cnt;
  if (l == r) {  s[p] += val;  return;  }
  int mid = (l + r) >> 1;
  if (pos <= mid)  insert(lc[p], l, mid, pos, val);
  else  insert(rc[p], mid + 1, r, pos, val);
  s[p] = 0;
  if (lc[p])  s[p] += s[lc[p]];
  if (rc[p])  s[p] += s[rc[p]];
}
int merge(int a,int b,int x,int y) {
  if (!a || !b)  return a + b;
  int rt = ++cnt;
  if (x == y) {  s[rt] = s[a] + s[b];  return rt;  }
  int mid = (x + y) >> 1;
  lc[rt] = merge(lc[a], lc[b], x, mid); 
  rc[rt] = merge(rc[a], rc[b], mid + 1, y);
  s[rt] = 0;
  if (lc[rt])  s[rt] += s[lc[rt]];
  if (rc[rt])  s[rt] += s[rc[rt]];
  return rt;
}
int ask(int p,int l,int r,int pos) {
  if (l == r)  return s[p];
  int mid = (l + r) >> 1;
  if (pos <= mid)  return ask(lc[p], l, mid, pos);
  else  return ask(rc[p], mid + 1, r, pos);
}
int len, pt[N], tt[N];
pair <int,int> work(int k,int l,int r) {
  if (l == r) {
    int now = 0;
    rep (i, 1, len)  now += s[pt[i]];
    return {l, now};
  }
  int mid = (l + r) >> 1, sum = 0;
  rep (i, 1, len)  sum += s[lc[pt[i]]];
  if (sum >= k) {
    rep (i, 1, len)  pt[i] = lc[pt[i]];
    return work(k, l, mid);
  }  else {
    rep (i, 1, len)  pt[i] = rc[pt[i]];
    return work(k - sum, mid + 1, r);
  }
}
bool o2;
void solve() {
  // cout << "Memory: " << (double) ((&o2 - &o1) / 1024 / 1024) << ".Mib\n";
  n = read(), q = read();  lim = n + q;
  rep (i, 1, n) {
    int sz = read();
    rep (j, 1, sz) {  int x = read();  a[i].push_back(x);  insert(rt[i], 1, lim, x, 1);  }
  }
  rep (i, 1, q) {
    int op = read(), x, y, z;
    if (op == 1) {
      x = read(), y = read();
      a[x].push_back(y);  
      insert(rt[x], 1, lim, y, 1);
    }  else if (op == 2) {
      x = read();
      y = a[x].back();  a[x].pop_back();
      insert(rt[x], 1, lim, y, -1);
    }  else if (op == 3) {
      len = read();
      int sum = 0;
      rep (j, 1, len)  pt[j] = read(), sum += a[pt[j]].size(), pt[j] = rt[pt[j]];
      auto it = work((sum + 1) >> 1, 1, lim);
      if (it.second * 2 > sum)  printf("%lld\n", it.first);
      else  printf("-1\n");
    }  else if (op == 4) {
      x = read(), y = read(), z = read();
      rt[z] = rt[x];
      rt[z] = merge(rt[z], rt[y], 1, lim);
      if (a[x].size() > a[y].size()) {
        swap(a[z], a[x]);  
        for (auto it : a[y])  a[z].push_back(it);
        while (a[y].size())  a[y].pop_back();
      }  else {
        swap(a[z], a[y]);
        reverse(a[x].begin(), a[x].end());
        for (auto it : a[x])  a[z].push_front(it);
        while (a[x].size())  a[x].pop_back();
      }
    }
  }
}
signed main () {
#ifdef LOCAL_DEFINE
  freopen("1.in", "r", stdin);
  freopen("1.ans", "w", stdout);
#endif
  int T = 1;  while (T--)  solve();
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
/*
2 8
3 1 1 2
3 3 3 3
3 1 1
3 1 2
4 2 1 3
3 1 3

2 3

3 1 3
1 3 1
3 1 3
*/