QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#61828#2560. Streetlightsalpha1022RE 4835ms24736kbC++176.5kb2022-11-15 09:16:542022-11-15 09:16:56

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-15 09:16:56]
  • 评测
  • 测评结果:RE
  • 用时:4835ms
  • 内存:24736kb
  • [2022-11-15 09:16:54]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int inf = 0x3f3f3f3f;

const int N = 1e5;
const int Q = 2.5e5;

int n, q;
int a[N + 5], las[N + 5];
pair<int, int> upd[N + Q + 5];
int pre[N + Q + 5], nxt[N + Q + 5];

struct SegmentTree {
  #define ls (u << 1)
  #define rs (ls | 1)
  int seg[N * 4 + 5];
  void insert(int x, int k, int u, int tl, int tr) {
    if (tl == tr) { seg[u] = k; return ; }
    int mid = (tl + tr) >> 1;
    if (x <= mid) insert(x, k, ls, tl, mid);
    else insert(x, k, rs, mid + 1, tr);
    seg[u] = max(seg[ls], seg[rs]);
  }
  void insert(int x, int k) { insert(x, k, 1, 1, n); }
  pair<int, int> queryL(int x, int k, int u, int tl, int tr) {
    if (seg[u] < k) return {0, inf};
    if (tl == tr) return {tl, seg[u]};
    int mid = (tl + tr) >> 1;
    if (x <= mid) return queryL(x, k, ls, tl, mid);
    auto ret = queryL(x, k, rs, mid + 1, tr);
    if (!ret.first) ret = queryL(x, k, ls, tl, mid);
    return ret;
  }
  int queryL(int x, int k) {
    if (!x) return 0;
    auto res = queryL(x, k, 1, 1, n);
    return res.second == k ? res.first : 0;
  }
  pair<int, int> queryR(int x, int k, int u, int tl, int tr) {
    if (seg[u] < k) return {n + 1, inf};
    if (tl == tr) return {tl, seg[u]};
    int mid = (tl + tr) >> 1;
    if (x > mid) return queryR(x, k, rs, mid + 1, tr);
    auto ret = queryR(x, k, ls, tl, mid);
    if (ret.first > n) ret = queryR(x, k, rs, mid + 1, tr);
    return ret;
  }
  int queryR(int x, int k) {
    if (x > n) return n + 1;
    auto res = queryR(x, k, 1, 1, n);
    return res.second == k ? res.first : n + 1;
  }
  #undef ls
  #undef rs
} seg;

struct node {
  int l, r, h, w;
  node(int l = 0, int r = 0, int h = 0, int w = 0): l(l), r(r), h(h), w(w) {}
  bool operator<(const node &o) const { return l < o.l; }
  bool operator==(const node &o) const { return l == o.l; }
};

