QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#603155#8718. 保区间最小值一次回归问题UESTC_DECAYALI#WA 474ms37504kbC++203.0kb2024-10-01 14:59:462024-10-01 14:59:47

Judging History

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

  • [2024-10-01 14:59:47]
  • 评测
  • 测评结果:WA
  • 用时:474ms
  • 内存:37504kb
  • [2024-10-01 14:59:46]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#define int long long
#define RI register int
#define CI const int&
using namespace std;
const int N=500005,INF=1e18;
struct ifo
{
    int l,r,v;
    friend inline bool operator < (const ifo& A,const ifo& B)
    {
        return A.v>B.v;
    }
}O[N]; int t,n,m,tl,tr,a[N],fa[N],mxp[N];
inline int getfa(CI x)
{
    return fa[x]!=x?fa[x]=getfa(fa[x]):x;
}
class Segment_Tree
{
    private:
        int mn[N<<2];
    public:
        #define TN CI now=1,CI l=tl,CI r=tr
        #define LS now<<1,l,mid
        #define RS now<<1|1,mid+1,r
        inline void build(TN)
        {
            if (l==r) return (void)(mn[now]=INF);
            int mid=(l+r)/2; build(LS); build(RS);
        }
        inline int query(CI beg,CI end,TN)
        {
        	if (beg>end) return INF;
            if (beg<=l&&r<=end) return mn[now]; int mid=(l+r)/2,ret=INF;
            if (beg<=mid) ret=min(ret,query(beg,end,LS));
            if (end>mid) ret=min(ret,query(beg,end,RS));
            return ret;
        }
        inline void update(CI pos,CI mv,TN)
        {
            if (l==r) return (void)(mn[now]=mv); int mid=(l+r)/2;
            if (pos<=mid) update(pos,mv,LS); else update(pos,mv,RS);
            mn[now]=min(mn[now<<1],mn[now<<1|1]);
        }
        #undef TN
        #undef LS
        #undef RS
}SEG;
inline int solve(CI l,CI r,CI v)
{
    vector <int> vec;
    for (RI i=l;i<=r;++i)
    {
        int x=getfa(O[i].l);
        while (x<=O[i].r)
        {
            vec.push_back(x);
            fa[x]=x+1; x=getfa(x);
        }
    }
    sort(vec.begin(),vec.end());
    for (RI i=0;i<vec.size();++i) mxp[i]=0;
    for (RI i=l;i<=r;++i)
    {
        int L=lower_bound(vec.begin(),vec.end(),O[i].l)-vec.begin();
        int R=lower_bound(vec.begin(),vec.end(),O[i].r+1)-vec.begin()-1;
        if (L>R) return -1; else mxp[R]=max(mxp[R],L);
    }
    tl=0; tr=vec.size(); SEG.build(); SEG.update(0,0);
    int lst=-1;
    for (RI i=0;i<vec.size();++i)
    {
        if (i-1>=0) lst=max(lst,mxp[i-1]);
        int tmp=SEG.query(lst+1,i)+abs(a[vec[i]]-v);
        SEG.update(i+1,tmp);
        if (a[vec[i]]<=v) lst=i;
    }
    lst=max(lst,mxp[vec.size()-1]);
    return SEG.query(lst+1,vec.size());
}
signed main()
{
    //freopen("F.in","r",stdin);
    for (scanf("%lld",&t);t;--t)
    {
        scanf("%lld%lld",&n,&m);
        for (RI i=1;i<=n;++i) scanf("%lld",&a[i]);
        for (RI i=1;i<=n+1;++i) fa[i]=i;
        for (RI i=1;i<=m;++i) scanf("%lld%lld%lld",&O[i].l,&O[i].r,&O[i].v);
        sort(O+1,O+m+1); bool flag=1; int ans=0;
        for (RI i=1;i<=m;)
        {
            int j=i;
            while (j+1<=m&&O[j+1].v==O[i].v) ++j;
            int tmp=solve(i,j,O[i].v);
            if (tmp==-1) { flag=0; break; }
            ans+=tmp; i=j+1;
        }
        if (!flag) puts("-1"); else printf("%lld\n",ans);
    }
    return 0;
}

詳細信息

Test #1:

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

input:

1
3 2
2023 40 41
1 1 2022
2 3 39

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: -100
Wrong Answer
time: 474ms
memory: 37504kb

input:

1000
100 100
1 35141686 84105222 84105220 7273527 178494861 178494861 112519027 77833654 77833656 261586535 278472336 278472336 261586536 416361017 416361017 426649080 323519513 278472337 420127821 420127823 420127823 482516531 434108818 420127821 631535744 615930922 546346921 546346920 546346920 70...

output:

53
48
44
44
50
49
51
49
48
52
49
60
957
54
50
57
50
66
57
48
50
54
43
58
52
59
53
44
44
46
42
56
57
48
61
49
48
44
39
51
45
52
47
52
909
957
48
50
51
53
48
44
56
53
43
48
58
57
54
51
41
50
44
43
55
42
52
47
38
53
54
50
47
48
46
49
43
48
42
47
48
38
46
49
54
49
48
48
42
46
49
41
51
57
48
44
49
50
49
...

result:

wrong answer 1st numbers differ - expected: '49', found: '53'