跳到正文

题解

题解:P5607 [Ynoi2013] 无力回天 NOI2017

FS_NEO约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年1月20日
收录
2026年1月25日
更新
2026年11月9日
篇幅
约 7 分钟
题目
P5607

版本

  1. v1当前

回天无力 AHOI2026。


发现自己代码常数和 IQ 一样低,有趣吧。

先并转交,然后对集合大小根号分治,给每个大小大于 m\sqrt{m} 的集合开一个数组,长度为 mm,第 xx 位为 0/10/1 代表集合是否有元素 xx。

处理询问。显然两个大小小于 m\sqrt{m} 的集合可以直接查;而一个小于,一个大于的情况也类似。两个大于很讨厌,但注意到修改次数是少的,于是我们维护一个数组 ansx,yans_{x,y} 表示第 x 个大集合与第 y 个大集合交集大小。这可以在修改时 O(m)O(\sqrt{m}) 地暴力更新。

于是我们做完了,但是被卡了一个点。考虑优化常数,我们在两个小集合计算答案的时候精细实现一下,用更小的集合标记,较大的集合查询,具体见代码。

C++
/*

		2026.1.20

  * Happy Zenith Noises *

*/
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define RG register
using namespace std;
typedef pair<int,int>P;
typedef pair<int,P>PP;
typedef unsigned long long ull;
const int MAXN=1000005,B=1250;
int cnt,n,mx;
int ans[MAXN/B+5][MAXN/B+5],rev[MAXN],tp[MAXN],si[MAXN];
bool vis[MAXN];
bitset<MAXN>a[MAXN/B+5];
vector<int>b[MAXN];
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}

  ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
#endif
  char gc() {
#if DEBUG  // 调试,可显示字符
    return getchar();
#endif
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }

  void read(int &x) {
    bool neg = false;
    x = 0;
    char ch = gc();
    for (; !isdigit(ch); ch = gc())
      if (ch == '-') neg = true;
    if (neg)
      for (; isdigit(ch); ch = gc()) x = x * 10 + ('0' - ch);
    else
      for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
  }

  void read(char *s) {
    char ch = gc();
    for (; isspace(ch); ch = gc());
    for (; !isspace(ch); ch = gc()) *s++ = ch;
    *s = 0;
  }

  void read(char &c) { for (c = gc(); isspace(c); c = gc()); }

  void push(const char &c) {
#if DEBUG  // 调试,可显示字符
    putchar(c);
#else
    if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
    *pp++ = c;
#endif
  }

  void write(int x) {
    bool neg = false;
    if (x < 0) {
      neg = true;
      push('-');
    }
    static int sta[40];
    int top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while (x);
    if (neg)
      while (top) push('0' - sta[--top]);
    else
      while (top) push('0' + sta[--top]);
  }

  void write(int x, char lastChar) { write(x), push(lastChar); }
} io;
signed main(){
	io.read(n);
	while(n--){
		int op,x,y;io.read(op),io.read(x),io.read(y);
		if(op==1){
			si[x]++;b[x].pb(y);mx=max(mx,si[x]);
			if(tp[x]){
				a[rev[x]][y]=1;int rx=rev[x];
				for(register int i=1;i<rx;i++)ans[i][rx]+=(a[i][y]==1);
				for(register int i=rx+1;i<=cnt;i++)ans[rx][i]+=(a[i][y]==1);
			}
			else{
				if(si[x]==B){
					tp[x]=1;
					cnt++,rev[x]=cnt;
					for(register int i:b[x])a[cnt][i]=1;
					for(register int i=1;i<cnt;i++)ans[i][cnt]=(a[cnt]&a[i]).count();
				}
			}
		}
		else{
			if(x==y){io.write(si[x],'\n');continue;}
			int s=0;
			if(tp[y])swap(x,y);
			if(!tp[x]&&!tp[y]){
				if(si[x]>si[y])swap(x,y);
				for(register int i:b[x])vis[i]=1;
				for(register int i:b[y])s+=vis[i];
				for(register int i:b[x])vis[i]=0;
			}
			else if(!tp[y]){int rx=rev[x];for(register int i:b[y])if(a[rx][i])s++;}
			else s=ans[min(rev[x],rev[y])][max(rev[x],rev[y])];
			io.write(si[x]+si[y]-s,'\n');
		}
	}
	return 0;
}

其实写这篇题解,单纯是想记录一下自己 O(mm)O(m \sqrt{m}) 创过 1e6,1s 题目的伟大事迹。

原文