Luo Gu [P1972] [HH] BZOJ1878 necklace [Mo] team

Disclaimer: If you want to reprint, can be explained in the comments directly, thank you! https://blog.csdn.net/SSL_ZYC/article/details/91043383

Subject to the effect:

Topic links:
Luo Gu: https://www.luogu.org/problemnew/show/P1972
BZOJ: https://www.lydsy.com/JudgeOnline/problem.php?id=1878

A given sequence of query [ l i , r i ] [l_i,r_i] How many different numbers.


Ideas:

Mo Bare team title. In fact, this question is a weaker version (without modification)
but this question Alcamo right. open O f a s t , O 3 Ofast,O3 will MLE.
Do not talk about the team method, I believe we will. Here to talk about how the team card with Mo over this question.

  1. Input Output Optimization
  2. Block length T = 1250 T=1250
  3. Merge statement.
    This can greatly improve the efficiency of the program. In addition to the code are okay wind ugly accident

So far the program can be snapped into the 1200 m s 1200ms . Mo is the most original team 2000 + m s 2000+ms is.
Really do not know how to cut. So Mo had looked at the card team code.
They find that sort of no avail c m p cmp , but staged this

bool operator < (const Ask &a,const Ask &b){
	return pos[a.l]^pos[b.l] ? a.l<b.l : pos[a.l]&1 ? a.r<b.r:a.r>b.r;
}

Seen from the conditional statement, this apparently is in place c m p cmp a.
And can greatly speed up. I do not know the specific reasons q w q moment .

  1. Plus the metaphysics optimization.
  2. Luo open valley comes O 2 O2 .

This can be very stable over this question out of.
Here Insert Picture Description

We can see the slowest point 600 m s 600ms inside, considered a fast.


Code:

// luogu-judger-enable-o2
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <algorithm>
#define reg register
using namespace std;

const int N=500010,M=1000010;
int n,m,l,r,sum,T,a[N],cnt[M],pos[N],ans[N];

struct Ask
{
    int l,r,id;
}ask[N];

bool operator < (const Ask &a,const Ask &b)
{
    return pos[a.l]^pos[b.l] ? a.l<b.l : pos[a.l]&1 ? a.r<b.r:a.r>b.r;
}

inline int read()
{
    int d=0;
    char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch))
        d=(d<<3)+(d<<1)+ch-48,ch=getchar();
    return d;
}

inline void write(int x)
{
    if (x>9) write(x/10);
    putchar(x%10+48);
}

int main()
{
    n=read();
    T=1250;
    for (reg int i=1;i<=n;++i)
    {
        a[i]=read();
        pos[i]=(i-1)/T+1;
    }
    m=read();
    for (reg int i=1;i<=m;++i)
    {
        ask[i].l=read(); ask[i].r=read();
        ask[i].id=i;
    }
    sort(ask+1,ask+1+m);
    l=1;
    for (reg int i=1;i<=m;++i)
    {
        while(l>ask[i].l) sum+=(++cnt[a[--l]]==1);
        while(l<ask[i].l) sum-=(--cnt[a[l++]]==0);
        while(r>ask[i].r) sum-=(--cnt[a[r--]]==0);
        while(r<ask[i].r) sum+=(++cnt[a[++r]]==1);
        ans[ask[i].id]=sum;
    }
    for (reg int i=1;i<=m;++i)
        write(ans[i]),putchar(10);
    return 0;
}

Guess you like

Origin blog.csdn.net/SSL_ZYC/article/details/91043383