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

Source: Internet
Author: User

HDU 5172 GTY's gay friends (line segment tree), hdugty
Problem DescriptionGTY 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.
InputMulti 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. 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.
OutputFor each query, if there is a permutation [1. r −l + 1] In [L, r] , Print 'yes', else print 'no '.
Sample Input

8 52 1 3 4 5 2 3 11 31 12 24 81 53 21 1 11 11 2
 
Sample Output
YESNOYESYESYESYESNO
Question: give n numbers, m queries, and ask if the range [l, r] is from 1 to r-l + 1. The size can be easily obtained through the record prefix, but the key is deduplication. Considering the line segment tree practice, we record the position of the same element closest to the left of each point, and then find the maximum value of the entire interval (that is, the maximum precursor) if it is smaller than l, that is, the condition is met, output YES.
# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <string> # include <iostream> # include <queue> # include <cmath> # include <map> # include <stack> # include <bitset> using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) typedef long LL; typedef pair <int, int> p Il; const int MOD = 10000007; const int INF = 0x3f3f3f3f; const int maxn = 1000000 + 10; int mp [maxn]; int ans [maxn], pre [maxn]; LL s [maxn]; int num [maxn], sum [maxn <2]; int n, m; void pushup (int rs) {sum [rs] = max (sum [rs <1], sum [rs <1 | 1]);} void build (int rs, int l, int r) {sum [rs] = 0; if (l = r) return; int mid = (l + r)> 1; build (rs <1, l, mid); build (rs <1 | 1, mid + 1, r);} void update (int x, int c, int l, int r, int rs) {if (l = r ){ Sum [rs] = c; return;} int mid = (l + r)> 1; if (x <= mid) update (x, c, l, mid, rs <1); else update (x, c, mid + 1, r, rs <1 | 1); pushup (rs);} int query (int x, int y, int l, int r, int rs) {if (l> = x & r <= y) return sum [rs]; int mid = (l + r)> 1; int res = 0; if (x <= mid) res = max (res, query (x, y, l, mid, rs <1); if (y> mid) res = max (res, query (x, y, mid + 1, r, rs <1 | 1 )); pushup (rs); return res;} int main () {int x, y; while (~ Scanf ("% d", & n, & m) {s [0] = 0; CLEAR (mp, 0); CLEAR (ans, 0 ); REPF (I, 1, n) {scanf ("% d", & num [I]); s [I] = s [I-1] + num [I]; if (! Mp [num [I]) {pre [I] = 0; mp [num [I] = I ;} else {pre [I] = mp [num [I]; mp [num [I] = I;} update (I, pre [I], 1, n, 1);} while (m --) // The offline practice times out {scanf ("% d", & x, & y ); LL temp = (1 + y-x + 1) * (y-x + 1)/2; if (s [y]-s [x-1] = temp & query (x, y, 1, n, 1) <x) puts ("YES "); else puts ("NO") ;}} return 0 ;}


 

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.