Hdu5172 --- GTY's gay friends, hdu5172 --- gtygay
Problem Description
GTY has n 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 ai, 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]. because of GTY's strange hobbies, he wants there is a permutation [1 .. r−l + 1] in [l, r]. you need to let him know if there is such a permutation or not.
Input
Multi test cases (about 3 ). the first line contains two integers n and m (1 ≤ 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 ith number ai (1 ≤ ai ≤ n) indicates GTY's ith 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 (1 ≤ l ≤ r ≤ n), indicating the query range.
Output
For each query, if there is a permutation [1. r −l + 1] in [l, r], print 'yes', else print 'no '.
Sample Input
8 5 2 1 3 4 5 2 1 1 3 1 1 2 2 4 8 1 3 2 1 1 1 1 1 2
Sample Output
YES NO
Source
BestCoder Round #29
Recommend
Hujie | We have carefully selected several similar problems for you: 5173 5169 5168 5165 5164
If a range is [1 ,... , R-l + 1], first, range and yes
X * (x + 1)/2,-> prefix and Processing
Next, each number is different. Preprocessing the position pre of the last occurrence of each number, and then finding the maximum value of pre in the interval, if the maximum pre is smaller than the left endpoint and the range is x * (x + 1)/2, YES is output; otherwise, NO is output. here we can use the line segment tree to solve this problem.
Note that when judging, it is best to first judge the range and. If this is not enough, do not query the line segment tree. Otherwise, it is easy to cause the TLE
/*************************************** * *********************************> File Name: hdu5172.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstri Ng> # include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; const int N = 1000010; int pre [N]; LL sum [N]; int _ hash [N]; struct node {int l, r; int val;} tree [N <2]; void build (int p, int l, int r) {tree [p]. l = l; tree [p]. r = r; if (l = r) {tree [p]. val = pre [l]; return ;} Int mid = (l + r)> 1; build (p <1, l, mid); build (p <1 | 1, mid + 1, r ); tree [p]. val = max (tree [p <1]. val, tree [p <1 | 1]. val);} int query (int p, int l, int r) {if (tree [p]. l = l & tree [p]. r = r) {return tree [p]. val;} int mid = (tree [p]. l + tree [p]. r)> 1; if (r <= mid) {return query (p <1, l, r);} else if (l> mid) {return query (p <1 | 1, l, r);} else {return max (que Ry (p <1, l, mid), query (p <1 | 1, mid + 1, r) ;}} int main () {int n, m; while (~ Scanf ("% d", & n, & m) {memset (_ hash,-1, sizeof (_ hash); memset (sum, 0, sizeof (sum); int x, l, r; for (int I = 1; I <= n; ++ I) {scanf ("% d ", & x); pre [I] = _ hash [x]; _ hash [x] = I; sum [I] = sum [I-1] + (LL) x;} build (1, 1, n); while (m --) {scanf ("% d", & l, & r); LL len = (LL) (r-l + 1); len = (len + 1) * len/2; if (len! = Sum [r]-sum [l-1]) {printf ("NO \ n"); continue;} int res = query (1, l, r ); if (res <l) {printf ("YES \ n") ;}else {printf ("NO \ n") ;}} return 0 ;}