QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#87972#5363. ZYB 的游览计划wxy0 5736ms19632kbC++143.0kb2023-03-14 22:09:502023-03-14 22:09:54

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-14 22:09:54]
  • 评测
  • 测评结果:0
  • 用时:5736ms
  • 内存:19632kb
  • [2023-03-14 22:09:50]
  • 提交

answer

// start time : 21 : 55

#include <bits/stdc++.h>

#define rep(i,l,r) for (int i = l; i <= r; i++)
#define per(i,r,l) for (int i = r; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define mp std::make_pair
#define gin std::cin
#define prt std::cout
#define edl std::endl

namespace Main {
  const int N = 2e5 + 5;
  int n,k,p[N];
  int head[N],edge[N << 1],fail[N << 1],tot;
  int dfn[N],idx[N],fa[20][N],dep[N],cnt;
  std::set<int> S;
  int curl,curr;
  long long curans,f[N],g[N];
  void add(int x,int y) {
    edge[++ tot] = y,fail[tot] = head[x],head[x] = tot;
  }
  void dfs(int x) {
    dfn[x] = ++ cnt; idx[cnt] = x;
    rep(i,1,19) fa[i][x] = fa[i - 1][fa[i - 1][x]];
    for (int i = head[x]; i; i = fail[i]) {
      int v = edge[i];
      if (v == fa[0][x]) continue;
      fa[0][v] = x; dep[v] = dep[x] + 1; 
      dfs(v);
    }
  }
  int lca(int x,int y) {
    if (dep[x] < dep[y]) std::swap(x,y);
    int delta = dep[x] - dep[y];
    rep(i,0,19) if (delta >> i & 1) x = fa[i][x];
    if (x == y) return x;
    per(i,19,0) if (fa[i][x] != fa[i][y]) x = fa[i][x],y = fa[i][y];
    return fa[0][x];
  }
  int dist(int x,int y) {
    return dep[x] + dep[y] - 2 * dep[lca(x,y)];
  }
  void insert(int x) {
    if (x == 1) return;
    auto it = S.upper_bound(x);
    if (it == S.end()) {
      -- it; int pre = idx[*it];
      curans -= dist(1,pre); curans += dist(1,x); curans += dist(pre,x);
    } else {
      int next = idx[*it]; -- it; int pre = idx[*it];
      curans -= dist(pre,next); curans += dist(pre,x); curans += dist(x,next);
    }
    S.insert(dfn[x]);
  }
  void delet(int x) {
    if (x == 1) return;
    S.erase(dfn[x]);
    auto it = S.upper_bound(x);
    if (it == S.end()) {
      -- it; int pre = idx[*it];
      curans -= dist(pre,x); curans -= dist(1,x); curans += dist(1,pre);
    } else {
      int next = idx[*it]; -- it; int pre = idx[*it];
      curans -= dist(pre,x); curans -= dist(x,next); curans += dist(pre,next);
    }
  }
  void solve(int l,int r,int x,int y) {
    if (l > r) return;
    int mid = (l + r) >> 1,pos = x;
    rep(j,x,std::min(mid,y)) {
      while (curr < mid) {
        ++ curr; insert(p[curr]);
      }
      while (curl > j) {
        -- curl; insert(p[curl]);
      }
      while (curr > mid) {
        delet(p[curr]); -- curr;
      }
      while (curl < j) {
        delet(p[curl]); ++ curl;
      }
      if (g[j - 1] + curans >= f[mid]) {
        pos = j; f[mid] = g[j - 1] + curans;
      }
    }
    solve(l,mid - 1,x,pos); solve(mid + 1,r,pos,y);
  }
  void main() {
    scanf("%d%d",&n,&k); rep(i,1,n) scanf("%d",&p[i]);
    rep(i,1,n - 1) {
      int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x);
    }
    dep[0] = -1; dfs(1);
    rep(i,1,n) f[i] = -1000000000000000000;
    rep(i,1,k) {
      rep(j,0,n) g[j] = f[j];
      rep(j,0,n) f[j] = -1000000000000000000;
      curl = 1; curr = 0; curans = 0; S.clear(); S.insert(1); solve(1,n,1,n);
    }
    prt << f[n];
  }
} signed main() { Main::main(); return 0; }

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 2ms
memory: 3688kb

input:

5 2
2 4 3 5 1
1 2
2 3
3 4
4 5

output:

14

result:

ok single line: '14'

Test #2:

score: -5
Wrong Answer
time: 5736ms
memory: 19632kb

input:

90752 2
88555 48815 61754 47133 45462 73740 84783 58077 73713 78537 14562 78533 71607 24890 16284 41187 78450 31531 25296 45169 55240 83197 82146 86338 83180 75924 9923 40553 84394 73069 7278 77214 24896 14446 87566 70680 48218 58608 86578 47698 86173 89371 77350 85782 36318 22735 80925 5435 76359 2...

output:

3224496

result:

wrong answer 1st lines differ - expected: '251418', found: '3224496'

Subtask #2:

score: 0
Wrong Answer

Test #10:

score: 0
Wrong Answer
time: 921ms
memory: 3796kb

input:

760 217
632 417 493 400 316 482 542 614 36 134 604 291 745 484 451 746 518 378 487 650 633 223 601 259 33 257 309 683 705 627 513 670 130 395 512 115 466 376 575 356 180 716 539 403 431 563 568 468 596 239 296 379 537 224 526 215 725 234 663 603 401 21 75 660 506 393 105 88 462 620 449 338 276 200 4...

output:

114848

result:

wrong answer 1st lines differ - expected: '35938', found: '114848'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #29:

score: 0
Wrong Answer
time: 1316ms
memory: 6292kb

input:

14878 6
1663 4532 2022 11114 1768 7744 12403 7698 14863 1406 13199 9405 3528 9898 1205 3076 11342 7459 9401 10025 14498 7178 11457 1802 9923 1648 13136 10720 3002 7332 13780 2094 1716 13215 8118 318 11186 14833 7941 8174 8999 6189 7504 13738 4933 3367 12973 1889 9835 4080 3546 1993 1861 11613 2504 1...

output:

694178

result:

wrong answer 1st lines differ - expected: '78002', found: '694178'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%