ACM-ICPC 2018 南京賽區網路預賽 __G Lpl and Energy-saving Lamps【線段樹+模擬】
- 1000ms
- 65536K
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 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 k1,k2,...,kn
(1≤kj≤10000,j=1,2,...,n) — the number of lamps in the rooms of the Castle. The number in position jjis 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≤q≤100000) — the number of queries.
The fourth line contains qq integers d1,d2,...,dq
(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.
Output
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 dp month.
Hint
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.
樣例輸入
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
題目來源
題解:線段樹基礎,求數列中第一個小於等於某個數的數。
AC的C語言程式碼:
#include<stdio.h>
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define N 100010
#define INF 0x3f3f3f3f
int a[N];//點資訊
int p[N<<2];//區間資訊
int d[N];
struct ANS{
int n1,n2;//分別表示替換完成的房間數以及剩餘的燈泡數
}ans[N];
int min(int a,int b)
{
return a<b?a:b;
}
void PushUp(int rt)
{
p[rt]=min(p[rt<<1],p[rt<<1|1]);
}
//建樹
void Build(int l,int r,int rt)
{
if(l==r){
p[rt]=a[l];
return;
}
int m=(l+r)>>1;
Build(ls);
Build(rs);
PushUp(rt);
}
//點修改 A[L]=C
void Update(int L,int C,int l,int r,int rt)
{
if(l==r){
p[rt]=C;
return;
}
int m=(l+r)>>1;
if(L<=m)
Update(L,C,l,m,rt<<1);
else
Update(L,C,m+1,r,rt<<1|1);
PushUp(rt);
}
//區間查詢 查詢區間[l,r]內第一個小於等於k的數
int Query(int k,int l,int r,int rt)
{
if(p[rt]>k)//如果 [l,r]內所有的數都大於k則不存在 返回0
return 0;
if(l==r)
return l;
int m=(l+r)>>1;
if(p[rt<<1]<=k)//如果在[l,m]區間內
return Query(k,ls);
else if(p[rt<<1|1]<=k)//如果在[m+1,r]區間內
return Query(k,rs);
}
int main()
{
int n,m,q,x,i,pos;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
Build(1,n,1);//建樹
scanf("%d",&q);
int MAX=0;
for(i=1;i<=q;i++){
scanf("%d",&d[i]);
if(d[i]>MAX)
MAX=d[i];
}
int n1=0,n2=0;//分別表示替換完成的房間數以及剩餘的燈泡數
for(i=1;i<=MAX;i++){//模擬MAX個月
if(n1<n)
n2+=m;//如果還沒有更換完,每個月要新增m個節能燈
while(pos=Query(n2,1,n,1)){//如果存在小於n2的數
n2-=a[pos];
//修改pos處的值為INF
Update(pos,INF,1,n,1);
n1++;//更換的房間數加一
}
ans[i].n1=n1;
ans[i].n2=n2;
}
for(i=1;i<=q;i++)
printf("%d %d\n",ans[d[i]].n1,ans[d[i]].n2);
return 0;
}
相關文章
- ACM-ICPC 2018 南京賽區網路預賽__B The writing on the wall【列舉】ACM
- ACM-ICPC 2018 徐州賽區網路預賽ACM
- ACM-ICPC 2018 瀋陽賽區網路預賽ACM
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- ACM-ICPC 2018 徐州賽區網路預賽 F. Features TrackACM
- ACM-ICPC 2018 南京賽區網路預賽__K The Great Nim Game【博弈論+費馬小定理+DP】ACMGAM
- ACM-ICPC 2018 南京賽區網路預賽__J. Sum【尤拉篩法+質因子分解+思維】ACM
- ACM-ICPC 2018 南京賽區網路預賽__L. Magical Girl Haze 【Dijkstra演算法+分層圖思想】ACM演算法
- ACM-ICPC 2018 徐州賽區網路預賽 I. Characters with Hash【簽到題】ACM
- 2018 瀋陽賽區網路預賽 I.Lattice's basics in digital electronics(模擬)Git
- 聯賽模擬測試8 Dash Speed 線段樹分治
- 2018 徐州網路賽 G 題解
- 2018icpc 南京網路賽L Magical Girl Haze
- 2023北航校賽-K線段樹
- 一些線段樹典(求求了區域賽遇到線段樹不要被卡了)
- 【某NOIP模擬賽T2 - 旅遊】--線段樹最佳化 DP 的魅力
- 10.6 模擬賽(NOIP 模擬賽 #9)
- 2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)佇列
- 2018 ICPC南京區域賽題解 更新至 8 題
- 模擬賽
- [題解]NOIP2018模擬賽 plutotree
- 5.4 模擬賽
- 2024.2.18 模擬賽
- 2024.1.26 模擬賽
- 2024.1.23 模擬賽
- 2024.3.30 模擬賽
- 2024.3.17 模擬賽
- 2024.2.25 模擬賽
- 11.4 模擬賽
- 20241016 模擬賽
- 模擬賽 2
- 2024.10.16 模擬賽
- 10.18 模擬賽
- 10.12 模擬賽
- 1002模擬賽
- 10.7 模擬賽
- 1114模擬賽
- 11.11模擬賽