Hdu 3450 tree array optimized dp and hdu3450

Source: Internet
Author: User

Hdu 3450 tree array optimized dp and hdu3450


I won't talk about the meaning of the question; hdu2227 is almost just that this question is not incremental, but the adjacent difference is not greater than d. In fact, they are the same and there are also differences;

This question does not contain a range and the size relationship between trees has an impact on the results. Therefore, the original id is used in the tree array. Due to the large number of values, the discretization feeling here is similar to the ing, discretization is used to find IDs;

When the id of num [I] is x, dp [I] indicates that the number of solutions is obvious dp [I] = sun (dp [j ], j <I & abs (num [j]-num [I]) <= d) here in the tree array, sum [1 to num [I] + d]-sum [1 to num [I]-d] is found by the num ing relationship num [I] + The id of d and num [I]-d (it is nice to find the two points written by myself) update it again.

#include<stdio.h>#include<algorithm>#include<string.h>#include<iostream>using namespace std;#define mod 9901int num1[100010],num2[100010],mark[100010],tt,n;int coun[100010];int update(int a,int b){    for(int i=a;i<=n;i+=(i&-i))    {        coun[i]+=b;        coun[i]%=mod;    }    return 0;}int find(int a){    int s=0;    for(int i=a;i>=1;i-=(i&-i))    {        s+=coun[i];        s%=mod;    }    return s;}int searchr(int a){    int left=1,right=tt,now;    while(left<=right)    {        int mid=(left+right)/2;        if(mark[mid]<=a)            {               now=mid;            left=mid+1;        }        else         {            right=mid-1;        }    }        return now;}int searchl(int a){    int left=1,right=tt;    while(left<right)    {         int mid=(left+right)/2;        if(mark[mid]<a) left=mid+1;        else if(mark[mid]==a) return mid;        else right=mid;    }    return left;}int main(){    int i,j,d;    while(~scanf("%d%d",&n,&d))    {        for(i=1;i<=n;i++)        {            scanf("%d",&num1[i]);            num2[i]=num1[i];        }        sort(num2+1,num2+1+n);        tt=0;                 num2[0]=-1;        for(i=1;i<=n;i++)        {            if(num2[i]!=num2[i-1])            {                tt++;                mark[tt]=num2[i];            }        }        memset(coun,0,sizeof(coun));        int sum=0;        int x=searchl(num1[1]);        for(i=1;i<=n;i++)        {            int left=searchl(num1[i]-d)-1;            int right=searchr(num1[i]+d);            int now=searchl(num1[i]);            int s=find(right)-find(left);            s=(s%mod+mod)%mod;            sum+=s;            sum%=mod;            update(now,s+1);        }        printf("%d\n",sum);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.