正文
增加 46 行删除 122 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138```#include<bits/stdc++.h>using namespace std;int n,k,f[1000001],p[1000001],s[1000002],d[1000001];bool q;vector<int>tree[1000001];queue<pair<int,int> >que;int dfs(int root,int fa){int ans=0;for(int i=0;i<tree[root].size();i++)if(tree[root][i]!=fa)ans=max(ans,dfs(tree[root][i],root)+1);return ans;}int solve(int root,int x,int fa,int ban){int sum=0,tmp,now,dis;for(int i=0;i<tree[root].size();i++)if(f[tree[root][i]]>x)sum++;if(sum==2){dis=0;for(int i=0;i<tree[root].size();i++)if(f[tree[root][i]]<=x)dis=max(dis,dfs(tree[root][i],root)+1);else if(tree[root][i]!=fa)tmp=solve(tree[root][i],x,root,ban)+1;d[tmp]=dis;return tmp;}else{for(int i=0;i<tree[root].size();i++)if(f[tree[root][i]]>x&&tree[root][i]!=fa){tmp=solve(tree[root][i],x,root,ban);if(tmp>=ban){for(int j=1;j<=tmp;j++){if(j+(x-d[j])+1-ban<=j-(x-d[j])){s[max(1,min(tmp+1,j+(x-d[j])+1-ban))]++;s[max(1,min(tmp+1,j-(x-d[j])+1))]--;}}now=0;for(int j=1;j<=tmp-ban+1;j++){now+=s[j];if(now==0)q=true;}for(int j=1;j<=tmp+1;j++)s[j]=0;}}return 0;}}bool check(int x){int sum,pos;pos=sum=0;for(int i=1;i<=n;i++)if(f[i]>x){sum++;if(f[i]==x+1)pos=i;}if(sum<=k)return true;if(sum>k+2*x)return false;q=false;solve(pos,x,0,sum-k);return q;}int binary_search(int l,int r){if(l>r)return l;int mid=(l+r)/2;if(check(mid))return binary_search(l,mid-1);elsereturn binary_search(mid+1,r);}int main(){int c,t,u,v;pair<int,int>tmp;scanf("%d%d",&c,&t);for(int i=1;i<=t;i++){scanf("%d%d",&n,&k);for(int j=1;j<=n;j++)f[j]=p[j]=0;for(int j=1;j<n;j++){scanf("%d%d",&u,&v);tree[u].push_back(v);tree[v].push_back(u);p[u]++;p[v]++;}if(n==k){printf("%d\n",0);for(int j=1;j<=n;j++)tree[j].clear();continue;}for(int j=1;j<=n;j++)if(p[j]==1)que.push(make_pair(j,1));while(!que.empty()){tmp=que.front();que.pop();f[tmp.first]=tmp.second;for(int ii=0;ii<tree[tmp.first].size();ii++){p[tree[tmp.first][ii]]--;if(p[tree[tmp.first][ii]]==1)que.push(make_pair(tree[tmp.first][ii],tmp.second+1));}}printf("%d\n",binary_search(1,n));for(int j=1;j<=n;j++)tree[j].clear();}return 0;}```1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162```#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);elseedit(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;}```