[SDOI2009] HH necklace (Fenwick tree)

Subject to the effect

Description

HH string necklace from a variety of beautiful shells composed. HH believe different shells to bring good luck, so after every walk, he will be free to shell out a while, thinking the meaning they express. HH constantly collecting new shell, so he's getting longer necklace. One day, he suddenly raises the question: a certain period of shells, including how many different shells? This question is difficult to answer ...... because the necklace is too long. So he had to help you wise, to solve this problem.

Input

The first line: an integer N, the length of the necklace. Second line: N integers representing sequentially represents necklace shell (numbering is an integer between 0 and 1000000). Third row: an integer M, represents the number HH inquiry. Next M rows: each row two integers, L and R (1 ≤ L ≤ R ≤ N), representing the interval inquiry.

Output

M lines, each an integer, respectively for the corresponding answer inquiries.

Sample Input

6
1 2 3 4 3 5
3
1 2
3 5
2 6

Sample Output

2
2
4

HINT

Data for 20%, N ≤ 100, M ≤ 1000 ;
for 40% of data, N ≤ 3000, M ≤ 200000 ;
to 100% of the data, N ≤ 50000, M ≤ 200000 .

Report problem-solving

Read blog yellow seniors then push a bit to get it ...... thanks h seniors orz

Huang posted about seniors refined analysis:

 

 

#include<bits/stdc++.h>
using namespace std;
const int N = 1000500;
inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int n,m,mx;
int a[N],next[N],t[N],p[N];
struct data{
  int l,r,id,ans;
}q[N];
bool cmp1(data a,data b) {return a.l==b.l?a.r<b.r:a.l<b.l;}
bool cmp2(data a,data b) {return a.id < b.id;}
int lowbit(int x){return x&(-x);}

void modify(int x,int v)
{
    while(x<=n){
        t[x] += v;
        x +=lowbit (X); 
    } 
} 

int Query ( int X) 
{ 
    int SUM = 0 ;
     the while (X> 0 ) { 
        SUM + = T [X]; 
        X - = lowbit (X); 
    } 
    return SUM; 
} 

int main ( ) 
{ 
    n- = Read ();
     for ( int I = . 1 ; I <= n-; I ++ ) 
        a [I] = Read (), max = MX (MX, a [I]); // input of each position what type of pearl, type out the number 
    
    for ( int= n-I; I> 0 ; i--) // (** ** to back) 
        Next [I] = P [A [I]], P [A [I]] = I; // P [ under i] records the location of one color 
    
    for ( int I = . 1 ; I <= MX; I ++ )
         IF (P [i]) Modify (P [i], . 1 );   // each color with a first counting positions + 1'd 
    
    m = Read ();
     for ( int I = . 1 ; I <= m; I ++ ) 
        Q [I] .L = Read (), Q [I] .r = Read (), Q [ I] .id = I;   // offline reading interrogation 
        
    Sort (Q + . 1 , m + Q + . 1 , CMP1);  // all the left end points interrogation sorting 
    
    int L = . 1 ;
     for ( int I = . 1 ; I <= m; I ++ ) {
         the while (L < Q [I] .L) {  
             IF (Next [L]) Modify (Next [L], . 1 );   // Next described is not 0 appears more than once
                      // then counted in a position coupled with the second occurrence of 
            L ++ ; 
        } 
        Q [I] .ans = Query (Q [I] .r ) - Query (Q [I] .L - . 1 );   // prefix and 
    } 
    Sort (Q + . 1 , m + Q + . 1 , CMP2); //Reducing the interrogation sequence 
    for ( int I = . 1 ; I <= m; I ++) the printf ( " % D \ n- " , Q [I] .ans); // original sequence output 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/phemiku/p/11627599.html