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.

输入

Each input will consist of a single test case.

The first line contains integers $n$ and$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_nk1,k2,…,kn$
$(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 jj is the number of lamps in $j-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_qd1,d2,…,dq$
$(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 $1$; at the beginning of the first month Lpl buys the first m energy-saving lamps.

输出

Print $q$ lines.

Line $p$ 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_p$ month.

样例

输入

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

Note

Explanation for the sample:

In the first month, he bought $4$ energy-saving lamps and he replaced the first room in his list and remove it. And then he had $1$ energy-saving lamps and skipped all rooms next. So, the answer for the first month is $1,1———1$ room’s lamps were replaced already, $1$ energy-saving lamp remain.

题意

一共$m$个房间,小A$(暂且叫他小A)$每个月都要将房间的灯换成节能灯,但是小A换灯的时候有个习惯,必须要能够一次性将一个房间里的灯全部换掉才会去换掉这个房间的灯,否则就跳过这个房间去下一个房间重复这个步骤

每个月小A都会去买$m$个灯,然后一直换下去

现在每次询问都要输出两个值,当前已经换掉的房间与手中剩下的灯数

思路

直接暴力肯定会超时
直接套用线段树模板来维护区间最小值,每次找到最左端满足条件的房间然后将该房间的灯全部换掉

查找的时候可以直接二分找到最左端满足条件的房间

每次换掉一个房间以后该房间的灯的数量加$1e5+10$ ,因为正常情况下每个房间的灯的数量不会超过$1e4$,所以当所有房间的灯都超过$1e5+10$之后,也就代表了所有房间已经全部被换成了节能灯

二分查找的过程还可以优化一下,但是不优化也不会超时,懒得改了

线段树模板:线段树模板(含区间最大(小)值)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* =========================================================================
*
* Filename: G.cpp
*
* Link: https://nanti.jisuanke.com/t/30996
*
* Version: 1.0
* Created: 2018/09/01 14时55分32秒
* Revision: none
* Compiler: g++
*
* Author: 杜宁元 (https://duny31030.top/), duny31030@126.com
* Organization: QLU_浪在ACM
*
* =========================================================================
*/
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for(ll i=a;i<=n;i++)
#define pre(i,a,n) for(int i=a;i>=n;i--)
#define ll long long
#define max3(a,b,c) fmax(a,fmax(b,c))
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int inf = 1e5+10;
const int mod = 1e9 + 7;
const int MAXN = 1e6+100;
int have[MAXN],now[MAXN];
int n,m,q,que,haved;
int a[MAXN];
// a[]为原序列信息
int mn[MAXN*5];
// mn[]区间最小值

void PushUp(int rt)
{
mn[rt] = min(mn[rt<<1],mn[rt<<1|1]); // 区间最小值
}

void Build(int l,int r,int rt)
{
if(l == r)
{
mn[rt] = a[l];
return ;
}
int mid = (l+r)>>1;
Build(l,mid,rt<<1);
Build(mid+1,r,rt<<1|1);
PushUp(rt);
}

void Add(int L,int C,int l,int r,int rt)
{
if(l == r)
{
mn[rt] += C;
return ;
}
int mid = (l+r) >> 1;
if(L <= mid)
Add(L,C,l,mid,rt<<1);
else
Add(L,C,mid+1,r,rt<<1|1);
PushUp(rt);
}

int Query(int L,int R,int l,int r,int rt)
{
if(L <= l && r <= R)
{
return mn[rt];
}
int mid = (l+r)>>1;
int MIN = INF;
if(L <= mid)
{
MIN = min(Query(L,R,l,mid,rt<<1),MIN);
}
if(R > mid)
{
MIN = min(Query(L,R,mid+1,r,rt<<1|1),MIN);
}
return MIN;
}

int Find()
{
int l = 1,r = n;
while(l < r)
{
int mid = (l+r)/2;
int tmp = Query(l,mid,1,n,1);
if(tmp <= haved)
{
r = mid;
}
else
{
l = mid+1;
}
}
return l;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
scanf("%d %d",&n,&m);
rep(i,1,n)
{
scanf("%d",&a[i]);
}
Build(1,n,1);
haved = m;

int time = 0,ca = 1;
// time 换完的房间数量
// ca 时间计数(月数)
scanf("%d",&q);
while(Query(1,n,1,n,1) < inf)
{
int t = Find();
int tmp = Query(t,t,1,n,1);
while(tmp <= haved)
{
haved -= tmp;
// haved : 当前手中拥有的节能灯数量
time++;
Add(t,inf,1,n,1);
t = Find();
tmp = Query(t,t,1,n,1);
if(haved == 0)
break;
}

have[ca] = time;
now[ca++] = haved;
haved += m;
if(ca > 100000)
break;
}
for(ll i = ca;i <= 100000;i++)
{
have[i] = have[ca-1];
now[i] = now[ca-1];
}
rep(i,1,q)
{
scanf("%d",&que);
printf("%d %d\n",have[que],now[que]);
}
fclose(stdin);
// fclose(stdout);
return 0;
}