DTOJ # 3194. Go to the moon

Description [title]

After $ Scape $ and $ Mythological $ exchange experiences sprint play geometric, $ Mythological $ very happy and recommend a game for $ Scape $ $ To the moon $.

After the game, an elderly man $ John $ memories are dusty drugs, were released dusty ceremony, $ Scape $ into his memory.

$ John $ remembered a Huanzuo $ River $ girl's figure, with countless paper rabbit, there is a sandbag, a platypus doll, a lighthouse. $ Scape $ was deeply impressed.

"My platypus and sandbags where is it?" $ Scape $ this thought, can not help but dream up $ Mythological $ decided to give him a gift to send $ n $, the first of which $ i $ species is part of $ a_i $. These gifts lined up in order.

She selected a gift very special way, every time she would choose the same two species present, and between which they do not yet satisfy the gift away gifts, and take away this gift.

Now Scape given several times asked Always ask if he gave her a range of $ [L_i, R_i] between $ gift, so she can take up to how much a gift, ask mandatory online.

[Input Format]

$ 1 $ the first row, three integers $ n, m, q $ is the size of each set of test data which, $ $ a_i maximum possible value, the number of interrogation.

Second row $ 2 $, $ $ n-integers, the first integer represents $ I $ $ a_i $.

Next $ Q $ lines of two integers $ L_i, R_i $, $ Las provided on an answer to the inquiry of $ (initial $ 0 $), the inquiry interval $ [L_i ^ \ land las, R_i ^ \ land las] $ (legal guarantee).

[Output format]

A total of $ q $ line, represents the answer to each inquiry.

[Sample]

Sample Input 
Sample Input 1
. 9 10. 3
. 1. 1. 1. 1. 3 2 2. 3. 1
2. 9
. 1 2
2. 5
Sample Input 2
10. 3 10
. 1. 4. 5. 6. 7. 3 2. 9 10. 8
. 1. 9
2. 3
. 4. 5 

Sample output 
sample output 1
. 8
2
0
sample output 2
0
0
0

[Note] with the data range

For $ 20 \% $ data, $ 1 \ leq n \ leq 10 ^ 3,1 \ leq q \ leq 10 ^ 3 $.

For $ 80 \% $ data, $ 1 \ leq n \ leq 10 ^ 5,1 \ leq q \ leq 10 ^ 5 $.

For $ 100 \% $ data, $ 1 \ leq n \ leq 10 ^ 5,1 \ leq m \ leq 10 ^ 5,1 \ leq q \ leq 2 \ times 10 ^ 6 $.

【answer】

First, there is a nature, from left to right can be deleted directly delete the section. It is not difficult to prove that a certain optimal.

So we can sweep from left to right again for each inquiry, with a stack can be maintained. Efficiency $ O (n ^ 2) $.

Consider optimization. We found that the starting point for a range, the starting point must belong to a block can be deleted, and the starting point of the backward and forward part of the part should be the same. Such rearward section of the process of popping can be understood as a push process of the preceding paragraph.

So a number of points that can be deleted interval is equivalent to twice the number of push. Count the number of bad push, we do not consider part of the deletion. We stack with a $ Trie $ tree maintenance, maintenance, son of each node with $ map $.

Starting from the root, if the new node to the current node same color, perform popped operation, jump back to the parent node. If the color is different, jumped down along the edge color. So that each section can be deleted have been compressed to a point in the.

On tree $ Trie $, each distance between $ l-1 $ and $ $ R & lt node corresponding to the inquiry is not deleted portion. Jump up part is symmetrical parts, and the jump is just part of it much later.

Just because the distance between two points, with $ RMQ $ maintenance can be.

[Code]

#include<bits/stdc++.h>
namespace IO
{
    char C[1 << 25], D[1 << 25], *S = C;
    int T = 0;
    inline void init (void) { fread (C, 1, 1 << 25, stdin); }
    inline int read (void)
    {
        int x = 0; char c;
        do c = *S++; while ( c < '0' or c > '9' );
        do x = ( x << 1 ) + ( x << 3 ) + c - '0', c = *S++; while ( c >= '0' and c <= '9' );
        return x;
    }
    inline void write (int x)
    {
        if ( !x ) { D[T++] = '0'; D[T++] = '\n'; return; }
        char st[30]; int tp = 0;
        while ( x ) st[++tp] = x % 10 + 48, x /= 10;
        while ( tp ) D[T++] = st[tp--];
        D[T++] = '\n';
    }
    inline void flush (void) { fwrite (D, 1, T, stdout); }
}
const int maxn=200000+10;
int st[maxn][19],fst[maxn],fa[maxn],tot,dep[maxn],Log[maxn],a[maxn],n,m,q,A[maxn];
std::unordered_map<int,int> ch[maxn];
using namespace IO;
std::vector<int> E[maxn];
inline void dfs ( int u )
{
    st[fst[u]=++tot][0]=dep[u];
    for ( int v:E[u] ) dfs(v),st[++tot][0]=dep[u];
}
inline int lca ( int u,int v )
{
    int ql=fst[u],qr=fst[v];
    if ( ql>qr ) std::swap(ql,qr);
    int k=Log[qr-ql+1];
    return std::min(st[ql][k],st[qr-(1<<k)+1][k]);
}
signed main()
{
    init();n=read();m=read();q=read();
    int u=0;
    for ( int i=1;i<=n;i++ )
    {
        a[i]=read()+1;
        if ( a[i]==a[u] ) u=fa[u];
        else
        {
            if ( !ch[u].count(a[i]) ) ch[u][a[i]]=i,E[u].push_back(i),fa[i]=u,dep[i]=dep[u]+1;
            u=ch[u][a[i]];
        }
        A[i]=u;
    }
    dfs(0);
    for ( int i=2;i<=tot;i++ ) Log[i]=Log[i>>1]+1;
    for ( int j=1;j<=Log[tot];j++ ) for ( int i=1;i+(1<<j)-1<=tot;i++ ) st[i][j]=std::min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
    for ( int ans=0,l,r;q--; ) l=read()^ans,r=read()^ans,write(ans=r-l+1-dep[A[l-1]]-dep[A[r]]+2*lca(A[l-1],A[r]));
    flush();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/RenSheYu/p/11329969.html