HDU 5172 GTY & amp; #39; s gay friends (line segment tree)

Source: Internet
Author: User

HDU 5172 GTY & #39; s gay friends (line segment tree)

Address: HDU 5172

During the competition, the water of the Line Segment tree that maintains the Range Sum, the range product, and the maximum value of the range passed .. After the game, the data will be changed back to 10 ^ 6, and then it will be TLE ..

The positive solution is the interval and the prefix and maintenance. Then, maintain a occurrence location on the number. If the previous occurrence location of each number in each query is smaller than l, it indicates that there are no duplicates, if the range and conform to the sum in the full arrangement, it indicates that it must be a full arrangement.

The Code is as follows:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include #include 
     
      #include 
      #include 
       
        #include 
        
         using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1int a[1000002], pre[1000002], pos[1000002];int Max[4000000];LL sum[1000002];void PushUp(int rt){ Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);}void build(int l, int r, int rt){ if(l==r){ Max[rt]=pre[l]; return ; } int mid=l+r>>1; build(lson); build(rson); PushUp(rt);}int Query(int ll, int rr, int l, int r, int rt){ if(ll<=l&&rr>=r){ return Max[rt]; } int mid=l+r>>1, ans=-1; if(ll<=mid) ans=max(ans,Query(ll,rr,lson)); if(rr>mid) ans=max(ans,Query(ll,rr,rson)); return ans;}int main(){ int n, m, i, j, x, l, r, ans; while(scanf("%d%d",&n,&m)!=EOF){ memset(pos,-1,sizeof(pos)); sum[0]=0; for(i=1;i<=n;i++){ scanf("%d",&x); pre[i]=pos[x]; pos[x]=i; sum[i]=sum[i-1]+x; } build(root); while(m--){ scanf("%d%d",&l,&r); if(sum[r]-sum[l-1]!=(LL)(r-l+1)*(r-l+2)/2){ puts("NO"); continue ; } ans=Query(l,r,root); if(ans
         
          

Related Article

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.