Lpl and Energy-saving Lamps

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 mm 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 nn and 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 nn integers k_1, k_2, ..., k_nk
1

,k
2

,...,k
n

(1 \le k_j \le 10000, j = 1, 2, ..., n)(1≤k
j

≤10000,j=1,2,...,n) — the number of lamps in the rooms of the Castle. The number in position jj is the number of lamps in jj-th room. Room numbers are given in accordance with Lpl's list.

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

The fourth line contains qq integers d_1, d_2, ..., d_qd
1

,d
2

,...,d
q

(1 \le d_p \le 100000, p = 1, 2, ..., q)(1≤d
p

≤100000,p=1,2,...,q) — numbers of months, in which queries are formed.

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

Output
Print qq lines.

Line pp 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 d_pd
p

month.

Hint
Explanation for the sample:

In the first month, he bought 44 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 11 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,1------11,1−−−−−−1 room's lamps were replaced already, 11 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 南京赛区网络预赛

【思路】:题目要求我们每次更改最左端符合条件的点,有个坑点,如果更新完还不为0,那还要接着修改最左端符合条件的点,
显然是建立一颗线段树,然后修改query函数,每次找出最符合条件的点,更改即可

附上代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 100005;
int arr[MAXN], n, m, q, brr[MAXN];
struct Segtree{
    struct NODE{
        int l, r, mins;
    }seg[MAXN << 2];
    void build(int root, int l, int r){
        seg[root].l = l, seg[root].r = r;
        if(l == r){
            seg[root].mins = arr[l];
            return ;
        }
        int mid = (l + r) >> 1;
        build(root << 1, l, mid);
        build(root << 1 | 1, mid + 1, r);
        seg[root].mins = min(seg[root << 1].mins, seg[root << 1 | 1].mins);
        return ;
    }
    void update(int root, int cnt, int val){
        if(seg[root].l == seg[root].r){
            seg[root].mins = val;
            return ;
        }
        int mid = (seg[root].l + seg[root].r) >> 1;
        if(cnt <= mid){
            update(root << 1, cnt, val);
        }
        else{
            update(root << 1 | 1, cnt, val);
        }
        seg[root].mins = min(seg[root << 1].mins, seg[root << 1 | 1].mins);
        return ;
    }
    int query(int root, int x){
        if(seg[root].l == seg[root].r)  return seg[root].l;
        if(seg[root].mins > x){
            return -1;
        }
        if(seg[root << 1].mins <= x){
            return query(root << 1, x);
        }
        else{
            return query(root << 1 | 1, x);
        }
    }
}seg;
pair<int, int>parr[MAXN];
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i ++){
        scanf("%d", &arr[i]);
    }
    seg.build(1, 1, n);
    scanf("%d", &q);
    int maxs = 0;
    for(int i = 1; i <= q; i ++){
        scanf("%d", &brr[i]);
        maxs = max(maxs, brr[i]);
    }
    int ans = 0, sum = 0, day;
    for(int i = 1; i <= maxs; i ++){
        if(ans == n){
            day = i - 1;
            break;
        }
        sum += m;
        int num = seg.query(1, sum);
        while(num != -1){
            sum -=  arr[num];
            seg.update(1, num, 10001);
            ans ++;
            num = seg.query(1, sum);
        }
        parr[i].first = ans, parr[i].second = sum;
    }
    for(int i = 1; i <= q; i ++){
        if(brr[i] >= day){
            printf("%d %d\n", parr[day].first, parr[day].second);
        }
        else{
            printf("%d %d\n", parr[brr[i]].first, parr[brr[i]].second);
        }
    }
    return 0;
}
posted @ 2019-10-25 11:24  moxin0509  阅读(131)  评论(0编辑  收藏  举报