1. 程式人生 > >2018南京網路賽 G+Lpl and Energy-saving Lamps 線段樹預處理

2018南京網路賽 G+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(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,...,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)(1≤kj​≤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(1≤q≤100000) — 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)(1≤dp​≤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個房間每個房間都有一定數量的普通燈泡ai,給你一個每個月將會購入的節能燈的數量m,從房間的編號從小到大依次遍歷,如果當前節能燈數大於等於的當前房間的普通燈泡數,那麼當前房間的普通燈泡都會換成節能燈,然後繼續遍歷下一個房間,如果遍歷了所有房間還有燈泡剩餘,就留到下個月,當所有房間的燈泡都配換了後就不在繼續買燈泡了,q次詢問,問你n個月後有多少房間以及被全部換上節能燈了,已經還剩了多少節能燈。

思路:q次詢問k個月有多少房間和節能燈,q和k都只有1e5很容易就想到了先預處理出來再O(1)的詢問,關鍵是怎麼預處理,O(n*n)的想都別想了,我最開始的先排序了在二分找可以換的房間,但是又必須先換標號靠前的那個,好像不怎麼好找,暴力的話一定有拉回了O(n*n)的複雜度,突然靈光一現,線段樹似乎可以分段考慮,線段樹每一個節點表示當前區間已經換了的房間的個數cnt,以及除去換了過後的剩下房間的最小的普通燈泡個數mi,每次更新返回為嘗試更新這段區間後可以剩下的節能燈的數量,如果當前剩餘節能燈的數量小於mi直接返回剩餘節能燈的數量,大於的話就去更新他。具體看程式碼。

程式碼:

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
const ll linf=8e18+9e17;
const int mod=1e9+7;
const double e=exp(1.0);
const double pi=acos(-1);
const double eps=1e-6;
int ans[maxn][2];
struct tree{
	tree(){}
	tree(int l,int r,int mi,int cnt)
	{
		this->l=l;
		this->r=r;
		this->mi=mi;
		this->cnt=cnt;
	}
	int l,r,mi,cnt;
}t[maxn<<2];
void pushup(int rt)
{
	t[rt].mi=min(t[rt<<1].mi,t[rt<<1|1].mi);
	t[rt].cnt=t[rt<<1].cnt+t[rt<<1|1].cnt;
}
void build(int l,int r,int rt)
{
	t[rt].l=l;
	t[rt].r=r;
	t[rt].cnt=0;
	if(l==r)
	{
		scanf("%d",&t[rt].mi);
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	pushup(rt);
}
int updata(int rt,int data)
{
	int temp;
	if(t[rt].mi>data)return data;
	if(t[rt].l==t[rt].r){
		if(t[rt].mi<=data){
			t[rt].cnt=1;
			temp=t[rt].mi;
			t[rt].mi=inf;
			return data-temp;
		}
	}
	temp=updata(rt<<1,data);
	temp=updata(rt<<1|1,temp);
	pushup(rt);
	return temp;
}
int main()
{
	int n,m,q;
	scanf("%d%d",&n,&m);
	build(1,n,1);
	scanf("%d",&q);
	int last=0;
	for(int i=1;i<=100000;i++)
	{		
		if(t[1].cnt<n){
		last+=m;
		last=updata(1,last);}
		ans[i][0]=t[1].cnt;
		ans[i][1]=last;
	}
	while(q--)
	{
		int id;
		scanf("%d",&id);
		printf("%d %d\n",ans[id][0],ans[id][1]);
	}
	return 0;
 }