HDU 3450 Segment Tree + two points

Source: Internet
Author: User

Click to open link

Test instructions: Give a sequence of numbers, and ask you that the absolute value of the difference between the two adjacent number is greater than 2, and the 9901

Train of thought: see not at all, did not think is the topic of line tree, weak cry ~ ~ ~, see the solution of Daniel, is to know how the matter, for the current number A, then it is the last element can be composed of the situation is a-d to a+d and, can also think, A-d has formed the M kind of situation, Then in the case of less than D, you can directly put a into the a-d composition of the left and right sequence, then directly add can, and then update A can be composed of the situation, the data is too large also need to be discretized, and then two points to find the location of a-d and A+d, the data may not have these two locations, Then find the first is greater than or equal to a-d and the first position less than or equal to a+d update on the line, and will not affect the results, I write a data to see it will understand PS: Multi-school topic is very difficult, want to brush a set of their own part of the topic, and too difficult to give up directly (after all, too weak),

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < Algorithm>using namespace Std;typedef Long long ll;typedef unsigned long long ull;const int Inf=0x3f3f3f3f;const ll INF =0x3f3f3f3f3f3f3f3fll;const int maxn=100010;const int mod=9901;int num[maxn<<2],a[maxn],b[maxn];int n,m;int    lower_1 (int val,int k) {int le=0,ri=k;        while (ri-le>1) {int mid= (LE+RI) >>1;        if (b[mid]<=val) Le=mid;    else Ri=mid; } return le+1;}        void update (int pos,int add,int le,int ri,int node) {if (Le==ri) {num[node]+=add;        Num[node]%=mod;    return;    } int t= (LE+RI) >>1;    if (pos<=t) update (POS,ADD,LE,T,NODE&LT;&LT;1);    else update (POS,ADD,T+1,RI,NODE&LT;&LT;1|1); Num[node]= (num[node<<1]+num[node<<1|1])%mod;}    int query (int l,int r,int le,int ri,int node) {if (l<=le&&ri<=r) return Num[node];    int ans=0,t= (LE+RI) >>1; if (l<=t) ans+=query(l,r,le,t,node<<1);    if (r>t) ans+=query (l,r,t+1,ri,node<<1|1); return ans%mod;}        int main () {while (scanf ("%d%d", &n,&m)!=-1) {int k=1,ans=0;        memset (num,0,sizeof (num));            for (int i=0;i<n;i++) {scanf ("%d", &a[i]);        B[i]=a[i];        } sort (b,b+n);        for (int i=1;i<n;i++) {if (b[i]!=b[i-1]) b[k++]=b[i];            } for (int i=0;i<n;i++) {int le=lower_bound (b,b+k,a[i]-m)-b+1;            int ri=lower_1 (A[I]+M,K);            int Pos=lower_bound (b,b+k,a[i])-b+1;            int Ans1=query (le,ri,1,k,1);            Ans= (ans+ans1)%mod;        Update (pos,ans1+1,1,k,1);    } printf ("%d\n", ans); } return 0;}

HDU 3450 Segment Tree + two points

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.