正文
增加 39 行删除 55 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778```#include<bits/stdc++.h>using namespace std;int n,a[1000001],stk[1000001],ans[1000001],tree[1000001];struct node{int l,r,val,id;};node f[2000001];bool cmp(node x,node y){return x.l>y.l||x.l==y.l&&(x.r<y.r||x.r==y.r&&x.id<y.id);}int lowbit(int x){return x&-x;}void edit(int x,int k){while(x<=n){tree[x]=max(tree[x],k);x+=lowbit(x);}}int query(int x){int ans=0;while(x>0){ans=max(ans,tree[x]);x-=lowbit(x);}return ans;}int main(){// freopen("c.in","r",stdin);// freopen("c.out","w",stdout);int m,cnt,top;cin>>n>>m;for(int i=1;i<=n;i++)cin>>a[i];top=0;cnt=0;for(int i=1;i<=n;i++){while(top>0&&a[i]>=a[stk[top]])top--;if(top>0){cnt++;f[cnt].l=stk[top];f[cnt].r=i;f[cnt].val=a[i]+a[stk[top]];f[cnt].id=0;}stk[++top]=i;}for(int i=1;i<=m;i++){cnt++;cin>>f[cnt].l>>f[cnt].r>>f[cnt].val;f[cnt].id=i;}sort(f+1,f+cnt+1,cmp);for(int i=1;i<=cnt;i++)if(f[i].id==0)edit(f[i].r,f[i].val);elseif(query(f[i].r)<=f[i].val)ans[f[i].id]=1;elseans[f[i].id]=0;for(int i=1;i<=m;i++)cout<<ans[i]<<endl;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;}```