pair<vector<node>, int> build(vector<int> newStatic, vector<pair<int, int>> upd, vector<node> tr) {
  sort(newStatic.begin(), newStatic.end()), sort(upd.begin(), upd.end());
  static int anc[N + 5];
  static node newTr[N + 5];
  int j = 0, top = 0, tot = 0;
  for (auto [i, _]: upd) seg.insert(i, 0);
  for (int i: newStatic) {
    seg.insert(i, a[i]);
    for (; j < tr.size() && tr[j].l <= i; ++j) {
      auto [l, r, h, w] = tr[j];
      for (; top && tr[anc[top]].r <= r; --top) newTr[tot++] = tr[anc[top]];
      anc[++top] = j;
    }
    for (; top && tr[anc[top]].r < i; --top) newTr[tot++] = tr[anc[top]];
    for (; top && tr[anc[top]].h <= a[i]; --top);
  }
  for (; top; --top) newTr[tot++] = tr[anc[top]];
  for (; j < tr.size(); ++j) newTr[tot++] = tr[j];
  for (int i: newStatic) {
    int l = seg.queryL(i - 1, a[i]);
    if (l) newTr[tot++] = node(l, i, a[i], 1);
    int r = seg.queryR(i + 1, a[i]);
    if (r <= n) newTr[tot++] = node(i, r, a[i], 1);
  }
  tr = vector<node>(newTr, newTr + tot), sort(tr.begin(), tr.end()), tr.erase(unique(tr.begin(), tr.end()), tr.end());
  vector<int> sum(tr.size());
  int cnt = 0;
  for (int i = 0; i < tr.size(); ++i) {
    auto [l, r, h, w] = tr[i];
    for (; top && tr[anc[top]].r <= r; --top);
    if (top) sum[i] = sum[anc[top]];
    sum[i] += w, cnt += w;
    anc[++top] = i;
  }
  vector<int> crit(1, 0);
  crit.reserve(upd.size() + 1);
  j = top = 0;
  for (auto [i, h]: upd) {
    for (; j < tr.size() && tr[j].l <= i; ++j) {
      auto [l, r, h, w] = tr[j];
      for (; top && tr[anc[top]].r <= r; --top);
      anc[++top] = j;
    }
    for (; top && tr[anc[top]].r < i; --top);
    crit.push_back(anc[top]);
    int l = 1, r = top, mid, res = 1;
    while (l <= r) {
      mid = (l + r) >> 1;
      if (tr[anc[mid]].h > h) l = mid + 1, res = mid;
      else r = mid - 1;
    }
    crit.push_back(anc[res]);
  }
  sort(crit.begin(), crit.end()), crit.erase(unique(crit.begin(), crit.end()), crit.end());
  crit.reserve(crit.size() * 2);
  j = top = 0;
  for (int i = 0, siz = crit.size(); i + 1 < siz; ++i) {
    for (; j <= crit[i]; ++j) {
      auto [l, r, h, w] = tr[j];
      for (; top && tr[anc[top]].r <= r; --top);
      anc[++top] = j;
    }
    for (; top && tr[anc[top]].r <= tr[crit[i + 1]].r; --top);
    crit.push_back(anc[top]);
  }
  sort(crit.begin(), crit.end()), crit.erase(unique(crit.begin(), crit.end()), crit.end());
  top = 0;
  for (int i = 0; i < crit.size(); ++i) {
    auto [l, r, h, w] = tr[crit[i]];
    for (; top && tr[anc[top]].r <= r; --top);
    w = sum[crit[i]];
    if (top) w -= sum[anc[top]];
    cnt -= w; 
    anc[++top] = crit[i];
    newTr[i] = node(l, r, h, w);
  }
  return {vector<node>(newTr, newTr + crit.size()), cnt};
}

int ans[N + Q + 5];

void solve(int l, int r, vector<node> tr) {
  if (l == r) {
    a[upd[l].first] = upd[l].second;
    int tag = build({upd[l].first}, {}, tr).second;
    ans[l] += tag, ans[l + 1] -= tag;
    return ;
  }
  int mid = (l + r) >> 1;
  vector<int> tmp;
  vector<pair<int, int>> updTmp;
  static bool vis[N + 5];
  if (mid >= n) {
    for (int i = mid + 1; i <= r; ++i) if (pre[i] < l) tmp.push_back(upd[i].first), vis[upd[i].first] = 1;
    updTmp = vector<pair<int, int>>(upd + l, upd + mid + 1);
    for (int i = l; i <= mid; ++i) if (pre[i] && pre[i] < l && !vis[upd[pre[i]].first]) updTmp.push_back(upd[pre[i]]);
    for (int i: tmp) vis[i] = 0;
    auto [L, lTag] = build(tmp, updTmp, tr);
    ans[l] += lTag, ans[mid + 1] -= lTag;
    vector<int>().swap(tmp), vector<pair<int, int>>().swap(updTmp);
    solve(l, mid, L);
  }
  for (int i = l; i <= mid; ++i) if (nxt[i] > r) tmp.push_back(upd[i].first), vis[upd[i].first] = 1;
  updTmp = vector<pair<int, int>>(upd + mid + 1, upd + r + 1);
  for (int i = mid + 1; i <= r; ++i) if (pre[i] && pre[i] <= mid && !vis[upd[pre[i]].first]) updTmp.push_back(upd[pre[i]]);
  for (int i: tmp) vis[i] = 0;
  auto [R, rTag] = build(tmp, updTmp, tr);
  ans[mid + 1] += rTag, ans[r + 1] -= rTag;
  solve(mid + 1, r, R);
}

