2018ACM/ICPC南京站网络赛G Lpl and Energy-saving Lamps(线段树)

2018ACM/ICPC南京站网络赛G Lpl and Energy-saving Lamps

During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the Castle? Dragon smiled enigmatically and answered that it is a big secret. After a pause, Dragon added:

— We have a contract. A rental agreement. He always works all day long. He likes silence. Besides that, there are many more advantages of living here in the Castle. Say, it is easy to justify a missed call: a phone ring can’t reach the other side of the Castle from where the phone has been left. So, the imprisonment is just a tale. Actually, he thinks about everything. He is smart. For instance, he started replacing incandescent lamps with energy-saving lamps in the whole Castle…

Lpl chose a model of energy-saving lamps and started the replacement as described below. He numbered all rooms in the Castle and counted how many lamps in each room he needs to replace.

At the beginning of each month, Lpl buys mmm energy-saving lamps and replaces lamps in rooms according to his list. He starts from the first room in his list. If the lamps in this room are not replaced yet and Lpl has enough energy-saving lamps to replace all lamps, then he replaces all ones and takes the room out from the list. Otherwise, he’ll just skip it and check the next room in his list. This process repeats until he has no energy-saving lamps or he has checked all rooms in his list. If he still has some energy-saving lamps after he has checked all rooms in his list, he’ll save the rest of energy-saving lamps for the next month.

As soon as all the work is done, he ceases buying new lamps. They are very high quality and have a very long-life cycle.

Your task is for a given number of month and descriptions of rooms to compute in how many rooms the old lamps will be replaced with energy-saving ones and how many energy-saving lamps will remain by the end of each month.
Input

Each input will consist of a single test case.

The first line contains integers nnn and m(1≤n≤100000,1≤m≤100)m (1 \le n \le 100000, 1 \le m \le 100)m(1≤n≤100000,1≤m≤100) — the number of rooms in the Castle and the number of energy-saving lamps, which Lpl buys monthly.

The second line contains nnn integers k1,k2,…,knk_1, k_2, …, k_nk1​,k2​,…,kn​
(1≤kj≤10000,j=1,2,…,n)(1 \le k_j \le 10000, j = 1, 2, …, n)(1≤kj​≤10000,j=1,2,…,n) — the number of lamps in the rooms of the Castle. The number in position jjj is the number of lamps in jjj-th room. Room numbers are given in accordance with Lpl’s list.

The third line contains one integer q(1≤q≤100000)q (1 \le q \le 100000)q(1≤q≤100000) — the number of queries.

The fourth line contains qqq integers d1,d2,…,dqd_1, d_2, …, d_qd1​,d2​,…,dq​
(1≤dp≤100000,p=1,2,…,q)(1 \le d_p \le 100000, p = 1, 2, …, q)(1≤dp​≤100000,p=1,2,…,q) — numbers of months, in which queries are formed.

Months are numbered starting with 111; at the beginning of the first month Lpl buys the first m energy-saving lamps.
Output

Print qqq lines.

Line ppp contains two integers — the number of rooms, in which all old lamps are replaced already, and the number of remaining energy-saving lamps by the end of dpd_pdp​ month.
Hint

Explanation for the sample:

In the first month, he bought 444 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 111 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,1−−−−−−11,1——11,1−−−−−−1 room’s lamps were replaced already, 111 energy-saving lamp remain.
样例输入

5 4
3 10 5 2 7
10
5 1 4 8 7 2 3 6 4 7

样例输出

4 0
1 1
3 6
5 1
5 1
2 0
3 2
4 4
3 6
5 1

题目来源

ACM-ICPC 2018 南京赛区网络预赛

题意:

已知n个房间,每个房间需要有ai个灯,主角在给所有房间都装好节能灯之前,会每个月购买m个节能灯,同时每个月按房间编号从小到大依次给能完全满足需求的房间装节能灯,剩下的节能灯留到下个月用。接下来q个查询,询问第bi个月后已经装好灯的房间数和该月最后剩下的节能灯数。

分析:

可以使用线段树维护区间最小值,查询的时候查询最靠左的小于等于要求数的位置,然后更新成inf表示已经更新,因为查询的月份最多1e5,因此可以先求出每个月的结果存到数组里,之后O(1)查询即可。

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
const int INF = 0x3f3f3f3f;
int sum[N<<2],a[N],ans1[N],ans2[N];

void build(int rt,int l,int r){
    if(l == r){
        sum[rt] = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    sum[rt] = min(sum[rt<<1],sum[rt<<1|1]);
}

void update(int rt,int l,int r,int x){//将位置x更新成inf
    if(l == r){
        sum[rt] = INF;
        return;
    }
    int mid = l + r >> 1;
    if(x <= mid) update(rt<<1,l,mid,x);
    else update(rt<<1|1,mid+1,r,x);
    sum[rt] = min(sum[rt<<1],sum[rt<<1|1]);
}

int query(int rt,int l,int r,int x){//查找到满足小于等于x的最靠左的位置
    if(l == r){
        return l;
    }
    int mid = l + r >> 1;
    if(sum[rt<<1] <= x) return query(rt<<1,l,mid,x);//先往左找,满足条件继续往左,若能找到就直接返回了
    if(sum[rt<<1|1] <= x) return query(rt<<1|1,mid+1,r,x);//找不到在往右侧找
    return -1;//还找不到说明没有返回-1
}
int n,m,q;
int main(){
    while(~scanf("%d%d",&n,&m)){
        for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
        build(1,1,n);
        int ansa = 0,ansb = 0;
        for(int i = 1; i <= 100000; i++){//打表预处理出所有月份的答案
            if(ansa < n){//如果还有没有更换的房间
                ansb += m;//现在手里有的灯泡为上个月剩下的ansb+新买的m
                while(1){//连续更新
                    int pos = query(1,1,n,ansb);//查询符合条件的最靠左的位置
                    if(pos != -1){
                        ansb -= a[pos];
                        ansa++;
                        update(1,1,n,pos);//将已经更换灯泡的房间变成inf
                    }
                    else break;//找不到满足条件的房间了就退出进入下一个月

                }
            }
            ans1[i] = ansa;
            ans2[i] = ansb;
        }
        scanf("%d",&q);
        while(q--){
            int d;
            scanf("%d",&d);
            printf("%d %d\n",ans1[d],ans2[d]);
        }
    }
    return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值