计蒜客- Lpl and Energy-saving Lamps 线段树+点更新

Posted the-way-of-cas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计蒜客- 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(1n100000,1m100) — 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)(1kj?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(1q100000) — 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)(1dp?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个房间,每个房间ki个灯,每个月买入m个新灯,同时对房间的旧灯进行更换,按照给定的顺序换,如果不够,可以下个月在换。问每个月已经更换完成的房间数以及当月月底剩余的新灯数量

代码:

技术分享图片
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #define ll long long
 5 #define inf 0x3f3f3f3f
 6 #define maxn 100009
 7 #define maxq 100009
 8 #define maxdp 100009
 9 #define ls l,mid,rt<<1
10 #define rs mid+1,r,rt<<1|1
11 
12 using namespace std;
13 
14 int n, m, q;
15 int rm[maxn], qus[maxq];
16 int lp, deln;
17 int tree_min[maxn << 2];
18 struct node_month
19 {
20     int num_del, num_leftdp;
21 }month[maxdp];
22 
23 void buildtree(int l, int r, int rt) {
24     if (l == r) {
25         tree_min[rt] = rm[l];
26         return;
27     }
28     int mid = (l + r) >> 1;
29     buildtree(ls);
30     buildtree(rs);
31     tree_min[rt] = min(tree_min[rt << 1], tree_min[rt << 1 | 1]);
32 }
33 void del_rm(int l, int r, int rt) {
34     if (l == r) {
35         lp -= tree_min[rt];
36         tree_min[rt] = inf;
37         return;
38     }
39     int mid = (l + r) >> 1;
40     if (lp >= tree_min[rt << 1]) {
41         del_rm(ls);
42     }
43     else del_rm(rs);
44     tree_min[rt] = min(tree_min[rt << 1], tree_min[rt << 1 | 1]);
45 }
46 
47 int main() {
48     scanf("%d%d", &n, &m);
49     for (int i = 1; i <= n; i++) {
50         scanf("%d", &rm[i]);
51     }
52     scanf("%d", &q);
53     for (int i = 1; i <= q; i++) {
54         scanf("%d", &qus[i]);
55         qus[0] = max(qus[0], qus[i]);
56     }
57     buildtree(1, n, 1);
58     for (int i = 1; i <= qus[0]; i++) {
59         lp += m;
60         while (lp>=tree_min[1])
61         {
62             del_rm(1, n, 1);
63             deln++;
64         }
65         month[i].num_leftdp = lp;
66         month[i].num_del = deln;
67     }
68     for (int i = 1; i <= q; i++) {
69         printf("%d %d
", month[qus[i]].num_del, month[qus[i]].num_leftdp);
70     }
71     return 0;
72 }
View Code

 



以上是关于计蒜客- Lpl and Energy-saving Lamps 线段树+点更新的主要内容,如果未能解决你的问题,请参考以下文章

计蒜客 Lpl and Energy-saving Lamps(二分+线段树区间最小)

H - Prince and Princess 计蒜客 - 42402

计蒜客 Red Black Tree(树形DP)

计蒜客 身高接近

计蒜客:求平均年龄Python方法

计蒜客:求平均年龄Python方法