QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19111#1877. Matryoshka DollsForza_FerrariRE 3ms9936kbC++142.9kb2022-01-28 10:45:182022-05-06 04:14:31

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:14:31]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:9936kb
  • [2022-01-28 10:45:18]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,a[500001],block,p[500001],pos[500001];
long long ans[500001],dp[1001][1001],sum;
struct element
{
    int l,r,id;
    bool operator <(const element &other) const
    {
        return pos[l]^pos[other.l]? pos[l]<pos[other.l]:pos[l]&1? r<other.r:r>other.r;
    }
}q[500001];
set<int> s[1001],tmp;
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 int Abs(int x)
{
    return x>=0? x:-x;
}
inline void insert(int x)
{
    set<int>::iterator nxt=tmp.upper_bound(x);
    if(nxt!=tmp.end())
        sum+=Abs(p[x]-p[*nxt]);
    if(nxt!=tmp.begin())
    {
        set<int>::iterator pre=prev(nxt);
        sum+=Abs(p[x]-p[*pre]);
        if(nxt!=tmp.end())
            sum-=Abs(p[*nxt]-p[*pre]);
    }
    tmp.insert(x);
}
inline void del(int x)
{
    set<int>::iterator it=tmp.lower_bound(x),nxt=next(it);
    if(nxt!=tmp.end())
        sum-=Abs(p[x]-p[*nxt]);
    if(it!=tmp.begin())
    {
        set<int>::iterator pre=prev(it);
        sum-=Abs(p[x]-p[*pre]);
        if(nxt!=tmp.end())
            sum+=Abs(p[*nxt]-p[*pre]);
    }
    tmp.erase(it);
}
inline void solve()
{
    for(int i=1;i<=n;++i)
    {
        p[a[i]=read()]=i;
        s[i].insert(a[i]);
    }
    for(int len=2;len<=n;++len)
        for(int i=1,j=len;j<=n;++i,++j)
        {
            dp[i][j]=dp[i][j-1];
            set<int>::iterator nxt=s[i].upper_bound(a[j]);
            if(nxt!=s[i].end())
                dp[i][j]+=Abs(j-p[*nxt]);
            if(nxt!=s[i].begin())
            {
                set<int>::iterator pre=prev(nxt);
                dp[i][j]+=Abs(j-p[*pre]);
                if(nxt!=s[i].end())
                    dp[i][j]-=Abs(p[*nxt]-p[*pre]);
            }
            s[i].insert(a[j]);
        }
    while(m--)
    {
        int l=read(),r=read();
        printf("%lld\n",dp[l][r]);
    }
}
int main()
{
    n=read(),m=read();
    //if(m==1ll*n*(n-1)/2)
    {
        solve();
        return 0;
    }
    block=sqrt(n);
    for(int i=1;i<=n;++i)
    {
        p[a[i]=read()]=i;
        pos[i]=(i-1)/block+1;
    }
    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;i<=m;++i)
    {
        for(;r<q[i].r;++r)
            insert(a[r+1]);
        for(;l>q[i].l;--l)
            insert(a[l-1]);
        for(;r>q[i].r;--r)
            del(a[r]);
        for(;l<q[i].l;++l)
            del(a[l]);
        //for(auto j:tmp)
            //cout<<j<<" ";
        //cout<<endl;
        ans[q[i].id]=sum;
    }
    for(int i=1;i<=m;++i)
        printf("%lld\n",ans[i]);
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 9936kb

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: 3ms
memory: 5808kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Runtime Error

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:


result: