Poj-3368 frequent values ***

Source: Internet
Author: User
/* 480 Ms
* Poj-3368.cpp
* Created on: 2011-10-14
*
*
* Rmq:
* 10 3
-1-1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
*
* 1. value [I]: the value at the position I
* 2. For each value, record the location where the value is last, endpoint
* 3. Record the first value of each value: prepointvalue. In example, the first value of 1 is-1, that is, prepointvalue [1] =-1.
* 4. record the number of occurrences of each value, for example, frequency [1] = 4
*
* For each Q (x, y), find the number of occurrences of ANS = max {value [x]: A1, value [y]: A2, located in X, frequency between values Y and different from the values of value [X] and value [y]: A3}
* Pay attention to different situations. Some of the above three values may not be calculated, for example, Q (1, 2), A3 does not, and A1 = a2
* This should be discussed in detail
*
* In addition, if a3 is used for traversal, it can use sparse-table.
*
* Finally, because the value may be negative, maxn must be added to the most array table.
*/
# Include <cstdio>
# Include <cstring>
Using namespace STD;

Const int maxn = 100000 + 10;
Const int maxk = 20;

Int N, Q;
// As described above
Int value [maxn], endpoint [2 * maxn], frequency [2 * maxn], prepointvalue [2 * maxn];
// Used for sparse_table data
Int valuenum [2 * maxn], stfre [maxn], d [maxn] [maxk], num;

Inline int max (int lhs, int RHs ){
Return (stfre [LHS]> stfre [RHS]? LHS: RHs );
}

// St preprocessing
Void Prest (){
For (INT I = 0; I <num; I ++)
D [I] [0] = I;

For (Int J = 1; (1 <j) <= num; j ++)
For (INT I = 0; I + (1 <j)-1 <num; I ++)
D [I] [J] = max (d [I] [J-1], d [I + (1 <(J-1)] [J-1]);
}

// St
Int sparsetable (int x, int y ){
Int K;
For (k = 1; (1 <k) <= (Y-x + 1); k ++ );
K --;

If (stfre [d [x] [k] <stfre [d [Y-(1 <k) + 1] [k])
Return stfre [d [Y-(1 <k) + 1] [k];
Else
Return stfre [d [x] [k];
}



Int main (){
While (scanf ("% d", & N )){
If (n = 0) return 0;

Memset (frequency, 0, sizeof (frequency ));
Num = 0;

Scanf ("% d", & Q );

// Input
// The first value
Scanf ("% d", & value [0]);
Frequency [maxn + value [0] ++;
Valuenum [maxn + value [0] = num ++;

// 1-N value
For (INT I = 1; I <n; I ++ ){
Scanf ("% d", & value [I]);
// Enter a new value
If (frequency [maxn + value [I] = 0 ){
// Record the data of the previous value
Endpoint [maxn + value [I-1] = I-1;
Stfre [valuenum [maxn + value [I-1] = frequency [maxn + value [I-1];

// Record the data about the current new value
Prepointvalue [maxn + value [I] = value [I-1];
Valuenum [maxn + value [I] = num ++;
}
Frequency [maxn + value [I] ++;
}
// Record the last worthwhile data
Endpoint [maxn + value [n-1] = n-1;
Stfre [valuenum [maxn + value [n-1] = frequency [maxn + value [n-1];


// Rmq
Prest ();

Int X, Y;
For (INT I = 0; I <q; I ++ ){
Scanf ("% d", & X, & Y );

X --; y --; // note that the array starts from 0.
Int tmpmax =-1;
// Case 1
If (value [x] = value [y]) tmpmax = Y-x + 1;
// Other cases: for example, Q (1, 2)
Else {
Int xx = valuenum [maxn + value [endpoint [maxn + value [x] + 1];
Int YY = valuenum [maxn + prepointvalue [maxn + value [y];
// Case 2: for example, Q (1, 7) or Q (1, 10) // YY <XX such as Q (1, 3)
If (yy> = XX ){
Tmpmax = sparsetable (XX, YY );
}
// A1
Int xfre = endpoint [maxn + value [x]-x + 1;
// A2
Int yfre = Y-endpoint [maxn + prepointvalue [maxn + value [y];

If (xfre> tmpmax) tmpmax = xfre;
If (yfre> tmpmax) tmpmax = yfre;
}

Printf ("% d \ n", tmpmax );
}
}



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.