GTY's gay friends HDU

GTY has nn gay friends. To manage them conveniently, every morning he ordered all his gay friends to stand in a line. Every gay friend has a characteristic value aiai , to express how manly or how girlish he is. You, as GTY's assistant, have to answer GTY's queries. In each of GTY's queries, GTY will give you a range [l,r][l,r] . Because of GTY's strange hobbies, he wants there is a permutation [1..rl+1][1..r−l+1] in [l,r][l,r]. You need to let him know if there is such a permutation or not.

InputMulti test cases (about 3) . The first line contains two integers n and m ( 1n,m10000001≤n,m≤1000000 ), indicating the number of GTY's gay friends and the number of GTY's queries. the second line contains n numbers seperated by spaces. The ithith number aiai ( 1ain1≤ai≤n ) indicates GTY's ithith gay friend's characteristic value. The next m lines describe GTY's queries. In each line there are two numbers l and r seperated by spaces ( 1lrn1≤l≤r≤n ), indicating the query range.OutputFor each query, if there is a permutation [1..rl+1][1..r−l+1] in [l,r][l,r], print 'YES', else print 'NO'.Sample Input

8 5
2 1 3 4 5 2 3 1
1 3
1 1
2 2
4 8
1 5
3 2
1 1 1
1 1
1 2

Sample Output

YES
NO
YES
YES
YES
YES
NO

题意 给出一个数列,询问连续的从l开始到r为止的数是否刚好能够组成从1开始到r-l+1的数列

第一个判断用前缀和求和,然后就是查重
用线段树维护每一个数它上一次出现的地方,
如果这个区间里面所有的数上一次出现的最大值小于了a 那就是符合条件的了
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-6
14 #define  fi first
15 #define  se second
16 #define  rtl   rt<<1
17 #define  rtr   rt<<1|1
18 #define  bug         printf("******
")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  name2str(x) #x
21 #define  fuck(x)     cout<<#x" = "<<x<<endl
22 #define  f(a)        a*a
23 #define  sf(n)       scanf("%d", &n)
24 #define  sff(a,b)    scanf("%d %d", &a, &b)
25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
27 #define  pf          printf
28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
32 #define  FIN         freopen("in.txt","r",stdin)
33 #define  gcd(a,b)    __gcd(a,b)
34 #define  lowbit(x)   x&-x
35 using namespace std;
36 typedef long long  LL;
37 typedef unsigned long long ULL;
38 const int mod = 1e9 + 7;
39 const int maxn = 1e6 + 10;
40 const int INF = 0x3f3f3f3f;
41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
42 int n, m, x, vis[maxn];
43 LL sum[maxn];
44 struct node {
45     int l, r, num;
46     int mid() {
47         return ( l + r ) >> 1;
48     }
49 } tree[maxn << 2];
50 void pushup ( int rt ) {
51     tree[rt].num = max ( tree[rtl].num, tree[rtr].num );
52 }
53 void build ( int l, int r, int rt ) {
54     tree[rt].l = l, tree[rt].r = r;
55     if ( l == r ) {
56         tree[rt].num = 0;
57         return ;
58     }
59     int m = ( l + r ) >> 1;
60     build ( l, m, rtl );
61     build ( m + 1, r, rtr );
62 }
63 void update ( int pos, int key, int rt ) {
64     if (  tree[rt].l == tree[rt].r ) {
65         tree[rt].num += key;
66         return ;
67     }
68     int m = tree[rt].mid();
69     if ( pos <= m ) update ( pos, key, rtl );
70     else update ( pos, key, rtr );
71     pushup ( rt );
72 }
73 int query ( int L, int R, int rt ) {
74     if ( L <= tree[rt].l && tree[rt].r <= R ) return tree[rt].num;
75     int m = tree[rt].mid();
76     if ( L > m ) return query ( L, R, rtr );
77     else if ( R <= m ) return query ( L, R, rtl );
78     else return max ( query ( L, m, rtl ), query ( m + 1, R, rtr ) );
79 }
80 int main() {
81     while ( ~sff ( n, m ) ) {
82         mem ( vis, 0 );
83         build ( 1, n, 1 );
84         for ( int i = 1 ; i <= n ; i++ ) {
85             sf ( x );
86             sum[i] = sum[i - 1] + x;
87             if ( vis[x] ) update ( i, vis[x], 1 );
88             vis[x] = i;
89         }
90         while ( m-- ) {
91             int a, b;
92             sff ( a, b );
93             //  fuck(sum[b] - sum[a - 1]),fuck(( b - a + 1 ) * ( b - a + 2 ) / 2)
94             if ( sum[b] - sum[a - 1] == ( b - a + 1 ) * ( b - a + 2 ) / 2 && query ( a, b, 1 ) < a ) printf ( "YES
" );
95             else printf ( "NO
" );
96         }
97     }
98     return 0;
99 }
 
原文地址:https://www.cnblogs.com/qldabiaoge/p/9768874.html