QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19286#1877. Matryoshka DollsForza_FerrariWA 3ms16280kbC++142.5kb2022-01-28 22:21:462022-05-06 04:36:20

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-06 04:36:20]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:16280kb
  • [2022-01-28 22:21:46]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,a[500001],p[500001],block,pos[500001],s[500001],pre[500001],nxt[500001],sum[500001];
long long ans[500001],res;
struct element
{
    int l,r,id;
    bool operator <(const element &other) const
    {
        return pos[l]^pos[other.l]? pos[l]<pos[other.l]:r>other.r;
    }
}q[500001];
inline int read()
{
    int x=0;
    char c=getchar();
    while(c<'0'||c>'9')
        c=getchar();
    while(c>='0'&&c<='9')
    {
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    return x;
}
inline void print(long long x)
{
    if(x>=10)
        print(x/10);
    putchar(x%10+'0');
}
inline int Abs(int x)
{
    return x>=0? x:-x;
}
inline void insert(int x)
{
    ++sum[x];
}
inline void del(int x)
{
    --sum[x];
    if(pre[x])
    {
        res-=Abs(p[pre[x]]-p[x]);
        nxt[pre[x]]=nxt[x];
    }
    if(nxt[x])
    {
        res-=Abs(p[nxt[x]]-p[x]);
        pre[nxt[x]]=pre[x];
    }
    if(pre[x]&&nxt[x])
        res+=Abs(p[pre[x]]-p[nxt[x]]);
    pre[x]=nxt[x]=0;
}
int main()
{
    n=read(),m=read();
    block=sqrt(n);
    for(int i=1;i<=n;++i)
    {
        p[a[i]=read()]=i;
        pos[i]=(i-1)/block+1;
        if(pos[i]^pos[i-1])
            s[pos[i]]=i;
    }
    for(int i=1;i<=m;++i)
    {
        q[i].l=read(),q[i].r=read();
        q[i].id=i;
    }
    sort(q+1,q+m+1);
    for(int i=1,l=1,r=0,lst=0;i<=m;++i)
    {
        if(lst^pos[q[i].l])
        {
            lst=pos[q[i].l];
            for(;l>s[pos[q[i].l]];--l)
                insert(a[l-1]);
            for(;r<n;++r)
                insert(a[r+1]);
            for(;l<s[pos[q[i].l]];++l)
                del(a[l]);
            int k=0;
            res=0;
            for(int j=1;j<=n;++j)
            {
                pre[j]=nxt[j]=0;
                if(sum[j])
                {
                    pre[j]=k;
                    if(k)
                    {
                        nxt[k]=j;
                        res+=Abs(p[j]-p[k]);
                    }
                    k=j;
                }
            }
        }
        for(;r>q[i].r;--r)
            del(a[r]);
        int tmp=res;
        for(;l<q[i].l;++l)
            del(a[l]);
        ans[q[i].id]=res;
        for(;l>s[pos[q[i].l]];--l)
            insert(a[l-1]);
        res=tmp;
    }
    for(int i=1;i<=m;++i)
    {
        print(ans[i]);
        putchar('\n');
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 15884kb

input:

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

output:

7
5
3
1
0

result:

ok 5 number(s): "7 5 3 1 0"

Test #2:

score: 0
Accepted
time: 2ms
memory: 15820kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 2ms
memory: 16280kb

input:

100000 1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...

output:

4999950000

result:

ok 1 number(s): "4999950000"

Test #4:

score: 0
Accepted
time: 2ms
memory: 15872kb

input:

20 1
12 8 13 10 18 14 1 19 5 16 15 9 17 20 6 2 11 4 3 7
9 18

output:

36

result:

ok 1 number(s): "36"

Test #5:

score: -100
Wrong Answer
time: 3ms
memory: 15956kb

input:

20 10
5 16 11 7 19 8 12 13 17 18 6 1 14 3 4 2 15 20 10 9
7 11
7 13
7 17
11 15
1 7
4 6
1 5
6 14
3 5
9 9

output:

14
23
34
9
20
3
10
27
10
5

result:

wrong answer 1st numbers differ - expected: '7', found: '14'