Poj 3264 Frequent values

Source: Internet
Author: User

Frequent valuesTime Limit: 2000 MS Memory Limit: 65536 K DescriptionYou are given a sequence of n integers a1, a2 ,..., an in non-decreasing order. in addition to that, you are given several queries consisting of indices I and j (1 ≤ I ≤ j ≤ n ). for each query, determine the most frequent value among the integers ai ,..., aj. inputThe input consists of several test cases. each test case starts with a line containing two integers n and q (1 ≤n, q ≤100000 ). the next line contains n integers a1 ,..., an (-100000 ≤ ai ≤ 100000, for each I ε {1 ,..., n}) separated by spaces. you can assume that for each I ε {1 ,..., n-1}: ai ≤ ai + 1. the following q lines contain one query each, consisting of two integers I and j (1 ≤ I ≤ j ≤ n), which indicate the boundary indices for the query. the last test case is followed by a line containing a single 0. outputFor each query, print one line with one integer: The number of occurrences of the most frequent value within the given range. sample Input10 3-1-1 1 1 1 1 3 10 10 102 31 105 100 Sample Output143SourceUlm Local 2007

/***** Question: give a series of non-descending sequences, and then ask the maximum length of the same consecutive numbers in a certain interval. ~~~~ Line Segment tree ~~~~ * ***/# Include <map> # include <set> # include <list> # include <queue> # include <stack> # include <cmath> # include <ctime> # include <vector> # include <bitset> # include <cstdio> # include <string> # include <numeric> # include <cstring> # include <cstdlib> # include <iostream> # include <algorithm> # include <functional> using namespace std; typedef long ll; typedef unsigned long ull; int dx [4] = {- 1, 1, 0, 0}; int dy [4] = {0,-1, 1 }; // up down left right # define eps 1e-8 # define inf 0x7fffffff # define debug puts ("BUG") # define lson l, m, rt <1 # define rson m + 1, r, rt <1 | 1 # define read freopen ("in.txt", "r", stdin) # define write freopen ("in.txt", "w", stdout) # define Mem (a, x) memset (a, x, sizeof ()) # define maxn 100010/****** idea: we do not need to create a line segment tree for the given range, which will increase the complexity of the Line Segment tree (there will be many variables ). We use the frequency of numbers to create a line segment tree. Because it is a non-descending sequence, as long as the same number is stored only once, no two numbers are the same. In addition, the original string can be changed to: the first digit has x1, and the second digit has x2 .... The number num contains xnum. The new string we get is used to create a line segment tree. The maintained value of the Line Segment tree is the maximum value of a certain range. In this case, the interval value of the Line Segment tree changes to the number of l and the number of r. We need to store the original range information, and store the starting position of each digit in order to convert the queried range endpoint into the range value of the Line Segment tree. We can get not only the new string, but also the range endpoint information and process it together. During the query, we need to convert the queried range value to: the position l is located between the numbers in the l0, and the position r is located between the numbers in the r0, this requires binary search. If not, the line segment tree is equivalent to a white write and has not been tried =! Note that the position of l and r during the query will result in continuous truncation of the number of l0, and the same is true for r. Therefore, you need to take out the content of the two parts of the truncation, and then query the maximum value from l0 + 1 to the r0-1 range, so that the answer is the maximum value of the three numbers. However, it may occur that the interval is composed of two truncated parts. At this time, the query of the Line Segment tree is meaningless. You can judge it first or slightly process the line segment tree. * ***/Int len [maxn <2]; // int Max [maxn <2]; // line segment tree, from the nth digit to the nth digit, the longest consecutive length of the digit int ls [maxn <2]; // the starting position of the nth digit int num, x; // num is the number of different numbers, and x is the counter void init () used for the initialization of the line tree {// initialize num = 1; x = 1; mem (len, 0); Mem (Max, 0); Mem (ls, 0);} void PushUp (int rt) {// line segment tree update Max [rt] = max (Max [rt <1], Max [rt <1 | 1]);} void build (int l, int r, int rt) {// create a line segment tree if (l = r) {Max [rt] = len [x ++]; return ;} int m = (l + r)> 1; build (lso N); build (rson); PushUp (rt);} int query (int L, int R, int l, int r, int rt) {// query if (L> R) // return 0 may occur in this case; if (L <= l & r <= R) return Max [rt]; int m = (l + r)> 1; int ans = 0; if (L <= m) ans = max (ans, query (L, R, lson )); if (R> m) ans = max (ans, query (L, R, rson); return ans;} int bisect (int k) // Binary Search {if (k> = ls [num]) // return num for the last number; int l = 1, r = num; while (l <= r) {int m = (l + r)> 1; if (k> = ls [m] & k <ls [m + 1]) // locate the condition r Eturn m; else if (k> ls [m] & k> ls [m + 1]) l = m + 1; else if (k> ls [m] & k = ls [m + 1]) // returns m + 1; else r = m ;}} int main () {// read; int n, m; while (~ Scanf ("% d", & n) {init (); scanf ("% d", & m); int x0; scanf ("% d ", & x0); // input ls [num] = 1; len [num] = 1; for (int I = 2; I <= n; I ++) {int x1; scanf ("% d", & x1); if (x1! = X0) // determine whether the {num ++; ls [num] = I; // storage location} len [num] ++; // update the length x0 = x1; // pave the way for the next value} ls [num + 1] = n + 1; // This build is useless (1, num, 1 ); // create a for (int I = 1; I <= m; I ++) {int l, r, l0, r0; int Max1 = 0, Max2 = 0, max3 = 0, ans = 0; scanf ("% d", & l, & r); l0 = bisect (l), r0 = bisect (r ); // binary location Max1 = ls [l0 + 1]-l, Max2 = r-ls [r0] + 1; // calculate the length of the two truncation segments if (l0 = r0) // in this case, you do not need to calculate {printf ("% d \ n ", r-l + 1); continue;} else if (r0 = l0 + 1) // This is also {ans = max (Max1, Max2 ); printf ("% d \ n", ans); continue;} Max3 = query (l0 + 1, r0-1, 1, num, 1 ); // maximum ans = max (Max3, Max2), ans = max (Max1, ans); printf ("% d \ n", ans );}} 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.