GTY ' s gay friends Problem description
Gty have n base friends, out of some kind of bad taste, gty every morning will let his friends line up, each base friend has a characteristic value, that the base friend has more majestic or Niang cannon, you, as Gty Assistant, must answer gty each inquiry, gty each will ask an interval[L,r][l,R] is an arrangement of 11 to r-l+1r−l+1.
Enter a description
Multiple sets of data (approximately 3 groups), with two numbers for the first row of each group of dataN,m (1 \leq n,m \leq 100000)N,M(1≤N,M≤100000) indicates the number of initial base friends and the number of queries, and the second line containsNN NumberA_i (1 \leq a_i \leq N)A?I??(1≤A?I??≤nm< Span class= "katex-html" >< Span class= "Mord mathit" >m line two numbers per line l,rl< Span class= "mpunct" >,r indicates an inquiry [l,r][l,r is an arrangement.
Output description
For each query, if it is an arrangement, output "YES", otherwise output "NO"
Input sample
8 52 1 3 4 5 2 3 11 31 12 24 81 53 21 1 11 11 2
Output sample
Yesnoyesyesyesyesno
If the interval (l,r) is an arrangement, then two conditions must be met 1. The sum of the number in the interval is equal to the sum of len* (len+1)/2 natural number 2. There are no duplicates in the interval the first condition is prefixed and judged, the second condition uses the pre array to judge, The pre represents the last occurrence of the AI position if the largest pre in this interval is less than L, then there must be no duplicate number in this interval, and conversely there must be a number of repetitions using the segment tree to seek RMQ, O (n), query O (logn)
#include <iostream>#include<cstring>#include<cstdio>using namespaceStd;typedefLong LongLL;Const intmax=1e6+Ten; LL Sum[max];intmaxv[max*4];intMap[max];intPre[max];//Tag DeliveryvoidPushup (introot) {Maxv[root]=max (maxv[root<<1],maxv[root<<1|1]);}//Create a line segment treevoidBuild (intLintRintroot) { if(l==r) maxv[root]=Pre[l]; Else { intMid= (l+r) >>1; Build (L,mid,root<<1); Build (Mid+1,r,root<<1|1); Pushup (root); }}//EnquiryintQuery (intQlintQrintLintRintroot) { if(QL<=L&&QR>=R)returnMaxv[root]; intMid= (l+r) >>1; intans=-1; if(Ql<=mid) Ans=max (Ans,query (ql,qr,l,mid,root<<1)); if(Qr>mid) Ans=max (Ans,query (ql,qr,mid+1,r,root<<1|1)); returnans;}intMain () {intN,m,x,l,r; while(~SCANF ("%d%d",&n,&m) {memset (map,-1,sizeof(map)); for(intI=1; i<=n;i++) {scanf ("%d",&x); I==1? sum[i]=x:sum[i]=sum[i-1]+x; Pre[i]=Map[x]; MAP[X]=i; } Build (1N1); while(m--) {scanf ("%d%d",&l,&R); LL Len=r-l+1; if(sum[r]-sum[l-1]!=len* (len+1)/2) {puts ("NO"); Continue; } if(Query (L,r,1N1) <l) puts ("YES"); ElsePuts"NO"); } } return 0;}
View Code
Hdu 5172 GTY's gay friends segment tree