BZOJ 2527 Meteors

http://www.lydsy.com/JudgeOnline/problem.php?id=2527

思路:整体二分

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 1000000010
int n,m,a[500005],first[500005],next[500005],tot;
int tol[500005],tor[500005],ans[500005],id[500005],K;
ll cur[500005],tmp[500005],h[500005],go[500005];
struct node{
    int id,l,r,w;
}q[500005];
int read(){
    int t=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
    return t*f;
}
ll sum(int x){
    ll res=0;
    while (x){
      res+=h[x];
      x-=(x)&(-x);
    }
    return res;
}
void add(int x,ll v){
   for (int i=x;i<=m;i+=(i)&(-i))
        h[i]+=v;
}
void add(int x,int y,ll v){
    add(x,v);add(y+1,-v);
}
void solve(int l,int r,int x,int y){
    if (x>y) return;
    if (l==r){
        for (int i=x;i<=y;i++)
         ans[id[i]]=l;
        return;
    }
    int lnum=0,rnum=0;
    int mid=(l+r)>>1;
    for (int i=l;i<=mid;i++)
     if (q[i].l<=q[i].r) add(q[i].l,q[i].r,q[i].w);
     else add(q[i].l,m,q[i].w),add(1,q[i].r,q[i].w);
    for (int i=x;i<=y;i++){
        tmp[id[i]]=0;
        for (int j=first[id[i]];j;j=next[j]){
            int pur=go[j];
            tmp[id[i]]+=sum(pur);
            if (tmp[id[i]]+cur[id[i]]>=a[id[i]]) break;
        }
        if (tmp[id[i]]+cur[id[i]]>=a[id[i]]) tol[++lnum]=id[i];
        else tor[++rnum]=id[i],cur[id[i]]+=tmp[id[i]];
    } 
    for (int i=l;i<=mid;i++)
     if (q[i].l<=q[i].r) add(q[i].l,q[i].r,-q[i].w);
     else add(q[i].l,m,-q[i].w),add(1,q[i].r,-q[i].w);
    for (int i=0;i<lnum;i++)
     id[x+i]=tol[i+1];
    for (int i=0;i<rnum;i++)
     id[x+lnum+i]=tor[i+1]; 
    solve(l,mid,x,x+lnum-1);
    solve(mid+1,r,x+lnum,y);
}
int main(){
    n=read();m=read();
    for (int i=1;i<=m;i++){
        int x=read();
        tot++;
        next[tot]=first[x];
        first[x]=tot;
        go[tot]=i;
    }
    for (int i=1;i<=n;i++)
     a[i]=read();
    K=read();
    for (int i=1;i<=K;i++){
        q[i].l=read();q[i].r=read();q[i].w=read();
        q[i].id=i;
    } 
    K++;
    q[K].l=1;q[K].r=m;q[K].w=inf;
    q[K].id=K;
    for (int i=1;i<=n;i++)
     id[i]=i;
    solve(1,K,1,n);
    for (int i=1;i<=n;i++){
        if (ans[i]!=K) printf("%d\n",ans[i]);
        else puts("NIE");
    }
    return 0;
}

 

posted @ 2016-07-12 17:37  GFY  阅读(149)  评论(0编辑  收藏  举报