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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

 

题目大意:

有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

*/

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值