QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#417259#8718. 保区间最小值一次回归问题whxqwqWA 667ms96240kbC++143.1kb2024-05-22 16:48:222024-05-22 16:48:23

Judging History

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

  • [2024-05-22 16:48:23]
  • 评测
  • 测评结果:WA
  • 用时:667ms
  • 内存:96240kb
  • [2024-05-22 16:48:22]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void read (int &x) {
    char ch = getchar(); x =0 ; while (!isdigit(ch)) ch =getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 5e5 + 5;
int n, m, a[N];
vector<int> del[N], add[N];
#define pb push_back
struct qwq { int l, r, v; } b[N];
multiset <int> s;
multiset <int>::iterator it;
int val[N];
vector<int> w[N]; vector<int>::iterator iw;
vector<pair<int, int> > seg[N]; 
#define ll long long
int L[N];
multiset<ll> u;  ll f[N];
multiset<ll>::iterator iu;
ll calc (int x) {
    int lim = 0;
    // printf ("666 %d\n", val[x]);
    // for (auto x : seg[x]) printf ("%d %d\n", x.first, x.second);
    // for (int y : w[x]) printf ("%d ", y); puts ("");
    for (int y : w[x]) L[y] = 0;
    for (auto i : seg[x]) {
        int l = i.first, r = i.second;
        iw = upper_bound (w[x].begin(), w[x].end(), r);
        if (iw == w[x].begin()) return -1;
        --iw; if (*iw < l) return -1;
        // printf ("ok %d %d\n", x, *iw);
        L[*iw] = max (L[*iw], l);
        lim = max (lim, l);
    }
    int num = (int)w[x].size(), la = 0, tag = 1;
    ll ans = 1e18;
    u.clear(); u.insert (0);
    for (int i = 1; i <= num; ++i) {
        int now = w[x][i - 1];
        // printf ("%d %d\n", now, )
        if ((int)u.size() == 0) return -1;
        f[i] = *u.begin() + abs (a[now] - val[x]);
        // printf ("123 %d %lld\n", i, f[i]);
        u.insert (f[i]);
        if (a[now] < val[x]) {
            u.clear (), tag = 0; u.insert (f[i]); la = i - 1;
        } else {
            if (tag && L[now]) iu = u.find (0), u.erase (iu), tag = 0;
            while (la < num && w[x][la] < L[now]) {
                iu = u.find (f[la + 1]); u.erase (iu); ++la;
            }      
        }
    }
    if ((int)u.size() == 0) return -1;
    // printf ("hhh %lld\n", ans);
    return *u.begin();
}
void work () {
    read (n), read (m);
    for (int i = 1; i <= n; ++i) read (a[i]);
    for (int i = 1; i <= n; ++i) del[i].clear(), add[i].clear();
    for (int i = 1, l, r, v; i <= m; ++i) {
        read (l), read (r), read (v);
        b[i] = (qwq){l, r, v}; val[i] = v;
    }
    sort (val + 1, val + m + 1);
    int tot = unique (val + 1, val + m + 1) - val - 1;
    for (int i = 1; i <= tot; ++i) seg[i].clear(), w[i].clear();
    for (int i = 1; i <= m; ++i) {
        int v = lower_bound (val + 1, val + tot + 1, b[i].v) - val;
        b[i].v = v;
        add[b[i].l].pb (v), del[b[i].r + 1].pb (v);
        seg[v].push_back (make_pair (b[i].l, b[i].r));
    }
    s.clear ();
    for (int i = 1; i <= n; ++i) {
        for (int x : add[i]) s.insert (-x);
        for (int x : del[i]) it = s.find (-x), s.erase (it);
        if ((int)s.size() == 0) continue;
        int now = *s.begin();
        w[-now].push_back (i);
    }   
    long long ans = 0;
    for (int i = 1, t = 0; i <= tot; ++i) {
        t = calc (i); ans += t; if (t == -1) return (void)puts("-1");
    }
    printf ("%lld\n", ans);
}
signed main() {
    int T; read (T);
    while (T--) work ();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 10ms
memory: 50676kb

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 608ms
memory: 96064kb

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:

49
46
43
39
48
47
49
46
46
48
47
56
952
53
50
55
46
62
57
46
49
50
42
55
51
57
50
43
41
44
41
53
57
45
59
45
48
44
37
48
43
52
46
50
909
948
48
50
50
50
45
42
54
53
42
46
55
52
51
48
38
48
43
41
55
41
48
47
38
51
54
46
44
47
46
49
43
48
42
45
47
36
43
45
53
46
48
45
42
45
48
40
49
54
44
43
45
48
49
...

result:

ok 1000 numbers

Test #3:

score: 0
Accepted
time: 644ms
memory: 96240kb

input:

1000
100 100
40 35141748 84105257 84105343 7273633 178494885 178495007 112519027 77833696 77833671 261586538 278472335 278472427 261586652 416361094 416361130 426649132 323519561 278472520 420127898 420127936 420127900 482516532 434108895 420127875 631535939 615931040 546346933 546346951 546347103 7...

output:

5391
5728
5395
4133
4671
3496
3629
5091
5285
5530
4748
5441
84743
6061
4524
6175
5755
5357
3835
5486
4698
5872
4955
5320
4374
5628
4426
4747
5019
4439
4361
5774
6141
6897
5578
5067
5036
5719
4380
4763
4098
4693
4514
5690
92018
84532
4776
5056
4351
5501
4885
4828
5223
6395
3673
6007
6028
5993
5289
45...

result:

ok 1000 numbers

Test #4:

score: -100
Wrong Answer
time: 667ms
memory: 96044kb

input:

1000
100 100
148088468 98149781 583014390 471155624 805747464 361353765 809227446 405213819 979246312 746234854 328924822 293996764 439427756 927284157 709886804 719570910 987283502 607077506 460561747 601690465 661900658 689494603 664094557 738719400 968257412 849232761 704892887 981300891 63494525...

output:

13272909075
10500705683
8434695623
9598250477
9795994718
8084945549
7816377095
11664729042
11963959801
12049221193
7612459564
12443185625
52382998805
15859152345
12213559141
13098156239
16019877206
13030210346
7118046309
12943939894
10948537395
10167904190
13684987690
12625876652
9528059400
11541470...

result:

wrong answer 3rd numbers differ - expected: '12729662919', found: '8434695623'