Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学

传送门

文章目录

题意:

思路:

考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是 a 1 ≤ a 2 ≤ a 3 ≤ . . . ≤ a n a_1\le a_2\le a_3\le ...\le a_n a1a2a3...an,显然不可能都是 ≤ \le 号,因为如果插入的话是有可能产生 < < <号的。假设我们现在有 x x x < < < 号,那么应该有 ( n + n − 1 − x n ) \binom{n+n-1-x}{n} (nn+n1x)个序列,因为我们将 a i ≤ a i + 1 a_i\le a_{i+1} aiai+1转换成 a i < a i + 1 + 1 a_i< a_i+1+1 ai<ai+1+1,也就是扩展了一下值域,最多扩展了 n − 1 − x n-1-x n1x个值域,也就是目前值域是 [ 1 , n ∗ 2 − 1 − x ] [1,n*2-1-x] [1,n21x],从中选 n n n个数构成一个严格递增的序列方案就是 ( n ∗ 2 − 1 − x n ) \binom{n*2-1-x}{n} (nn21x),那么现在问题就是如何知道 x x x

首先插入一组最多能产生一个 < < <号,因为有可能若干数插到了同一个数后面,比如这个例子 n = 3 , m = 2 , ( 2 , 1 ) , ( 3 , 2 ) n=3,m=2,(2,1),(3,2) n=3,m=2,(2,1),(3,2),这样在 a 1 a_1 a1前面插入了两个数 a 2 , a 3 a_2,a_3 a2,a3,能确定 a 1 > a 2 , a 1 > a 3 a_1>a_2,a_1>a_3 a1>a2,a1>a3,但是不能确定 a 2 a_2 a2 a 3 a_3 a3的关系,所以这样的话我们就认为 a 2 , a 3 a_2,a_3 a2,a3之间是一个 ≤ \le 号。我们考虑倒着来处理这 m m m组,首先最终序列长度是 n n n,假设现在有 ( x i , y i ) (x_i,y_i) (xi,yi),那么先找到第 y i y_i yi个位置 p p p和第 y i + 1 y_i+1 yi+1的位置 q q q,此时将 p p p删除,将 q q q标记一下代表有个数在他前面有 < < <即可。至于为什么是 y i , y i + 1 y_i,y_{i+1} yi,yi+1而不是 y i − 1 , y i y_{i-1},y_{i} yi1,yi,是因为如果 y i = 1 y_i=1 yi=1的时候, y i − 1 = 0 y_i-1=0 yi1=0,这个时候在线段树上二分就会出错。

在线段树上二分即可。

如果正着来,可以建一个基于权值的平衡树,让后模拟插入即可。

由于题目的问题,线段树不能每次建树。。。

// Problem: F. Top-Notch Insertions
// Contest: Codeforces - Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))
// URL: https://codeforces.com/contest/1561/problem/F
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=998244353,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
PII p[N];
struct Node {
	int l,r;
	int cnt;
}tr[N<<2];

void pushup(int u) {
	tr[u].cnt=tr[L].cnt+tr[R].cnt;
}

void build(int u,int l,int r) {
	tr[u]={l,r};
	if(l==r) {
		tr[u].cnt=1;
		return;
	}
	build(L,l,Mid); build(R,Mid+1,r);
	pushup(u);
}

void change(int u,int pos,int x) {
	if(tr[u].l==tr[u].r) {
		tr[u].cnt=x;
		return;
	}
	if(pos<=Mid) change(L,pos,x);
	else change(R,pos,x);
	pushup(u);
}

int query(int u,int cnt) {
	if(tr[u].l==tr[u].r) return tr[u].l;
	if(cnt<=tr[L].cnt) return query(L,cnt);
	else return query(R,cnt-tr[L].cnt); 
}

LL fun[N],inv[N];

