HDU 5808 Price List Strike Back (cdq 分治)

Problem Description
There are  n  shops numbered with successive integers from  1  to  n  in Byteland. Every shop sells only one kind of goods, and the price of the  i -th shop's goods is  vi . The distance between the  i -th shop and Byteasar's home is  di .

Every day, Byteasar will purchase some goods. On the  i -th day, he will choose an interval  [li,ri]  and an upper limit  ci . Then, he will visit each shop with distance at most  ci  away from home in  [li,ri] , buy at most one piece of goods from each shop and go back home. Of course, he can also choose to buy nothing. Back home, Byteasar will calculate the total amount of money he has costed that day and write it down on his account book, denoted as  sumi .

However, due to Byteasar's poor math, he may calculate it wrong. 

Please write a program to help Byteasar judge whether each number is sure to be calculated wrong.
 

Input
The first line of the input contains an integer  T   (1T10) , denoting the number of test cases.

In each test case, the first line of the input contains two integers  n,m   (1n20000,1m100000) , denoting the number of shops and the number of records on Byteasar's account book.

The second line of the input contains  n  integers  v1,v2,...,vn   (1vi100) , denoting the price of the  i -th shop's goods.

The third line of the input contains  n  integers  d1,d2,...,dn   (1di109) , denoting the distance between the  i -th shop and Byteasar's home.

Each of the next  m  lines contains four integers  li,ri,ci,sumi   (1lirin,1ci109,1sumi100) , denoting a record on Byteasar's account book.
 

Output
For each test case, print a line with  m  characters. If the  i -th number is sure to be calculated wrong, then the  i -th character should be '1'. Otherwise, it should be '0'.
 

Sample Input
  
  
2 3 3 3 3 3 2 4 3 3 3 5 3 3 3 3 1 2 3 1 3 5 4 5 1 2 4 2 1 8 9 2 1 1 5 1 3 4 4 1 5 1 5 3 5 1 3 5 1
 

Sample Output
  
  
011

1101

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 10010
#define MOD 1000000007
const int mod = 998244353;

struct Node
{
    int l,r,c,sum,id;
} que[120000];

int ans[120000];
int f[22000][120];
int g[22000][120];
int v[22000];
int d[22000];

void cdq(int l,int r,int x,int y)
{
    int mid = (l+r)/2;

    vector<Node> R,L,M;
    //R.clear(),L.clear(),M.clear();

    for(int i=x; i<=y; i++)
    {
        if(que[i].l>mid) R.push_back(que[i]);
        else if(que[i].r<mid) L.push_back(que[i]);
        else M.push_back(que[i]);
    }

    if(M.size()>0)
    {
        for(int i=0;i<=100;i++) f[mid][i] = g[mid][i]=INF;

        f[mid][v[mid]]=d[mid];

        for(int i=mid-1;i>=l;--i)
        {
            for(int j=0;j<=100;++j)
            {
                if(j>=v[i])
                {
                    if(j-v[i]==0) f[i][j] = min(f[i+1][j],d[i]);
                    else f[i][j] = min(f[i+1][j],max(f[i+1][j-v[i]],d[i]));
                }
                else f[i][j] = f[i+1][j];
            }
        }

        for(int i=mid+1;i<=r;++i)
        {
            for(int j=0;j<=100;j++)
            {
                if(j>=v[i])
                {
                    if(j-v[i]==0) g[i][j] = min(g[i-1][j],d[i]);
                    else g[i][j] =min(g[i-1][j],max(g[i-1][j-v[i]],d[i]));
                }else g[i][j] = g[i-1][j];
            }
        }

        for(int i=0;i<M.size();i++)
        {
            if(M[i].r==mid)
            {
                if(f[M[i].l][M[i].sum] > M[i].c) ans[M[i].id]=1;
                else ans[M[i].id]=0;
                continue;
            }

            bool flag=false;

            for(int j=0;j<=M[i].sum;j++)
            {
                int dis;

                if(j==0) dis = g[M[i].r][M[i].sum];
                else if(j==M[i].sum) dis = f[M[i].l][M[i].sum];
                else dis = max(f[M[i].l][j],g[M[i].r][M[i].sum-j]);
                if(dis<=M[i].c)
                {
                    flag=true;
                    break;
                }
            }
            if(flag) ans[M[i].id]=0;
            else ans[M[i].id]=1;
        }


    }

    int cnt=x;
    if(L.size()>0)
    {
        for(int i=0; i<L.size(); i++)
        {
            que[cnt++] = L[i];
        }

        cdq(l,mid-1,x,cnt-1);
    }

    if(R.size()>0)
    {
        cnt++;
        x=cnt;
        for(int i=0;i<R.size();i++)
        {
            que[cnt++] = R[i];
        }
        cdq(mid+1,r,x,cnt-1);
    }
}

int n,m;

int main()
{
    int T;

    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);

        for(int i=1; i<=n; i++)
            scanf("%d",&v[i]);
        for(int i=1; i<=n; i++)
            scanf("%d",&d[i]);

        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d%d",&que[i].l,&que[i].r,&que[i].c,&que[i].sum);
            que[i].id=i;
        }

        cdq(1,n,1,m);

        for(int i=1; i<=m; i++)
            printf("%d",ans[i]);
        printf("\n");

    }

    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值