[Mo] data structure team (two)

  Today's content with Mo repair team .

Example: P1903 [National Team] number of colors / Maintenance queue

Title Description

Ink ink bought a N branch color brush (some of which may be the same color), placed in a row, you need to answer questions ink ink. Ink ink will be released the following instructions to you:

1, QLR ask you on behalf of a total of brush several different colors of brushes from L to R brushes in the first.

2, RP Col brushes replace the color of P Col.

In order to meet the requirements of ink in the ink, you know you need to do it?

Input Format

Line 1 two integers N, M, representing the number of the initial number of ink in the ink pen and do things.

Row 2 N integers, representing the initial i-th row branched brush pen color.

The third row to the 2 + M rows, each row representing the ink in the ink will do a thing, formatted as casual working portion.

Output Format

For each query Query, you need to give a number in the corresponding row, L represents the brushes to a total of R brushes brush several different colors.

Sample input and output

Input # 1
6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6
Output # 1
4
4
3
4

Description / Tips

To 100% of the data, N≤50000, M≤50000, all integers appearing all input data are not less than 1 and no more than 10 ^ 6.

This problem may be a slight constant Card

Source: bzoj2120

This question data Luo Gu self-made data use CYaRon took 5 minutes to complete production data.


Daixiumodui

  Mo team is an amazing off-line algorithm. You can solve the problem on the interval, by sorting and optimizing violence against interrogation block.

  Mo is an ordinary team title given sequence, constant questioning.

  So if mixed with some modification operations in the challenge, MO team can do it?

  The answer of course is possible.

  Recall Mo implementation team:

    1. Pretreatment advance

    2. Ask block ordering

    3. Solution double pointer movement.

  The only modification operation because of a difference is 3 operation.

  It can be found: When there has been modify operation, I asked the current section of the processing may not have been looking for our range of.

  So how to do it?

  Learn about sustainable tree line of thought: time back .

  We can put with the team as general repair Mo Mo teams do, if done after the discovery time is not on it back in time.

  Accurate to say: Consider the cost of the operation between two points in time.

  So to say, as long as the back loop pile while ordinary Mo team plus two on OK.

 while(now<q[i].t)
     change(++now);
 while(now>q[i].t)
     change(now--);    

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int N=50005;
int col[N],n,m,sum[1000005],be[N];
int fans,qnum,cnum,ans[N];

struct qs{
    int l,r,t,id;
}q[N];
struct cs{
    int x,i;
}c[N];

bool cmp(qs x,qs y) {
    if(be[x.l]!=be[y.l])
        return x.l<y.l;
    if(be[x.r]!=be[y.r])
        return x.r<y.r;
    return x.t<y.t;
}

void upd1(int x) {
    if(!sum[col[x]])
        ++fans;
    ++sum[col[x]];
    return;
}

void upd2(int x) {
    --sum[col[x]];
    if(!sum[col[x]])
        --fans;
    return;
}

int l,r,now;

void change(int x) {
    if(c[x].i<=r&&c[x].i>=l) {
        --sum[col[c[x].i]];
        if(!sum[col[c[x].i]])
            --fans;
        if(!sum[c[x].x])
            ++fans;
        ++sum[c[x].x];
    }
    swap(c[x].x,col[c[x].i]);
    return;
}

int main()
{
    cin>>n>>m;
    int xx=pow(n,2.0/3.0);
    char opt; 
    for(int i=1; i <= n; i ++ ) { 
        cin >> col [i]; 
        bc [i] = i / XX + 1 ; 
    } 
    For ( int i = 1 ; i <= n; i ++ ) { 
        cin >> opt;
        if (opt == ' Q ' ) {
             ++ qnum; 
            cin >> m [qnum] >> l m [qnum] r; 
            q [qnum] t = cnum; 
            q [qnum] hu = qnum; 
        } 
        Else {
             ++ cnum; 
            cin>>c[cnum].i>>c[cnum].x;
        }
    }
    sort(q+1,q+qnum+1,cmp);
    l=r=fans=now=0;
    for(int i=1;i<=qnum;i++) {
        while(l<q[i].l)
            upd2(l++);
        while(l>q[i].l)
            upd1(--l);
        while(r>q[i].r)
            upd2(r--);
        while(r<q[i].r)
            upd1(++r);
        while(now<q[i].t)
            change(++now);
        while(now>q[i].t)
            change(now--);
        ans[q[i].id]=fans;
    }
    for(int i=1;i<=qnum;i++)
        cout<<ans[i]<<endl;
    return 0;
}
Number of colors

 


Double experience CF940F Machine Learning

  Here stamp

Guess you like

Origin www.cnblogs.com/qxyzili--24/p/11297608.html