# 计蒜客 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 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 m

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

The second line contains n

The third line contains one integer q (1 \le q \le 100000)

The fourth line contains q

Months are numbered starting with 1

Print q

Line p

### Hint

Explanation for the sample:

In the first month, he bought 4

#### 样例输入

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 南京赛区网络预赛

1 //G-线段树
2 #include<iostream>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<bitset>
7 #include<cassert>
8 #include<cctype>
9 #include<cmath>
10 #include<cstdlib>
11 #include<ctime>
12 #include<deque>
13 #include<iomanip>
14 #include<list>
15 #include<map>
16 #include<queue>
17 #include<set>
18 #include<stack>
19 #include<vector>
20 using namespace std;
21 typedef long long ll;
22
23 const double PI=acos(-1.0);
24 const double eps=1e-6;
25 const ll mod=1e9+7;
26 const int inf=0x3f3f3f3f;
27 const int maxn=1e5+10;
28 const int maxm=100+10;
29 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
30 #define lson l,m,rt<<1
31 #define rson m+1,r,rt<<1|1
32
33 int tree[maxn<<2];
34
35 void pushup(int rt)
36 {
37     tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);
38 }
39
40 void update(int p,int val,int l,int r,int rt)
41 {
42     if(l==r){
43         tree[rt]=val;
44         return ;
45     }
46
47     int m=(l+r)>>1;
48     if(p<=m) update(p,val,lson);
49     else     update(p,val,rson);
50     pushup(rt);
51 }
52
53 int query(int val,int l,int r,int rt)
54 {
55     if(l==r){
56         return l;
57     }
58
59     int m=(l+r)>>1;
60     if(tree[rt<<1]  <=val) return query(val,lson);//尽量往左找
61     if(tree[rt<<1|1]<=val) return query(val,rson);
62     return -1;
63 }
64
65 int a[maxn],op[maxn],ans[maxn],over[maxn];
66
67 int main()
68 {
69     int n,k;
70     scanf("%d%d",&n,&k);
71     ll sum=0;
72     for(int i=1;i<=n;i++){
73         scanf("%d",&a[i]);
74         sum+=a[i];
75         update(i,a[i],1,n,1);
76     }
77     int m=sum/k+(sum%k==0?0:1);//实际上最多需要买灯泡的月份
78     int q,mx=-1;
79     scanf("%d",&q);
80     for(int i=1;i<=q;i++){
81         scanf("%d",&op[i]);
82         mx=max(mx,op[i]);//查询里最多需要的月份
83     }
84     ll ret=0;
85     int cnt=0;
86     for(int i=1;i<=min(mx,m);i++){//取最小即可
87         ret+=k;
88         while(1){
89             int pos=query(ret,1,n,1);
90             if(pos!=-1){
91                 cnt++;ret-=a[pos];
92                 update(pos,inf,1,n,1);
93                 ans[i]=cnt;over[i]=ret;
94             }
95             else{
96                 ans[i]=cnt;over[i]=ret;
97                 break;
98             }
99         }
100     }
101     for(int i=1;i<=q;i++){
102         if(op[i]<=m)
103             printf("%d %d\n",ans[op[i]],over[op[i]]);
104         else
105             printf("%d %d\n",ans[m],over[m]);//如果查询的月份比实际换完需要的月份大，直接输出换完时的数据
106     }
107 }

posted @ 2018-09-13 17:06  ZERO-  阅读(293)  评论(0编辑  收藏  举报