QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603140#8718. 保区间最小值一次回归问题UESTC_DECAYALI#WA 485ms35756kbC++203.0kb2024-10-01 14:53:512024-10-01 14:53:52

Judging History

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

  • [2024-10-01 14:53:52]
  • 评测
  • 测评结果:WA
  • 用时:485ms
  • 内存:35756kb
  • [2024-10-01 14:53:51]
  • 提交

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)-vec.begin()+1;
        if (L>R) return -1; else mxp[R]=max(mxp[R],L);
    }
    if (vec.empty()) return 0;
    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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 485ms
memory: 35756kb

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:

51
46
37
39
44
43
45
46
46
48
40
56
777
50
43
54
44
61
53
45
45
52
38
51
49
56
51
43
40
43
41
52
53
43
57
42
43
40
38
48
41
46
45
48
738
776
43
49
47
52
46
40
50
48
39
46
52
53
49
45
38
46
40
38
52
42
47
42
33
51
47
49
45
45
42
38
36
42
35
45
43
31
43
47
49
47
46
46
38
38
42
38
46
50
46
42
43
42
42
...

result:

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