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_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 jjis 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_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 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_pdp​ 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 南京赛区网络预赛

用num[i]表示第i天修好的房间的数目, ans[i]表示第i天剩下的灯的个数

建线段树存的是区间灯数最小的

每次查询 查询的是可以被修完的并且是越往前越好的房间

预处理 从第1天开始到最后一天

如果发现了可以修好的房间 就更新这个点 用inf的灯数量表示这个房间除去


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std; typedef long long LL; const int maxn = 1e5+10;
int n, m, q;
LL lamp[maxn << 2], tree[maxn << 2], sum, num[maxn], ans[maxn]; void pushup(int rt)
{
tree[rt] = min(tree[rt<<1], tree[rt<<1|1]);
} void build(int rt, int l, int r)
{
if(l == r){
tree[rt] = lamp[l];
return;
}
int m = (l + r) / 2;
build(rt<<1, l, m);
build(rt<<1|1, m + 1, r);
pushup(rt);
} void update(int x, int val, int l, int r, int rt)
{
if(l == r){
tree[rt] = val;
return;
}
int m = (l + r) / 2;
if(x <= m){
update(x, val, l, m, rt << 1);
}
else{
update(x, val, m + 1, r, rt<<1|1);
}
pushup(rt);
} int query(int rt, int l, int r, int x)
{
if(l == r){
if(sum >= tree[rt]){
num[x]++;
ans[x] = sum - tree[rt];
sum -= tree[rt];
update(l, 10000000000ll, 1, n, 1);
return l;
}
else if(num[x] == 0){
num[x] = num[x - 1];
ans[x] = sum;
}
return 0;
}
int m = (l + r) / 2;
if(tree[rt << 1] <= sum){
return query(rt<<1, l, m, x);
}
else{
return query(rt<<1|1, m + 1, r, x);
}
} int main()
{
while(scanf("%d%d", &n, &m) != EOF){
for(int i = 1; i <= n; i++){
scanf("%lld", &lamp[i]);
}
build(1, 1, n);
sum = m;
for(int i = 1; i <= maxn; i++){
if(num[i - 1] == n){
num[i] = num[i - 1];
ans[i] = ans[i - 1];
continue;
}
bool flag = 0;
while(query(1, 1, n, i) > 0)
flag = 1;
if(flag){
num[i] += num[i - 1];
}
sum += m;
}
scanf("%d", &q);
while(q--){
int x;
scanf("%d", &x);
printf("%lld %lld\n", num[x], ans[x]);
}
}
return 0;
}

南京网络赛G-Lpl and Energy【线段树】的更多相关文章

  1. 2019南昌网络赛-I(单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...

  2. 2019 ICPC南京网络预选赛 I Washing clothes 李超线段树

    题意:有n个人,每个人有一件衣服需要洗,可以自己手洗花费t时间,也可以用洗衣机洗,但是洗衣机只有一台,即每个时刻最多只能有·一个人用洗衣机洗衣服.现在给你每个人最早可以开始洗衣服的时间,问当洗衣机的洗 ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)

    题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...

  4. 2019CCPC网络赛——array(权值线段树)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6703 题目大意: 给出一个n(n<1e5)个元素的数组A,A中所有元素都是不重复的[1,n]. 有 ...

  5. hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】

    https://hihocoder.com/problemset/problem/1586 线段树操作,原来题并不难..... 当时忽略了一个重要问题,就是ax*ay要最小时,x.y可以相等,那就简单 ...

  6. 2019南昌网络赛-I. Yukino With Subinterval 线段树套树状数组,CDQ分治

    TMD...这题卡内存卡的真优秀... 所以以后还是别用主席树的写法...不然怎么死的都不知道... 树套树中,主席树方法开权值线段树...会造成空间的浪费...这道题内存卡的很紧... 由于树套树已 ...

  7. ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 i题 Minimum(线段树)

    描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...

  8. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

  9. 2018ICPC南京网络赛

    2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \ ...

  10. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. 【转】WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  2. linux -- Ubuntu下安装和配置Apache2

    在Ubuntu中安装apache 安装指令:sudo apt-get install apache2 启动和停止apache的文件是:/etc/init.d/apache2 启动命令:sudo apa ...

  3. 转载:QTableView中嵌入可视化组件

    出处:http://qimo601.iteye.com/blog/1538364 QTableView中嵌入可视化组件方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简 ...

  4. erlang二进制的难理解的地方,有点神奇

    40> <<A:16>> = <<1,2>>.<<1,2>>41> <<B:16/bits>> ...

  5. opengl wglsharelists

    原文地址:http://blog.csdn.net/webscaler/article/details/5873179 OpenGL 中用到多线程和多 render context 渲染的时候会用到 ...

  6. sql 一些题目

    这道SQL笔试题你会怎么写(转) 最近面试了一些Senior BI的候选人,行业经验三年到七年不等,起初觉得这个Level的无需准备笔试题,碍于领导执念,就在真实项目中提取5道SQL题目,这里仅单说其 ...

  7. html5实现刮刮卡效果

    通过Canvas实现的可刮涂层效果. 修改img.src时涂层也会自动适应新图片的尺寸. 修改layer函数可更改涂层样式. 涂层: 可刮效果: 以下是HTML源代码(已增加移动设备支持): 1 2 ...

  8. 【转自IT虾米网:www.itxm.net】外部应用和drools-wb6.1集成解决方案

    一.手把手教你集成外部应用和drools workbench6.1 1.         首先按照官方文档安装workbench ,我用的是最完整版的jbpm6-console的平台系统,里面既包含j ...

  9. mysql数据库中,查看某个数据库下的表的存储类型都有哪些

    需求描述: 在备份数据库的时候,使用mysqldump进行数据库的备份,如果库中仅仅有innodb存储引擎, 那么使用--single-transaction就可以,如果还有其他的存储引擎类型就要使用 ...

  10. mysql在命令行中,指定要连接的数据库?

    需求描述: mysql客户端,可以在登录到mysql数据库时,指定要连接到哪个数据库 这里进行一个测试. 测试过程: 1.mysql通过-D参数指定连接到test数据库 [mysql@redhat6 ...