HDOJ 2688 HDU 2688 Rotate ACM 2688 IN HDU
Posted on 2010-08-30 16:18 MiYu 閱讀(440) 評(píng)論(0) 編輯 收藏 引用 所屬分類: ACM ( 數(shù)據(jù)結(jié)構(gòu) ) 、ACM ( 樹狀數(shù)組 )MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=2688
題目描述:
Rotate
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 250 Accepted Submission(s): 68
Problem Description
Recently 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.
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.
Input
The 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.
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.
Output
Output just according to said.
Sample Input
5 1 2 3 4 5 3 Q R 1 3 Q
Sample Output
10 8
題目分析:
如果是暴力 , 沒一次更新都要重新計(jì)算的話, 時(shí)間上的開銷會(huì)非常大.
對(duì)數(shù)據(jù)進(jìn)行分析,可以看到, 當(dāng)旋轉(zhuǎn)的時(shí)候, 除了 第一個(gè)數(shù), 其他的數(shù)的對(duì)數(shù)的個(gè)數(shù)值都與這個(gè)數(shù)有關(guān), 因此, 只要開始
先把總的對(duì)數(shù) sum 算出來(lái), 再 根據(jù)旋轉(zhuǎn)時(shí) 每個(gè)數(shù)跟第一個(gè)數(shù)的大小 比較 ,對(duì)sum 進(jìn)行更新就可以了.
代碼如下:
代碼/*
MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
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 ){ // 修改
while ( x <= MAX ){
com[x] += val; x += low ( x );
}
}
int quy ( int x ){ // 查詢
int sum = 0;
while ( x > 0 ){
sum += com[x]; x ^= low ( x );
}
return sum ;
}
inline bool scan_d(int &num) // 輸入
{
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;
}
MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
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 ){ // 修改
while ( x <= MAX ){
com[x] += val; x += low ( x );
}
}
int quy ( int x ){ // 查詢
int sum = 0;
while ( x > 0 ){
sum += com[x]; x ^= low ( x );
}
return sum ;
}
inline bool scan_d(int &num) // 輸入
{
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;
}


