soj#2402 「THUPC 2017」天天爱射击 / Shooting

分析

按照被穿过多少次整体二分即可

代码

#include<bits/stdc++.h>
using namespace std;
#define lb(x) x&(-x)
int n,m,le[200100],ri[200100],s[200100],pl[200100],N;
int id[200100],ans[200100],c1[200100],c2[100100],d[200100];
inline void add(int x,int k){while(x<=N)d[x]+=k,x+=lb(x);}
inline int que(int x){int res=0;while(x)res+=d[x],x-=lb(x);return res;}
inline void work(int l,int r,int x,int y){
    if(l>r||x>y)return;
    if(l==r){ans[l]=y-x+1;return;}
    int i,j,k,mid=(l+r)>>1,cnt1=0,cnt2=0;
    for(i=l;i<=mid;i++)add(pl[i],1);
    for(i=x;i<=y;i++){
      int wh=id[i],k=que(ri[wh])-que(le[wh]-1);
      if(k>=s[wh])c1[++cnt1]=wh;
        else s[wh]-=k,c2[++cnt2]=wh;
    }
    for(i=1;i<=cnt1;i++)id[x+i-1]=c1[i];
    for(i=1;i<=cnt2;i++)id[x+cnt1+i-1]=c2[i];
    for(i=l;i<=mid;i++)add(pl[i],-1);
    work(l,mid,x,x+cnt1-1),work(mid+1,r,x+cnt1,y);
}
int main(){
    int i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)scanf("%d%d%d",&le[i],&ri[i],&s[i]),N=max(N,ri[i]);
    for(i=1;i<=m;i++)scanf("%d",&pl[i]);
    for(i=1;i<=n;i++)id[i]=i;
    work(1,m+1,1,n);
    for(i=1;i<=m;i++)printf("%d\n",ans[i]);
    return 0;
}

 

posted @ 2019-10-30 14:36  水题收割者  阅读(179)  评论(0编辑  收藏  举报