LL qmi(LL a,LL b) {
	LL ans=1;
	while(b) {
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans%mod;
}

LL C(int a,int b) {
	if(a<0||b<0||a<b) return 0;
	return fun[a]*inv[b]%mod*inv[a-b]%mod;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
		
	fun[0]=1;
	for(int i=1;i<N;i++) fun[i]=fun[i-1]*i%mod;
	inv[N-1]=qmi(fun[N-1],mod-2);
	for(int i=N-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
	
	build(1,1,N-1);
	
	int _; scanf("%d",&_);
	while(_--) {
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++) scanf("%d%d",&p[i].X,&p[i].Y);
		set<int>s; vector<int>v;
		for(int i=m;i>=1;i--) {
			int a=query(1,p[i].Y),b=query(1,p[i].Y+1);
			change(1,a,0); s.insert(b); v.pb(a);
		}
		for(auto x:v) change(1,x,1);
		int cnt=s.size();
		printf("%lld\n",C(n*2-1-cnt,n));
	}
	


	return 0;
}
/*

*/









// Problem: F. Top-Notch Insertions
// Contest: Codeforces - Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))
// URL: https://codeforces.com/contest/1561/problem/F
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=998244353,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
int root,tot;
int x,y;
struct Node {
	int l,r;
	int size,fa,val,rank,tag;
}tr[N];

int newnode(int v) {
	tr[++tot].val=v,tr[tot].size=1,tr[tot].rank=rand();
	tr[tot].l=tr[tot].r=tr[tot].tag=0;
	return tot;
}

void pushup(int u) {
	if(!u) return;
	tr[u].size=tr[tr[u].l].size+tr[tr[u].r].size+1;
	// tr[u].val=max(tr[tr[u].l].val,tr[tr[u].r].val);
}

void pushdown(int u) {
	if(!u) return;
	int tag=tr[u].tag; tr[u].tag=0;
	if(tr[u].l) tr[tr[u].l].tag+=tag,tr[tr[u].l].val+=tag;
	if(tr[u].r) tr[tr[u].r].tag+=tag,tr[tr[u].r].val+=tag;
}

void split(int u,int k,int &x,int &y) {
	if(!u) { x=y=0; return; }
	pushdown(u);
	if(tr[u].val<=k) x=u,split(tr[u].r,k,tr[u].r,y);
	else y=u,split(tr[u].l,k,x,tr[u].l);
	pushup(u);
}

int merge(int u,int v) {
	if(!u||!v) return u+v;
	if(tr[u].rank<tr[v].rank) {
		pushdown(u);
		tr[u].r=merge(tr[u].r,v);
		pushup(u);
		return u;
	}
	else {
		pushdown(v);
		tr[v].l=merge(u,tr[v].l);
		pushup(v);
		return v;
	}
}

int find(int u,int x) {
	if(!u) return 0;
	if(tr[u].val==x) return 1;
	pushdown(u);
	if(x<tr[u].val) return find(tr[u].l,x);
	else return find(tr[u].r,x);
}

LL fun[N],inv[N];

LL qmi(LL a,LL b) {
	LL ans=1;
	while(b) {
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans%mod;
}

LL C(int a,int b) {
	if(a<0||b<0||a<b) return 0;
	return fun[a]*inv[b]%mod*inv[a-b]%mod;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
		
	fun[0]=1;
	for(int i=1;i<N;i++) fun[i]=fun[i-1]*i%mod;
	inv[N-1]=qmi(fun[N-1],mod-2);
	for(int i=N-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
	
	int _; scanf("%d",&_);
	while(_--) {
		root=tot=0;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++) {
			int a,b; scanf("%d%d",&a,&b);
			int flag=find(root,b);
			split(root,b-1,x,y);
			if(y) tr[y].val++,tr[y].tag++;
			root=merge(x,flag? y:merge(newnode(b+1),y));
		}
		printf("%lld\n",C(n*2-1-tot,n));
	}
	


	return 0;
}
/*

*/









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值