ACM-ICPC 2018 南京賽區網路預賽 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
題目來源
程式碼高亮 Sublime Vim Emacs
環境配色亮色配色暗色配色 暗色配色
返回
- 亮色配色
- 暗色配色
程式碼縮排248 4
返回
- 2
- 4
- 8
C 語言C++ 語言 (C++11)Java 語言 C++ 語言 (C++11)
返回
- C 語言
- C++ 語言 (C++11)
- Java 語言
通用
邏輯
迴圈
數學
文字
列表
顏色
變數
函式
79
ll powmod(ll x,ll y){ll t;for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod;return t;}
59
while(scanf("%d%d",&n,&m)!=EOF)
60
{
61
build(1,n,1);///順便寫入陣列
62
int tv=0,cnt=0;///tm表示最後一次修改的位置
63
for(int i=1;i<maxn;i++)
64
{
65
tv+=m;
66
while(~query(1,n,1,tv)) cnt++;
67
ans1[i]=cnt,ans2[i]=tv;///已經換的次數,剩餘的價值
68
///cout<<ans1[i]<<" "<<ans2[i]<<endl;
69
}
70
scanf("%d",&q);
71
for(int i=0;i<q;i++)
72
{
73
scanf("%d",&x);
74
printf("%d %d\n",ans1[x],ans2[x]);
75
}
76
}
77
return 0;
78
}
79
關閉終端 終端 - 計蒜客
已通過 0 組測試資料,共 5 組
只看題面
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define root l,r,rt
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
const int maxn =1e5+5;
const int mod=1e9+7;
const int UB=maxn*1000;
ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); }
ll powmod(ll x,ll y){ll t;for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod;return t;}
/*
題目大意:換燈泡,每個月都會增加m個價值,
然後每個房間都有對應的價值,必須按序掃,如果手中的價值大於等於房間的價值則必須換。
詢問給定月數末尾總共換了多少次,手中剩餘多少價值。
線段樹操作秀一下就行。
查詢就查詢序列中第一個小於等於給定值的位置,二分一下,
如果左邊區間的最小值符合要求則查詢左邊,否則查詢右邊,
邊界時,判定一下是否符合題意,然後更新就行,否則返回-1表示不可行。
線段樹的基礎題。
順便想提下線段樹的資料對應關係,線段樹的資料來源應該是由樹的
葉節點儲存,然後節點價值是由rt索引,minv[rt]儲存的,是子樹中資料來源的函式關係。
該函式滿足區間性質。
*/
int n,m;
///線段樹
int minv[maxn<<2];
void pushup(lrt) { minv[rt]=min(minv[rt<<1],minv[rt<<1|1]); }
void build(lrt)
{
minv[rt]=UB;///最大值
if(l==r) { scanf("%d",&minv[rt]); return ; }
int mid=l+r>>1;
build(lson),build(rson),pushup(root);
}
int query(lrt,int& v)///整體查詢第一個小於等於v的位置
{
if(l==r)
{
if(v<minv[rt]) return -1;
else
{
v-=minv[rt];
minv[rt]=UB;///這個點就不會再被選中了
return rt;
}
}
int mid=l+r>>1,ret;
if(minv[rt<<1]<=v) ret=query(lson,v);
else ret=query(rson,v);
pushup(root);///因為查詢時參雜著修改,所以每次都要重新整理
return ret;
}
///
int x,q;
int ans1[maxn],ans2[maxn];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);///順便寫入陣列
int tv=0,cnt=0;///tm表示最後一次修改的位置
for(int i=1;i<maxn;i++)
{
tv+=m;
while(~query(1,n,1,tv)) cnt++;
ans1[i]=cnt,ans2[i]=tv;///已經換的次數,剩餘的價值
///cout<<ans1[i]<<" "<<ans2[i]<<endl;
}
scanf("%d",&q);
for(int i=0;i<q;i++)
{
scanf("%d",&x);
printf("%d %d\n",ans1[x],ans2[x]);
}
}
return 0;
}