int main() {
  scanf("%d%d", &n, &q);
  a[0] = a[n + 1] = inf;
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i),
    upd[i] = make_pair(i, a[i]), las[i] = i;
  for (int i = n + 1; i <= n + q; ++i)
    scanf("%d%d", &upd[i].first, &upd[i].second),
    pre[i] = las[upd[i].first], las[upd[i].first] = i;
  fill(las + 1, las + n + 1, n + q + 1);
  for (int i = n + q; i; --i) nxt[i] = las[upd[i].first], las[upd[i].first] = i;
  solve(1, n + q, {node(0, n + 1, inf, 0)});
  for (int i = 1; i <= n + q; ++i) ans[i] += ans[i - 1];
  for (int i = n; i <= n + q; ++i) printf("%d\n", ans[i]);
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 13944kb

input:

6 2
4 2 2 2 4 6
4 6
6 4

output:

3
2
2

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 10644kb

input:

50 100
310081863 722273055 654741011 310081863 654741011 722273055 654741011 722273055 654741011 654741011 654741011 310081863 310081863 722273055 654741011 654741011 654741011 722273055 310081863 654741011 310081863 310081863 310081863 722273055 310081863 654741011 654741011 310081863 722273055 722...

output:

28
28
28
29
30
31
31
31
31
31
31
31
31
32
33
34
34
33
33
33
33
32
32
31
31
31
32
32
31
31
31
31
30
30
30
31
31
31
31
31
31
30
30
29
30
31
32
32
32
32
32
31
32
33
33
33
33
32
32
31
32
33
31
31
32
31
32
31
31
31
30
31
30
29
29
28
28
29
28
28
27
27
27
27
27
27
26
27
28
27
28
29
28
28
28
28
29
29
28
29
28

result:

ok 101 lines

Test #3:

score: 0
Accepted
time: 2ms
memory: 12340kb

input:

50 100
93308794 275481889 130830018 675774101 130830018 93308794 275481889 999873895 275481889 104418887 130830018 275481889 675774101 999873895 130830018 841188804 360486542 104418887 140762403 275481889 275481889 770511267 104418887 140762403 93308794 675774101 104418887 770511267 130830018 933087...

output:

12
12
11
11
11
11
11
11
10
10
10
10
10
10
10
11
11
11
11
12
12
12
12
12
11
10
11
11
11
11
11
12
13
12
12
13
13
14
12
11
11
10
10
10
10
10
9
9
9
9
9
9
8
9
10
10
9
10
9
9
9
10
10
11
12
12
13
13
13
13
14
14
13
13
13
12
12
12
12
13
12
12
12
12
12
12
13
13
13
13
15
15
15
17
18
18
17
17
16
16
15

result:

ok 101 lines

Test #4:

score: 0
Accepted
time: 2ms
memory: 14136kb

input:

50 100
195248019 905127308 129122336 764519854 338556860 795943323 554412442 338556860 217191782 140699690 654772489 386182517 217191782 37485244 795943323 924638428 795943323 820028162 855279832 795943323 129122336 554412442 195248019 764519854 810525122 554412442 201706134 661330059 129122336 2090...

output:

5
5
6
5
5
5
4
4
3
3
3
3
3
2
4
3
3
3
4
5
5
5
5
5
5
5
5
4
4
4
5
6
6
5
5
4
4
4
3
3
3
3
3
3
4
4
4
4
3
3
4
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
5
5
5
5
6
6
6
5
5
5
5
5
6
6
6
6
7
7
7
7
7
7
7
7
7
7
7
7
7
7
8
8
8
7

result:

ok 101 lines

Test #5:

score: 0
Accepted
time: 5ms
memory: 14232kb

input:

50 100
772094573 19576803 263817454 873867094 557813690 952336439 500513802 392057352 305209480 199018938 206776586 514630037 466387810 403552086 50423285 658534934 19576803 404488754 179660945 591777562 262850065 817419372 680762089 591777562 424021147 403552086 718896141 456431927 680762089 595426...

output:

