南京赛区网络预赛 G. Lpl and Energy-saving Lamps(线段树)

又是一道线段树水题啊,
比赛的时候怎么就不看看呢 aaaaa
题意:已知n个房间,每个房间需要有ai个灯,主角在给所有房间都装好节能灯之前,会每个月购买m个节能灯,同时每个月按房间编号从小到大依次给能完全满足需求的房间装节能灯,剩下的节能灯留到下个月用。接下来q个查询,询问第bi个月后已经装好灯的房间数和该月最后剩下的节能灯数。
思路:直接找最左面的比当前灯泡数目小的房间然后更新就好了,房间最多1e5,所以不会超时
因为查询的月份最多1e5,因此可以先求出每个月的结果存到数组里,之后O(1)查询即可。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int N=1e5+10;
int ar[N],tree[N<<2],ans1[N],ans2[N],an,cnt1;
void build(int l,int r,int rt)
{
    if(l==r){
        tree[rt]=ar[l];return ;
    }
    int m=(l+r)/2;
    build(lson);build(rson);
    tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);
}
void query(int l,int r,int rt)
{
    int m=(l+r)/2;
    if(l==r){
        an-=tree[rt],tree[rt]=inf;
        return ;
    }
    if(tree[rt<<1]<=an) query(lson);
    else if(tree[rt<<1|1]<=an) query(rson);
    tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);
}
int main()
{
    int n,m,q,d;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&ar[i]);
    build(1,n,1);
    an=0,cnt1=0;
    for(int i=1;i<N;i++)
    {
        if(tree[1]==inf){
            ans1[i]=cnt1,ans2[i]=an;continue;
        }
        an+=m;
        while(an>=tree[1]){
            query(1,n,1);
            cnt1++;
        }
        ans1[i]=cnt1,ans2[i]=an;
    }
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d",&d);
        printf("%d %d\n",ans1[d],ans2[d]);
    }
    return 0;
}
posted @ 2018-09-03 09:32  ffgcc  阅读(88)  评论(0编辑  收藏  举报