QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420618#8718. 保区间最小值一次回归问题ScintillaWA 378ms31268kbC++203.0kb2024-05-24 20:42:482024-05-24 20:42:49

Judging History

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

  • [2024-05-24 20:42:49]
  • 评测
  • 测评结果:WA
  • 用时:378ms
  • 内存:31268kb
  • [2024-05-24 20:42:48]
  • 提交

answer

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <random>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_map>

using namespace std;

#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define per(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << " \n"[_ == r]

using i64 = long long;

const i64 inf = 1e18;

const int N = 5e5 + 10;

int read() {
  int x = 0, f = 1; char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + (c - 48);
  return x * f;
}

int n, m, a[N], bd[N], mx[N];
i64 f[N], pre[N], ans;
struct node {
  int l, r, k;
  node() {}
  friend bool operator < (node u, node v) {
    return u.k < v.k;
  }
} o[N];
map <int, vector <int>> pos;
map <int, vector <node>> lim;

int fa[N];
int get(int u) { return u == fa[u] ? u : fa[u] = get(fa[u]); }
void merge(int u, int v) { fa[get(v)] = get(u); }

void solve() {
  n = read(), m = read(), ans = 0;
  pos.clear(), lim.clear();
  rep(i, 1, n) a[i] = read();
  rep(i, 1, m) {
    o[i].l = read(), o[i].r = read(), o[i].k = read();
    lim[o[i].k].emplace_back(o[i]);
  }
  sort(o + 1, o + m + 1);
  rep(i, 1, n) fa[i] = i, bd[i] = -1;
  per(i, m, 1) {
    for (int p = o[i].r; (p = get(p)) >= o[i].l; merge(p - 1, p)) bd[p] = o[i].k;
  }
  rep(i, 1, n) if (~bd[i]) pos[bd[i]].emplace_back(i);
  // pa(bd, 1, n);
  for (auto [k, vec] : lim) {
    if (pos[k].empty()) {
      printf("-1\n");
      return;
    }
  }
  for (auto [k, vec] : pos) {
    // cout << "--- k = " << k << endl;
    int w = vec.size();
    // pv(w);
    // pa(vec, 0, w - 1);
    rep(i, 1, w) {
      mx[i] = 0, f[i] = inf;
      pre[i] = pre[i - 1] + max(0, k - a[vec[i - 1]]);
    }
    for (auto it : lim[k]) {
      int l = lower_bound(vec.begin(), vec.end(), it.l) - vec.begin() + 1;
      int r = lower_bound(vec.begin(), vec.end(), it.r) - vec.begin() + 1;
      if (l > r) {
        printf("-1\n");
        return;
      }
      // cout << "l, r = " << l << ' ' << r << endl;
      mx[r] = max(mx[r], l);
    }
    deque <int> q;
    q.push_back(0);
    rep(i, 1, w) {
      mx[i] = max(mx[i], mx[i - 1]);
      f[i] = f[i - 1];
      while (q.size() && q.front() < mx[i - 1]) q.pop_front();
      if (q.size()) f[i] = min(f[i], f[q.front()] + pre[i - 1] - pre[q.front()]);
      f[i] += abs(k - a[vec[i - 1]]);
      while (q.size() && f[q.back()] >= f[i]) q.pop_back();
      q.push_back(i);
    }
    // pa(mx, 1, w);
    // pa(f, 1, w);
    // pa(g, 1, w);
    i64 res = inf;
    rep(i, mx[w], w) res = min(res, f[i]) + pre[w] - pre[i];
    ans += res;
  }
  printf("%lld\n", ans);
}

int main() {
  for (int tc = read(); tc; --tc) solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3792kb

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: 378ms
memory: 31268kb

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
45
42
39
48
47
49
45
46
48
46
56
943
52
50
55
45
62
57
45
48
50
42
55
50
57
49
43
40
43
40
50
57
42
58
45
48
43
35
43
43
52
44
48
904
937
48
50
48
50
45
41
53
51
41
45
54
51
51
48
38
46
43
41
55
41
46
47
36
49
51
46
44
46
46
49
42
48
40
45
46
36
43
45
53
46
48
44
41
45
47
39
48
54
44
42
43
47
48
...

result:

wrong answer 2nd numbers differ - expected: '46', found: '45'