Miyu original, post Please note: Reprinted from __________ White House
Question address:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2688
Description:
Rotate
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 250 accepted submission (s): 68
Problem descriptionrecently yifenfei face such a problem that give you millions of positive integers, tell how many pairs I and j that satisfy f [I] smaller than F [J] strictly when I is smaller than J strictly. I and J is the serial number in the interger sequence. of course, the problem is not over, the initial interger sequence will change all the time. changing format is like this [s e] (ABS (E-S) <= 1000) that mean between the S and E of the sequece will rotate one times.
For example initial sequence is 1 2 3 4 5.
If changing format is [1 3], than the sequence will be 1 3 4 2 5 because the first sequence is base from 0.
Inputthe input contains multiple test cases.
Each case first given a integer n standing the length of integer sequence (2 <=n <= 3000000)
Second a line with N integers standing f [I] (0 <F [I] <= 10000)
Third a line with one integer m (M <10000)
Than M lines quiry, first give the type of quiry. A character C, if C is 'R' than give the changing format, if C equal to 'Q', just put the numbers of satisfy pairs.
Outputoutput just according to said.
Sample Input
51 2 3 4 53qr 1 3q
Sample output
108
Question Analysis:
In the case of brute force attacks, the time overhead will be very high if no update is required to be recalculated.
After analyzing the data, we can see that when rotating, except for the first number, the logarithm of other numbers is related to this number. Therefore
Calculate the total logarithm sum, and then update the sum based on the size of each number during rotation compared with the first number.
CodeAs follows:
Code /*
Miyu original, post Please note: Reprinted from __________ White House
Http://www.cnblog.com/MiYu
Author by: miyu
Test: 1
Program: 2688
*/
# Include < Iostream >
# Include < Algorithm >
Using Namespace STD;
Const Int Max = 10000 ;
Int Ncount = 0 , N, m, X, Y;
Int COM [Max + 1 ], Num [ 300 * Max + 1 ];
Long Long Sum = 0 ;
Char Ask [ 5 ];
Inline Int Low ( Int X ){
Return X & ( - X );
}
Void Modify ( Int X, Int Val ){ // Modify
While (X <= Max ){
COM [x] + = Val; x + = Low (X );
}
}
Int QUY ( Int X ){ // Query
Int Sum = 0 ;
While (X > 0 ){
Sum + = COM [X]; x ^ = Low (X );
}
Return SUM;
}
Inline Bool Scan_d ( Int & Num) // Input
{
Char In ; Bool ISN = False ;
In = Getchar ();
If ( In = EOF) Return False ;
While ( In ! = ' - ' && ( In < ' 0 ' | In > ' 9 ' )) In = Getchar ();
If ( In = ' - ' ) {Isn = True ; Num = 0 ;}
Else Num = In - ' 0 ' ;
While ( In = Getchar (), In > = ' 0 ' && In <= ' 9 ' ){
Num * = 10 , Num + = In - ' 0 ' ;
}
If (ISN) num =- Num;
Return True ;
}
Int Main ()
{
While (Scan_d (N )){
Memset (COM, 0 , Sizeof (COM); sum = 0 ;
For ( Int I = 0 ; I < N; ++ I ){
Scan_d (Num [I]); Modify (Num [I], 1 );
Sum + = QUY (Num [I] - 1 );
}
Scan_d (m );
While (M -- ){
Scanf ( " % S " , Ask ); Int Temp;
Switch (Ask [ 0 ]) {
Case ' Q ' : Cout < Sum < Endl; Break ;
Case ' R ' : Scan_d (x), scan_d (y); temp = Num [x];
While (X < Y) {num [x] = Num [x + 1 ];
Num [x] > Temp ? Sum -- : Num [x] = Temp ? : Sum ++ ; X ++ ;
}
Num [y] = Temp; Break ;
}
}
}
Return 0 ;
}