Luo Gu P1494 [National Team] small Z socks / [template] Mo team

Topic Link

0x00 ideas

We consider the number of socks of the same color within the statistical range, set \ (S_i \) represents \ (i \) several colors of socks, the socks, the color scheme is to contribute to the number \ (S_i * (S_i - 1)\)

Length \ (S \) interval, the total number of programs as \ (S * (S - 1 ) \)

The answer to the last minute about, if the probability is zero, output0/1

0x01 Mo maintenance team

As long as we maintain the same color range of the number of socks, there is a \ (cnt \) array, while maintaining the number of possible options \ (sum \)

Each with two pointers \ (L \ R & lt \) , continue to move around, and to query interval to coincide

Part of the code:

    while(L > q[i].l) -- L,sum += cnt[a[L]] << 1,++ cnt[a[L]];
    while(L < q[i].l) -- cnt[a[L]],sum -= cnt[a[L]] << 1,++ L;
    while(R > q[i].r) -- cnt[a[R]],sum -= cnt[a[R]] << 1,-- R;
    while(R < q[i].r) ++ R,sum += cnt[a[R]] << 1,++ cnt[a[R]];

Note the order of maintenance

For inquiries range, we will be sorting, sorting principle

  1. The left end point of the block, the block size \ (sqrt (n-) \) , where the number of the block denoted left end point \ (POS \)

  2. If the two query intervals \ (pos \) different, press \ (pos \) from small to large

  3. If \ (POS \) the same, press the right end \ (R & lt \) from small to large

Part of the code:

bool cmp(node a,node b){
    return (a.pos ^ b.pos) ? (a.pos < b.pos) : (a.r < b.r);
}

The complexity of the proof reference blog mo algorithm team - from entry to the Black title

  1. Moving the right end points: one each in complexity is the worst \ (O (n) \) , a total of \ (O (\ sqrt {n }) \) block, the complexity of the \ (O (n \ sqrt { n}) \)

  2. Moving left point: In each block, there are \ (X_i \) a left end point, the left end point of movement is the worst \ (O (X_i \ sqrt { n}) \) ; and a front After a jump, the worst is \ (O (2 \ {n }) \ sqrt) distance, that is the left end point of each jump are likely to be \ (O (\ sqrt {n }) \) level, complexity \ (O ( n \ sqrt {n}) \ )

  3. Sort complexity \ (O (n \ log { n}) \)

The total complexity is \ (O (n \ sqrt { n}) \)

0x02 Code

#include<bits/stdc++.h>
using namespace std;
#define N 50005
int read(){
    int x=0; char c=getchar(); int flag=1;
    while(!isdigit(c)) { if(c=='-') flag=-1; c=getchar(); }
    while(isdigit(c)) { x=((x+(x<<2))<<1)+(c^48); c=getchar(); }
    return x*flag;
}
int n,m,a[N],cnt[N];
struct node{ int l,r,pos,id; } q[N]; 
bool cmp(node a,node b){
    return (a.pos ^ b.pos) ? (a.pos < b.pos) : ( (a.pos & 1) ? (a.r < b.r) : (a.r > b.r) );
}
long long ans[N][2];
long long gcd(long long x,long long y) { return !y ? x : gcd(y,x % y); }
signed main(){
    n = read(),m = read();
    int size = sqrt(n);
    for(int i = 1;i <= n;i ++) a[i] = read();
    for(int i = 1;i <= m;i ++) q[i].l = read(),q[i].r = read(),q[i].pos = (q[i].l - 1) / size + 1, q[i].id = i;
    sort(q + 1,q + m + 1,cmp);
    long long sum=0; int L=1,R=0;
    for(int i = 1;i <= m;i ++){
        while(L > q[i].l) -- L,sum += cnt[a[L]] << 1,++ cnt[a[L]];
        while(L < q[i].l) -- cnt[a[L]],sum -= cnt[a[L]] << 1,++ L;
        while(R > q[i].r) -- cnt[a[R]],sum -= cnt[a[R]] << 1,-- R;
        while(R < q[i].r) ++ R,sum += cnt[a[R]] << 1,++ cnt[a[R]];
        
        ans[q[i].id][0] = sum; ans[q[i].id][1] = 1ll * (R - L + 1) * (R - L);
    }
    for(int i = 1;i <= m;i ++){
        if(ans[i][0] == 0) { puts("0/1"); continue; }
        long long z = gcd(ans[i][0],ans[i][1]);
        ans[i][0] /= z; ans[i][1] /= z;
        printf("%lld/%lld\n",ans[i][0],ans[i][1]);
        
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zzhzzh123/p/12240435.html