QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#330384#7439. 铃原露露yllcm0 303ms79120kbC++144.5kb2024-02-17 15:04:142024-02-17 15:04:15

Judging History

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

  • [2024-02-17 15:04:15]
  • 评测
  • 测评结果:0
  • 用时:303ms
  • 内存:79120kb
  • [2024-02-17 15:04:14]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
using namespace std;
inline int read() {
  int x = 0; bool op = false;
  char c = getchar();
  while(!isdigit(c))op |= (c == '-'), c = getchar();
  while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return op ? -x : x;
}
const int N = 2e5 + 10;
const int INF = 1e9;
int n, m;
int a[N];
vector<int> G[N];
set<int> st[N];
struct SGT1 {
  int mn[N << 2];
  void build(int k, int l, int r) {
    mn[k] = INF;
    if(l == r)return ;
    int mid = l + r >> 1;
    build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r);
    return ;
  }
  void update(int k, int l, int r, int qx, int qy, int w) {
    if(l >= qx && r <= qy)return mn[k] = min(mn[k], w), void();
    int mid = l + r >> 1;
    if(qx <= mid)update(k << 1, l, mid, qx, qy, w);
    if(qy > mid)update(k << 1 | 1, mid + 1, r, qx, qy, w);
    return ;
  }
  void push(int k, int l, int r, int *f, int val = INF) {
    val = min(val, mn[k]);
    if(l == r)return f[l] = val, void();
    int mid = l + r >> 1;
    push(k << 1, l, mid, f, val); push(k << 1 | 1, mid + 1, r, f, val);
    return ;
  }
}T1, T2;
struct SGT2 {
  ll sum1[N << 2];
  int sum2[N << 2], tg[N << 2];
  void mark(int k, int w) {
    sum1[k] += sum2[k] * w;
    tg[k] += w;
    return ;
  }
  void pushdown(int k) {
    if(tg[k] == 0)return ;
    mark(k << 1, tg[k]);
    mark(k << 1 | 1, tg[k]);
    tg[k] = 0;
    return ;
  }
  void pushup(int k) {
    sum1[k] = sum1[k << 1] + sum1[k << 1 | 1];
    sum2[k] = sum2[k << 1] + sum2[k << 1 | 1];
    return ;
  }
  void update(int k, int l, int r, int qx, int qy, int w) {
    if(l >= qx && r <= qy)return mark(k, w), void();
    int mid = l + r >> 1; pushdown(k);
    if(qx <= mid)update(k << 1, l, mid, qx, qy, w);
    if(qy > mid)update(k << 1 | 1, mid + 1, r, qx, qy, w);
    pushup(k);
    return ;
  }
  void update(int k, int l, int r, int x, int w) {
    if(l == r)return sum2[k] += w, void();
    int mid = l + r >> 1; pushdown(k);
    if(x <= mid)update(k << 1, l, mid, x, w);
    else update(k << 1 | 1, mid + 1, r, x, w);
    pushup(k);
    return ;
  }
  ll query(int k, int l, int r, int qx, int qy) {
    if(l >= qx && r <= qy)return sum1[k];
    int mid = l + r >> 1; ll res = 0; pushdown(k);
    if(qx <= mid)res += query(k << 1, l, mid, qx, qy);
    if(qy > mid)res += query(k << 1 | 1, mid + 1, r, qx, qy);
    return res;
  }
}T3;
void merge(int u, int v) {
  if(st[u].size() < st[v].size())swap(st[u], st[v]);
  for(auto x : st[v]) {
    auto it = st[u].lower_bound(x);
    if(x > a[u]) {
      int p3 = (it == st[u].end() ? n + 1 : *it);
      int p2 = (it == st[u].begin() ? 0 : *prev(it));
      T1.update(1, 1, n, max(a[u], p2) + 1, x, p3 - 1);
      if(p2 > a[u] && it != st[u].begin()) {
        it--;
        int p1 = (it == st[u].begin() ? 0 : *prev(it));
        T1.update(1, 1, n, max(a[u], p1) + 1, p2, x - 1);
      }
    }
    else {
      int p2 = (it == st[u].end() ? n + 1 : *it);
      int p1 = (it == st[u].begin() ? 0 : *prev(it));
      T2.update(1, 1, n, x, min(a[u], p2) - 1, -(p1 + 1));
      if(p2 < a[u] && it != st[u].end()) {
        it++;
        int p3 = (it == st[u].end() ? 0 : *it);
        T2.update(1, 1, n, p2, min(a[u], p3) - 1, -(x + 1));
      }
    }
  }
  for(auto x : st[v])st[u].insert(x);
  return ;
}
void dfs(int u) {
  for(int v : G[u])dfs(v), merge(u, v);
  st[u].insert(a[u]);
  return ;
}
int pl[N], pr[N];
ll ans[N];
vector<pii> q[N];
vector<int> del[N];
int main() {
  n = read(); m = read();
  for(int i = 1; i <= n; i++)a[i] = read();
  for(int i = 2; i <= n; i++) {int u = read(); G[u].pb(i);}
  dfs(1);
  T1.build(1, 1, n); T2.build(1, 1, n);
  T1.push(1, 1, n, pl); T2.push(1, 1, n, pr);
  for(int i = 1; i <= n; i++)
    pl[i] = min(pl[i], n), pr[i] = max(-pr[i], 1);
  // for(int i = 1; i <= n; i++)printf("%d ", pl[i]); putchar('\n');
  // for(int i = 1; i <= n; i++)printf("%d ", pr[i]); putchar('\n');
  // fflush(stdout);
  for(int i = 1; i <= n; i++)del[pr[i] - 1].pb(i);
  for(int i = 1; i <= m; i++) {
    int l = read(), r = read();
    q[l].pb({i, r});
  }
  for(int i = n; i; i--) {
    T3.update(1, 1, n, i, 1);
    for(auto r : del[i])T3.update(1, 1, n, r, -1);
    T3.update(1, 1, n, i, pl[i], 1);
    for(auto t : q[i])ans[t.FR] = T3.query(1, 1, n, i, t.SE);
  }
  for(int i = 1; i <= m; i++)printf("%lld\n", ans[i]);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 30772kb

input:

100 100
5 29 12 16 25 36 18 37 27 47 34 40 20 3 1 42 26 19 33 41 6 22 8 58 32 62 24 15 35 17 59 30 50 61 43 49 39 67 44 21 13 31 68 69 65 64 10 28 38 54 70 63 9 46 66 52 23 7 48 60 55 56 51 2 57 11 53 14 45 4 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
...

output:

45
2701
561
10
91
78
946
120
2080
300
861
351
1540
666
210
210
595
2278
465
435
496
496
2556
210
28
1326
496
1596
1
741
3828
10
595
630
10
861
15
105
2556
120
1275
528
2080
21
861
1830
2485
136
10
861
1770
4186
2211
171
21
946
351
6
2415
28
190
435
36
4560
2080
1596
1953
1540
820
105
3
91
15
741
241...

result:

wrong answer 1st numbers differ - expected: '9', found: '45'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 303ms
memory: 79120kb

input:

200000 1
73119 155820 110077 139724 136809 18709 57745 43535 89117 43647 20295 60551 108184 188031 180437 52363 72969 130559 179796 75852 53879 96998 63387 76458 193661 142318 28260 40465 80050 188507 143795 141018 94880 71333 7644 109237 105208 109509 9779 159914 135096 47638 175577 182927 173100 1...

output:

20000100000

result:

wrong answer 1st numbers differ - expected: '216932', found: '20000100000'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%