SD provincial team training camp, "colorful world" 2019Day7 of

Colorful world (shinku) (Ynoi2015 even see the future)

Title Description

You give a sequence, each section of the query interval length is 1, 2, ... extremely long range the number of consecutive segments 10
define a continuous range of segment:
The segment number priority to which all the weight sorted meta the resulting sequence is b
if b satisfy [L] for the tuple (l, r), b [ l + 1], ... b [r] each number is a number +1 before
and for tuple (l, r + 1), (l-1, r) are not satisfied, we call (l, r) is a length of
r-l + extremely long range continuous segment 1

Thinking

A query interval length which is 1 -> number of extremely long range of 10 successive segments of
the query is independent of the number and the position of each interval
and then, if (l, r) is a range very long continuous sections that the absence of (l-1, r), (l, r + 1) of these two successive segments range
such as the range of all the values are mapped onto the array range:
000100111010 0111011111010
(4,4) (7,9) (11,11) (14,16) (18,22) (24,24) that all of the very long range successive segments
each output interval length is 1, 2 ... number of consecutive paragraphs extremely long range of k

Sub-section

A

Violence
overall complexity O (nm)
score of about 10-25

B

Team + bitset Mo
Mo range bitset team ran out of range, then the number of intervals by four Westerner to each successive segment can range

The total complexity of O (nm / w)
score of about 35-55

C

Mo + Team data structure
maintains each range segment successive data structure, each time Mo force transfer may be implemented on a binary data structure

The total complexity of O (nsqrtmlogn)
score of about 35-55

D

Do not delete the team
to maintain an extremely long range for each successive segment to the left, expand the farthest right position
every time, if only to insert, you can O (1) maintain
and apply the team can not delete Mo

The total complexity of O (nsqrtm)
score of about 45-55

E

Bitwise optimized Mo + team
each query is a query corresponding to a point forward, the number of consecutive 1
and from more than 10 on meaningless
the bit operation can be used to optimize
a single complexity is O (k / w) = O (1) in the

The total complexity of O (nsqrtm)
score of about 45-55

practice

Consider the scanning lines
scan the right end, the opening 10 of each tree indicates the number of array extremely long range of the length of each consecutive segment of the left point
to maintain the position of each value of the most recent occurrence (in sequence)
sweeping to a position when violence swept left and right 10

It is equivalent to the value 10 in the raised position of the left and right, and then appear in the last number of descending order, and a back one plug

This is left point <= 5 when there is a long range to very long continuous section 2
and the left endpoint of <= 3 when there is a long range to very long continuous section 3
and a left end point <= 2 when there is a long range to very long continuous section 7
and the left end point <= 1 when there is a continuous length 10 very long range section

Thus equivalent:
left position is extremely long range of successive segments 2 appears in the new length of the insertion point is around [4,5]
length is extremely long range left end position appearing continuous section 3 is [ 3,3]
length is extremely long range occurring successive segments 2 is a left end position [2,2]
of length extremely long range left end position appearing continuous section 1 is [1,1]

Then the original has been added contribution removed, replaced with the new contribution will be able
to produce a maximum of 20 new contribution to
overall complexity O (10 (n + m) logn)

Code

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#define lowbit( i ) i & -i
#define MAXN 1000010
#define MAXK 10
#define RG 11
#define MAXD 25

using namespace std;

struct point
{
    int x , y;
    point( int x , int y ) : x( x ) , y( y ) {}
    point() {}
} t[ MAXD ] , temp3[233];

inline bool cmp( const point & a , const point & b )
{
    return a.x == b.x ? a.y > b.y : a.x > b.x;
}

vector < point > q[ MAXN ];
int n , m , cnt , a[ MAXN ] , b[ ( MAXN + MAXK ) << 1 ] , last_pos[ MAXN << 1 ] , f[ MAXN ][ MAXK ] , g[ MAXK ][ MAXN ] , res[ MAXK ] , temp1[ MAXD ] , temp2[ MAXD ];
char ans[ MAXN ][ MAXD ];

inline void modify( int x , unsigned char y , char c )
{
    for( register int i = x ; i ; i ^= lowbit( i ) )
        f[i][y] += c;
}

inline void query( int x , int max_pos )
{
    for( register int i = 0 ; i < MAXK ; i++ ) res[i] = 0;
    for( register int i = x ; i <= max_pos ; i += lowbit( i ) )
        for( register unsigned char j = 0 ; j < MAXK ; j++ )
            res[j] += f[i][j];
}

int pos;

inline void Modify( int x , unsigned char y , char c )
{
    temp3[ pos++ ] = point( x , y );
    g[y][x] += c;
}

inline void make()
{
    for( register unsigned char i = 0 ; i < pos ; i++ )
        if( g[ temp3[i].y ][ temp3[i].x ] )
        {
            modify( temp3[i].x , temp3[i].y , g[ temp3[i].y ][ temp3[i].x ] );
            g[ temp3[i].y ][ temp3[i].x ] = 0;
        }
    pos = 0;
}

struct io               //快读
{
    char ibuf[1 << 25] , * s , obuf[1 << 24] , * t;
    int a[24];
    io() : t( obuf )
    {
        freopen( "shinku.in" , "rb" , stdin );
        freopen( "shinku.out" , "w" , stdout );
        s[ fread( s = ibuf , 1 , 1 << 25 , stdin ) ] = 0;
    }
    ~io()
    {
        fwrite( obuf , 1 , t - obuf , stdout );
    }
    inline int read()
    {
        register int u = 0;
        while( * s < 48 ) s++;
        while( * s > 32 )
            u = u * 10 + * s++ - 48;
        return u;
    }
    inline void print( register char * u )
    {
        for( register unsigned char i = 0 ; i < MAXK ; i++ )
            * t++ = * u++;
        * t++ = '\n';
    }
} ip;

#define read ip.read
#define print ip.print

int main()
{
    n = read() , m = read();
    for( register int i = 1 ; i <= n ; i++ ) a[i] = read();
    for( register int i = 1 ; i <= m ; i++ )
    {
        int l = read() , r = read();
        q[r].push_back( point( l , i ) );
    }
    for( register char i = 1 ; i <= MAXD ; i++ ) b[ ++cnt ] = 0;
    for( register int i = 1 ; i <= n ; i++ ) b[ ++cnt ] = a[i] , b[ ++cnt ] = a[i] + 1;
    sort( b + MAXD + 1 , b + cnt + 1 );
    cnt = unique( b + MAXD + 1 , b + cnt + 1 ) - b - 1;
    for( register int i = 1 ; i <= n ; i++ )
        a[i] = upper_bound( b + 1 , b + cnt + 1 , a[i] ) - b - 1;
    for( register int i = 1 ; i <= n ; i++ )
    {
        int c = 0;
        for( register char j = -RG ; j <= RG ; j++ )
            if( last_pos[ a[i] + j ] )
                t[ c++ ] = point( last_pos[ a[i] + j ] , j + RG );
        sort( t , t + c , cmp );
        t[c].x = 0;
        temp2[ RG ] = last_pos[ a[i] ] = i;
        Modify( i , 0 , 1 );
        Modify( t[0].x , 0 , -1 );
        for( register int j = 0 , a , b ; j < c && t[j].y != RG ; j++ )
        {
            temp1[ t[j].y ] = temp2[ t[j].y ] = i;
            for( a = 0 ; a < RG && temp1[ RG - a - 1 ] == i ; a++ );
            for( b = 0 ; b < RG && temp1[ RG + b + 1 ] == i ; b++ );
            if( a && a < RG )
                Modify( t[j].x , a - 1 , -1 ) , Modify( t[j + 1].x , a - 1 , 1 );
            if( b && b < RG )
                Modify( t[j].x , b - 1 , -1 ) , Modify( t[j + 1].x , b - 1 , 1 );
            if( a + b < MAXK )
                Modify( t[j].x , a + b , 1 ) , Modify( t[j + 1].x , a + b , -1 );
        }
        make();
        for( register vector < point > :: iterator j = q[i].begin() ; j != q[i].end() ; j++ )
        {
            query( j -> x , i );
            for( register int k = 0 ; k < MAXK ; k++ )
                ans[ j -> y ][k] = res[k] % 10 + 48;
        }
    }
    for( register int i = 1 ; i <= m ; i++ )
        print( ans[i] );
    return 0;
}

Guess you like

Origin www.cnblogs.com/water-lift/p/10993779.html