1
1
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
0
0
0
0
0
1
1
1
1
1
1
0
0
0
0
1
2
2
2
3
3
3
3
3
3
2
2
2
2
2
2
2
1
1
1
0
0
0
0
0
0
0
0
0
1
2
2
3
3
3
3
3
3
2
2
2
3
3
3
3
3
3
3

result:

ok 101 lines

Test #6:

score: 0
Accepted
time: 4ms
memory: 13824kb

input:

50 100
5096114 61078240 254964021 318250156 571031769 256037951 208426954 833646260 732869624 746606948 226729785 151221431 611264696 351005299 205027954 706057630 453231547 874058912 462474957 366832522 823051853 289489922 109072951 103985450 269915659 377686154 809672410 12123621 732787174 9017273...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 101 lines

Test #7:

score: 0
Accepted
time: 5ms
memory: 13912kb

input:

50 100
976187983 976187983 879080743 976187983 827737130 827737130 827737130 827737130 815905933 811453113 789018592 789018592 681089922 675640665 659464656 635119734 635119734 633485638 633485638 567930339 552957008 484438465 484438465 484438465 387753272 377659696 376161946 976187983 367642977 376...

output:

16
15
14
14
15
16
17
17
18
18
31
30
29
28
27
26
25
24
23
22
21
20
20
19
20
19
18
18
18
18
18
17
17
16
16
16
16
15
15
15
15
14
13
12
12
12
12
11
11
10
9
9
9
8
8
7
7
6
7
6
7
7
7
8
8
7
8
10
10
10
10
10
11
10
10
12
14
15
15
16
16
15
16
18
19
20
26
26
27
28
29
28
28
27
26
25
24
23
23
22
21

result:

ok 101 lines

Test #8:

score: 0
Accepted
time: 1ms
memory: 13832kb

input:

50 100
843864537 245114944 227661173 137675097 918583745 80278395 44678681 37169219 37007425 27167524 4382795 4043558 3655016 3624538 2994987 1979195 1407769 819862 771067 665903 137891 137891 665903 771067 819862 1407769 1979195 2994987 3624538 3655016 4043558 4382795 27167524 37007425 37142735 305...

output:

17
17
16
15
7
7
8
5
5
5
5
5
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
0
0
0
1
1
2
3
3
4
4
4
4
5
6
6
7
8
9
10
11
12
13
14
14
14
14
14
15
16
16
16
16
17
18
19
19
20
21
20
19
18
15
14
14
14
13
7
6
6
6
6
6
6
6
6
6
6
5
5
4
4
4
3
3
3
3
3
3
2
2
2
2

result:

ok 101 lines

Test #9:

score: 0
Accepted
time: 1ms
memory: 13016kb

input:

50 100
920202355 768392166 755066475 630812635 617367313 601334965 450742259 367726734 265094786 151773018 77676966 53524889 53524889 77676966 151773018 265094786 205222950 154745305 57476426 57476426 154745305 294856628 367726734 450742259 601334965 617367313 630812635 481253037 481253037 755066475...

output:

19
20
20
19
18
17
16
15
14
13
12
11
10
10
10
10
9
8
7
7
7
8
7
7
7
6
6
5
5
5
5
4
4
3
3
4
3
3
3
3
3
3
3
3
2
2
3
3
3
3
3
4
4
5
5
5
6
6
6
6
8
10
10
11
12
13
13
14
14
15
15
15
20
21
22
21
19
17
17
17
15
14
13
13
13
13
13
12
12
11
10
8
8
8
8
7
7
7
7
7
7

result:

ok 101 lines

Test #10:

score: 0
Accepted
time: 4835ms
memory: 24736kb

input:

100000 250000
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1...

output:

99296
99297
99298
99299
99301
99302
99304
99306
99307
99308
99310
99312
99313
99314
99315
99317
99318
99319
99320
99321
99322
99323
99324
99326
99327
99329
99330
99332
99333
99334
99335
99337
99338
99339
99341
99343
99345
99347
99348
99349
99350
99351
99353
99354
99355
99356
99357
99358
99359
99360
...

result:

ok 250001 lines

Test #11:

score: -100
Runtime Error

input:

100000 250000
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1...

output:


result: