Reservoir——The 2016 ACM - ICPC Asia Nha Trang Regional Contest

A big reservoir was built on Red River using a dam. Assume that the reservoir is a rectangular box with unit length width. The reservoir consists of many tanks. An example a cross section of an empty reservoir along its length and height dimensions is shown in the picture below:
在这里插入图片描述
Water flows in from the top left gate into the reservoir. The tanks in the reservoir are constructed using walls. Each wall is one unit thick (along the width dimension) and is shorter than the height of the reservoir.

Given the locations and the heights of the walls and the unit volume K of water flowing in, your task is to figure out the last wall water flows over.

Input

The input consists of several datasets. The first line of the input contains the number of datasets, which is a positive number and is not greater than 20. The following lines describe the datasets.

Each dataset is described by the following lines:

The first line contains one positive integers N – the number of walls separating the tanks (N≤105)

The second line contains N positive integers Li – the horizontal location (along the length dimension of the reservoir) of the ith wall (1≤Li≤109,Li>Li−1+1 for i>1).

The third line contains N positive integers Hi – the height in unit length of the ith wall (1≤Hi≤105).

The fourth line contains an integer Q – the number of queries (1≤Q≤105).

In the next Q lines, each line contains a positive integer K that is the unit volume of water flowing in the reservoir (1≤K≤1015).

Output

For each dataset, output Q lines where the ith contains the index of the last wall that water flows over for the ith query. If there is no wall that water flows over, output 0.

Explanation for the Sample Dataset
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Sample Input 1

1
4
1 3 5 8
2 5 3 1
3
3
13
17

Sample Output 1

1
1
3

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int INF=0xfffffff;
const int maxn=100001;
const long double PI=acos(-1);
ll wall[maxn],dith[maxn];
ll work[maxn];
unordered_map<int,int> in;
ll root[maxn];
ll all[maxn];
int main()
{
    ll T;
    scanf("%lld",&T);
    while(T--)
    {
        in.clear();
        ll n;
        scanf("%lld",&n);
        for(ll i=1; i<=n; i++)
            scanf("%lld",&wall[i]);
        root[1]=0;
        all[0]=0;
        for(ll i=1; i<=n; i++)
        {
            scanf("%lld",&dith[i]);
            all[i]=all[i-1]+dith[i];
            if(i>1)
            {
                if(dith[i-1]>dith[i])
                    root[i]=i-1;
                else
                {
                    ll p=i-1;
                    while(dith[p]<=dith[i]&&p>0)
                        p=root[p];
                    root[i]=p;
                }
            }
        }
        for(ll i=1; i<=n; i++)
        {
            if(root[i]==0)
            {
                work[i]=wall[i]*dith[i];
                work[i]-=all[i-1];
            }
            else
            {
                work[i]=dith[i]*(wall[i]-wall[root[i]]-1)+work[root[i]];
                work[i]-=all[i-1]-all[root[i]];
            }
        }
        ll q,k;
        scanf("%lld",&q);
        for(ll i=0;i<q;i++)
        {
            scanf("%lld",&k);
            ll j;
            j=lower_bound(work+1,work+n+1,k)-work-1;
            printf("%lld\n",j);
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值