2018南京網路賽G題(線段樹+最先符合條件查詢)
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
題目來源
題目大意:
有n個房間,每個房間有 a【i】個燈泡要換,你每個月會得到m個燈泡,進行替換,替換的規則是,從左到右依次判斷,如果你有的燈泡大於房間的燈泡,就全部換,否則小於就不換,遍歷下一個,可以一次換多個燈泡(數量充足的條件下),沒用完的燈泡可以留到下一個月用。
解題思路:
房間數有 1e5,詢問次數也要1e5,,由於詢問非常容易重複,所以我們直接按天數處理,處理到一個最大值,這個最大值是詢問當中的最大值,這樣我們就能做到O(1)的查詢。
然後很明顯,這個題我們需要先找到替換的燈泡,進行替換,然後多餘的留給下一月,再對下一個月進行判斷。
普通的暴力寫法就是for迴圈從左到右,大於這個房間燈泡數就換,這樣寫起來倒是簡單,但是肯定超時。
所以我們找到燈泡的位置不能直接遍歷,要換一個方法。
由於是相互比較,找到最左邊的那個符合要求的值,我們很容易就想到了線段樹,維護一下區間的最小值,對於每一個節點,對他的左右節點節點進行判斷,如果左邊節點的最小值小於我們剩餘的燈泡,就往左邊找,如果不是,對右邊進行同樣的判斷。
這一點,我記得線段樹基礎練習裡面有一道相似的題目。
但是光找一個位置還是不行啊,有可能你有的燈泡數太多,要替換很多房間,那麼就要查詢多次。
就比如說全部是 1 1 1 1 1 1 1....................
你有很多的燈泡,這時候點查詢就比較費時了。
所以我們多維護一個值,區間的和,如果區間和小於我們有的燈泡數,那我們就直接把區間的燈泡選了。
另外,選一個區間的燈泡之後,我們需要加一個標記,標記很簡單,就是表示區間置為0的意思,不短的下推。
下面是程式碼:
#include <iostream>
#include <cstdio>
#include<map>
#include <cstring>
#include<vector>
#include <string>
#include<queue>
#include<map>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<set>
#define mem(a,val) memset(a,val,sizeof a)
#define lef rt<<1
#define rig rt<<1|1
#define fori(l,r) for( int i = l ; i <= r ; i++ )
#define forj(l,r) for( int j = l ; j <= r ; j++ )
#define fork(l,r) for( int k = l ; k <= r ; k++ )
#define mid (l+r)>>1
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 1e5+6;
int n,m;
int cnt,remain;
int q[maxn];
int add[maxn<<2];
struct spe
{
int mi,sum;
int l,r;
};
struct spe2
{
int remain,cnt;
};
spe2 ans[maxn];
spe tree[maxn<<2];
int item;
void build( int l,int r,int rt )
{
tree[rt].l = l;
tree[rt].r = r;
if( l == r )
{
scanf("%d",&item);
tree[rt].mi = item;
tree[rt].sum = item;
return;
}
int m = mid;
build(l,m,lef);
build(m+1,r,rig);
tree[rt].mi = min(tree[lef].mi,tree[rig].mi);
tree[rt].sum = tree[lef].sum+tree[rig].sum;
}
void pushdown( int rt )
{
if( add[rt] )
{
add[rt] = 0;
tree[lef].mi = inf;
tree[rig].mi = inf;
tree[lef].sum = inf;
tree[rig].sum = inf;
add[lef] = 1;
add[rig] = 1;
}
}
void change( int rt )
{
tree[rt].mi = min(tree[lef].mi,tree[rig].mi);
if( tree[lef].sum == inf || tree[rig].sum == inf )
tree[rt].sum = inf;
else tree[rt].sum = tree[lef].sum+tree[rig].sum;
}
void query( int l,int r,int rt,int val )
{
if( tree[rt].sum <= val )
{
tree[rt].mi = inf;
remain -= tree[rt].sum;
cnt += tree[rt].r-tree[rt].l+1;
tree[rt].sum = inf;
add[rt] = 1;
return;
}
int m = mid;
pushdown(rt);
if( tree[lef].mi <= val )
query(l,m,lef,val);
else if( tree[rig].mi <= val )
query(m+1,r,rig,val);
change(rt);
}
int main()
{
while( scanf("%d %d",&n,&m) == 2 )
{
build(1,n,1);
int k;
scanf("%d",&k);
int ma = 0;
fori(1,k)
{
scanf("%d",&q[i]);
ma = max(ma,q[i]);
}
remain = 0;
cnt = 0;
fori(1,ma)
{
if( cnt == n )
{
ans[i].cnt = n;
ans[i].remain = ans[i-1].remain;
continue;
}
remain += m;
while( remain > 0 )
{
if( tree[1].mi > remain )
break;
query(1,n,1,remain);
}
ans[i].cnt = cnt;
ans[i].remain = remain;
}
/*
fori(1,ma)
{
printf("%d %d\n",ans[ i ].cnt,ans[ i ].remain);
}
*/
fori(1,k)
{
printf("%d %d\n",ans[ q[i] ].cnt,ans[ q[i] ].remain);
}
}
return 0;
}
/*
5 4
3 10 5 2 7
10
5 1 4 8 7 2 3 6 4 7
*/