QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#263698#7513. Palindromic Beadsucup-team859WA 1150ms340716kbC++147.2kb2023-11-25 03:34:062023-11-25 03:34:07

Judging History

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

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-11-25 03:34:07]
  • 评测
  • 测评结果:WA
  • 用时:1150ms
  • 内存:340716kb
  • [2023-11-25 03:34:06]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

using ll = long long;
using ull = unsigned long long;

string to_string(const string &s) {
  return '"' + s + '"';
}

string to_string(bool b) {
  return b ? "true" : "false";
}

template <typename A, typename B>
string to_string(const pair<A, B> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename T>
string to_string(const T &v) {
  string s = "{";
  bool first = true;
  for (const auto &it : v) {
    if (!first)
      s += ", ";
    else
      first = false;
    s += to_string(it);
  }
  return s += "}";
}

void debug_out() {
  cerr << endl;
}

template <typename T, typename... Args>
void debug_out(const T &first, const Args&... rest) {
  cerr << to_string(first) << " ";
  debug_out(rest...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

auto startTime = high_resolution_clock::now();
int get_time() {
  auto stopTime = chrono::high_resolution_clock::now();
  auto duration = duration_cast<milliseconds>(stopTime - startTime);
  return duration.count(); // in ms
}

namespace segtree2d {
  // dynamic segtree2d with point update and rectangle query
  
  struct node {
    int l, r;
    int d; // value for ox->oy node or actual value
  };

  int n, m;
  vector<node> arb;

  void build(int _n, int _m, int q) {
    n = _n;
    m = _m;
    if (q == 0)
      q = 1;
    arb.reserve(q);
    arb.emplace_back();
  }

  inline void calculate(int nod) {
    arb[nod].d = 0;
    if (arb[nod].l != 0)
      arb[nod].d = max(arb[nod].d, arb[arb[nod].l].d);
    if (arb[nod].r != 0)
      arb[nod].d = max(arb[nod].d, arb[arb[nod].r].d);
  }

  void update_y(int x, int v, int nod, int st = 1, int dr = m) {
    if (st == dr) {
      arb[nod].d = v;
      return;
    }

    int mij = (st + dr) / 2;
    if (x <= mij) {
      if (arb[nod].l == 0) {
        arb[nod].l = arb.size();
        arb.emplace_back();
      }
      update_y(x, v, arb[nod].l, st, mij);
    } else { 
      if (arb[nod].r == 0) {
        arb[nod].r = arb.size();
        arb.emplace_back();
      }
      update_y(x, v, arb[nod].r, mij + 1, dr);
    }   

    calculate(nod);
  }

  void update_x(int x, int y, int v, int nod = 0, int st = 1, int dr = n) {
    if (arb[nod].d == 0) {
      arb[nod].d = arb.size();
      arb.emplace_back();
    }

    update_y(y, v, arb[nod].d);

    if (st == dr)
      return;

    int mij = (st + dr) / 2;
    if (x <= mij) {
      if (arb[nod].l == 0) {
        arb[nod].l = arb.size();
        arb.emplace_back();
      }
      update_x(x, y, v, arb[nod].l, st, mij);
    } else {
      if (arb[nod].r == 0) {
        arb[nod].r = arb.size();
        arb.emplace_back();
      }
      update_x(x, y, v, arb[nod].r, mij + 1, dr);
    }
  }

  int query_y(int x, int y, int nod, int st = 1, int dr = m) {
    if (nod == 0)
      return 0;

    if (x <= st && dr <= y) {
      return arb[nod].d;
    }

    int mij = (st + dr) / 2;
    if (x <= mij && mij + 1 <= y)
      return max(query_y(x, y, arb[nod].l, st, mij), query_y(x, y, arb[nod].r, mij + 1, dr));

    if (x <= mij)
      return query_y(x, y, arb[nod].l, st, mij);

    return query_y(x, y, arb[nod].r, mij + 1, dr);
  }

  int query_x(int x, int y, int x2, int y2, int nod = 0, int st = 1, int dr = m) {
    if (x <= st && dr <= y)
      return query_y(x2, y2, arb[nod].d); 

    int mij = (st + dr) / 2;
    if (x <= mij && mij + 1 <= y && arb[nod].l != 0 && arb[nod].r != 0)
      return max(query_x(x, y, x2, y2, arb[nod].l, st, mij), query_x(x, y, x2, y2, arb[nod].r, mij + 1, dr));

    if (x <= mij && arb[nod].l != 0)
      return query_x(x, y, x2, y2, arb[nod].l, st, mij);

    if (arb[nod].r != 0)
      return query_x(x, y, x2, y2, arb[nod].r, mij + 1, dr);

    return 0;
  }

  void check() {
    for (int i = 0; i < arb.size(); ++i) {
      auto it = arb[i];
    }
  }
}

const int DIM = 2e5 + 5;

int a[DIM], h[DIM];
int f[DIM], nr[DIM];
int tmp, l[DIM], r[DIM];
int tt[DIM][20];
vector<int> v[DIM];

int get_dist(int x, int y) {
  if (h[x] < h[y])
    swap(x, y);

  int df = h[x] - h[y];
  int le = 0;
  for (int k = 19; k >= 0 ; --k) {
    if ((1 << k) & df) {
      x = tt[x][k];
      le |= 1 << k;
    }
  }
  if (x == y)
    return le;

  for (int k = 19; k >= 0 ; --k) {
    if (tt[x][k] != tt[y][k]) {
      x = tt[x][k];
      y = tt[y][k];
      le += 1 << k;
    }
  }

  return le;
}

int is_ancestor(int x, int y) {
  if (x == y)
    return x;

  if (h[x] < h[y])
    swap(x, y);

  int df = h[x] - h[y] - 1;
  for (int k = 19; k >= 0 ; --k)
    if ((1 << k) & df)
      x = tt[x][k];

  if (tt[x][0] == y)
    return x;

  return -1; 
}

void dfs(int nod = 1) {
  f[a[nod]] = nod;
  l[nod] = ++tmp;
  for (auto it : v[nod]) {
    if (it == tt[nod][0])
      continue;
    h[it] = h[nod] + 1;
    tt[it][0] = nod;
    dfs(it);
  }
  r[nod] = tmp;
}

struct pairs {
  int x, y, d;
  bool operator < (const pairs &aux) const {
    return d > aux.d; 
  }
};

void solve() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ++nr[a[i]];
  }

  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  tmp = 0;
  dfs();
  for (int k = 1; k <= 19; ++k) {
    for (int i = 1; i <= n; ++i)
      tt[i][k] = tt[tt[i][k - 1]][k - 1];
  }

  vector<pairs> ps;
  for (int i = 1; i <= n; ++i) {
    if (f[a[i]] != i) {
      int x = i;
      int y = f[a[i]];
      if (h[x] < h[y])
        swap(x, y);
      
      int d = get_dist(x, y);
      ps.push_back({x, y, d});
    }
  }

  sort(ps.begin(), ps.end());

  int ans = 1;
  segtree2d::build(n, n, n);
  for (auto it : ps) {
    int nod = is_ancestor(it.x, it.y);
    int q;
    if (nod == -1) {
      q = segtree2d::query_x(l[it.x], r[it.x], l[it.y], r[it.y]);
    } else {
      q = segtree2d::query_x(1, l[nod] - 1, l[it.x], r[it.x]);
      q = max(q, segtree2d::query_x(l[it.x], r[it.x], r[nod] + 1, n));
    }

    ans = max(ans, q + 2);
    segtree2d::update_x(min(l[it.x], l[it.y]), max(l[it.x], l[it.y]), q + 2);
    //segtree2d::update_x(l[it.y], l[it.x], q + 2);
  }

  for (int i = 1; i <= n; ++i) {
    for (auto it : v[i]) {
      if (it == tt[i][0])
        continue;

      if (l[it] - 1 > l[i])
        ans = max(ans, segtree2d::query_x(l[it], r[it], l[i], l[it] - 1) + 1);

      if (r[it] + 1 <= r[i]) {
        ans = max(ans, segtree2d::query_x(l[it], r[it], r[it] + 1, r[i]) + 1);
      }
    }

    if (l[i] > 1 && l[i] + 1 <= r[i]) {
      ans = max(ans, segtree2d::query_x(1, l[i] - 1, l[i] + 1, r[i]) + 1);
    }

    if (l[i] + 1 <= r[i] && r[i] + 1 <= n)
      ans = max(ans, segtree2d::query_x(l[i] + 1, r[i], r[i] + 1, n) + 1);

  }

  segtree2d::check();

  cout << ans << '\n';
}

int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  int t = 1;
//  cin >> t;
  while (t--)
    solve();

  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 15764kb

input:

4
1 1 2 2
1 2
2 3
2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 15928kb

input:

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

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 12928kb

input:

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

output:

2

result:

ok single line: '2'

Test #4:

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

input:

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

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 0ms
memory: 14216kb

input:

2000
845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...

output:

5

result:

ok single line: '5'

Test #6:

score: 0
Accepted
time: 697ms
memory: 336692kb

input:

200000
48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...

output:

5

result:

ok single line: '5'

Test #7:

score: 0
Accepted
time: 708ms
memory: 336660kb

input:

200000
13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 759ms
memory: 337380kb

input:

200000
38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...

output:

45

result:

ok single line: '45'

Test #9:

score: 0
Accepted
time: 849ms
memory: 337716kb

input:

200000
36449 57574 3145 38591 832 17710 66613 78947 27635 83275 89878 48329 94614 584 96832 9321 72046 44873 5396 61452 63224 63740 26579 13706 108490 19092 89439 85884 12016 5105 48638 74004 41569 35006 22276 45609 25350 49906 35479 15875 68938 77699 48828 21628 11242 77040 70838 45771 27704 64865 ...

output:

135

result:

ok single line: '135'

Test #10:

score: -100
Wrong Answer
time: 1150ms
memory: 340716kb

input:

200000
13528 65006 30352 8565 36687 6748 5507 44320 7189 17847 46996 82728 102722 4727 36914 74228 21460 87970 11733 47170 67282 104558 66436 64504 57055 88619 42995 25569 101298 90984 76491 51994 62257 103424 8221 69668 99170 6808 29043 73058 5277 26614 23654 25152 64939 38418 78518 5330 37531 4305...

output:

339

result:

wrong answer 1st lines differ - expected: '425', found: '339'