跳到正文

个人记录

123

heyechen约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年10月12日
收录
2026年10月12日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
int a[200001];
struct node{
	int mx;
	node *l,*r;
};
node *build()
{
	node *head=new node;
	head->mx=0;
	head->l=head->r=NULL;
	return head;
}
void pushup(node *head)
{
	head->mx=max(head->l->mx,head->r->mx);
}
void edit(node *head,int l,int r,int x,int k)
{
	if(l==r)
	{
		head->mx+=k;
		return;
	}
	if(head->l==NULL)
		head->l=build();
	if(head->r==NULL)
		head->r=build();
	if(x<=(l+r)/2)
		edit(head->l,l,(l+r)/2,x,k);
	else	
		edit(head->r,(l+r)/2+1,r,x,k);
	pushup(head);
}
int main()
{
	freopen("reorder.in","r",stdin);
	freopen("reorder.out","w",stdout);
	int n,q,p,x;
	node *head;
	cin>>n>>q;
	head=build();
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		edit(head,1,inf,a[i],1);
	}
	cout<<n-head->mx<<endl;
	for(int i=1;i<=q;i++)
	{
		cin>>p>>x;
		edit(head,1,inf,a[p],-1);
		a[p]=x;
		edit(head,1,inf,x,1);
		cout<<n-head->mx<<endl;
	}
	return 0;
}
原文