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 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 nnn 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 j 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≤q≤100000) — the number of queries.
The fourth line contains q 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

题意

城堡里有n个房间每个房间里有ki盏旧灯,Lpl每天购买m盏灯,他会按顺序,如果他的新灯>=房间的旧灯,他会拿新灯替换所有旧灯,直到不能替换,q个询问,每个询问输出两个数字分别为当前月有几个房间已经替换过了,还剩下几盏灯

题解

可以预处理出所有查询,每个查询循环1-n判断能否替换,显然是不行的

可以考虑每次找到可替换的最左边,显然可以用线段树维护区间最小值

代码

 #include<bits/stdc++.h>
using namespace std; const int maxn=1e5+;
int minn[maxn<<];
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&minn[rt]);
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
minn[rt]=min(minn[rt<<],minn[rt<<|]);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return minn[rt];
int mid=(l+r)>>,ans=1e9;
if(L<=mid)ans=min(ans,query(L,R,l,mid,rt<<));
if(R>mid)ans=min(ans,query(L,R,mid+,r,rt<<|));
return ans;
} int n,m,q;
int La,Fr,Left;
int f[maxn],r[maxn];
void update(int L,int R,int l,int r,int rt)
{
if(L>r||R<l)return;
if(L<=l&&r<=R&&minn[rt]>La)return;
if(l==r)
{
Fr++;
La-=minn[rt];
Left=l;
minn[rt]=0x3f3f3f3f;
return;
}
int mid=(l+r)>>;
if(L<=mid)update(L,R,l,mid,rt<<);
if(R>mid)update(L,R,mid+,r,rt<<|);
minn[rt]=min(minn[rt<<],minn[rt<<|]);
}
int main()
{
scanf("%d%d",&n,&m);
build(,n,); for(int i=;i<=;i++)
{
if(Fr==n)
{
f[i]=Fr;
r[i]=La;
continue;
}
La+=m;
Left=;
while(query(Left,n,,n,)<=La)update(Left,n,,n,);
f[i]=Fr;
r[i]=La;
} scanf("%d",&q);
for(int i=,x;i<q;i++)
{
scanf("%d",&x);
printf("%d %d\n",f[x],r[x]);
}
return ;
}

ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps(二分+线段树区间最小)的更多相关文章

  1. ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(线段树)

    题目链接:https://nanti.jisuanke.com/t/30996 中文题目: 在喝茶的过程中,公主,除其他外,问为什么这样一个善良可爱的龙在城堡里被监禁Lpl?龙神秘地笑了笑,回答说这是 ...

  2. ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)

    https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...

  3. ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps (弱线段树)

    线段树节点维护区间最小值,查找时优先从左侧的区间寻找. 每一次循环都在树中不停寻找第一个小于等于当前持有数的值,然后抹去,直到找不到为止. #include<cstdio> #includ ...

  4. 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)

    G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K   During tea-drinking, princess, amongst other t ...

  5. ACM-ICPC 2018 南京赛区网络预赛 I Skr (马拉车+hash去重)或(回文树)

    https://nanti.jisuanke.com/t/30998 题意 给一串由0..9组成的数字字符串,求所有不同回文串的权值和.比如说“1121”这个串中有“1”,“2”,“11”,“121” ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  7. ACM-ICPC 2018 南京赛区网络预赛(12/12)

    ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem 计算\(\sum_{i=1}^{n-1}i\cdot i!(MOD\ n)\) \(\sum_{i ...

  8. ACM-ICPC 2018 南京赛区网络预赛 J.sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  9. ACM-ICPC 2018 南京赛区网络预赛 E题

    ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...

随机推荐

  1. 机器学习进阶-图像基本操作-图像数据读取 1.cv2.imread(图片读入) 2.cv2.imshow(图片展示) 3.cv2.waitKey(图片停留的时间) 4.cv2.destroyAllWindows(清除所有的方框界面) 5.cv2.imwrite(对图片进行保存)

    1. cv2.imread('cat.jpg', cv2.IMGREAD_GRAYSCALE)  # 使用imread读入图像(BGR顺序), 使用IMGREAD_GRAYSCALE 使得读入的图片为 ...

  2. struts2默认临时文件更改

    struts的文件上传mutifile会有一个临时文件地址,如果需要使用自己指定临时文件地址需要在struts.xml中设置以下内容. <constant name="struts.m ...

  3. 子类中的成员函数覆盖父类(name hiding)

    只要子类中出现了和父类中同名的函数,父类中的所有这个名字的函数,就被屏蔽了. 静态函数成员也是如此?经过代码验证,确实如此. #include <iostream> using names ...

  4. 回溯法 leetcode题解 Combination Sum 递归法

    题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...

  5. OC copy mutableCopy, 浅拷贝,深拷贝

    copy与mutableCopy都是深拷贝,区别是mutableCopy拷贝出的对象是可变的. OC对象基本都是通过指针访问,所以一般情况下,通过对指针的赋值都是浅拷贝,即只是拷贝了一份对象的指针,对 ...

  6. 尚硅谷springboot学习15-日志框架1-入门

    引子 小张:开发一个大型系统 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? ​ 2.框架来记录系统的一些运行时信息:日志框架 : ...

  7. C# 模拟鼠标操作

    [Flags] enum MouseEventFlag : uint //设置鼠标动作的键值 { Move = 0x0001, //发生移动 LeftDown = 0x0002, //鼠标按下左键 L ...

  8. Linux磁盘空间不足处理方法

    维护Linux服务器正常使用需要经常删除Linux系统运行产生的系统日志和业务环境产生的debug日志文件.安装包等.本文主要描述如何通过脚本实现清理业务环境产生的 debug日志文件和上传或备份的打 ...

  9. 解决eclipse+adt出现的 loading data for android 问题

    因为公司最近做的项目中有用到一些第三方demo,蛋疼的是这些demo还比较旧...eclipse的... 于是给自己的eclipse装上了ADT插件,但是...因为我的eclipse比较新,Versi ...

  10. 关于那个.get .post .ajax ztree 还有后台servlet传递数据

    servlet给前台传递data串 用的方法是 PrintWriter out = response.getWriter(); // response.sendRedirect("test. ...