首页 > 其他分享 >20240929

20240929

时间:2024-09-29 18:50:15浏览次数:7  
标签:return int sum tr mid query 20240929

Tree Journey

猜结论

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e5 + 5;

int n, k, ans, dep[N];

vector<int> g[N];

void dfs(int u, int f) {
  dep[u] = dep[f] + 1;
  for (auto v : g[u]) {
    if (v == f) {
      continue;
    }
    dfs(v, u);
  }
  if (dep[u] - 1 <= k) {
    ans = max(ans, min(n, dep[u] + (k - dep[u] + 1) / 2));
  }
}

signed main() {
  cin >> n >> k;
  for (int i = 1, u, v; i < n; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  cout << ans;
  return 0;
}

Infinity Card Decks

我们显然只用考虑 \(a[i] > b[i]\) 的数对,假设现在考虑第 \(j\) 张牌是否能打断无限牌组.
设\(suma, sumb\)表示\(a[i],b[i]\)之和.
\(M - (suma - sumb) - b[j] < 0\)
\(M - (suma - sumb) - a[j] < 0\)
我们显然会发现这个是单调的,所以就可以了

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 5, INF = 1e18;

int n, m, sum[N], suma[N], sumb[N], p[N], a[N], b[N], res[N], ans, pos[N], pos1[N];

int tr[3][N * 4];

void Merge(int i) {
  tr[1][i] = tr[1][i * 2] + tr[1][i * 2 + 1];
  tr[2][i] = max(tr[2][i * 2], tr[2][i * 2 + 1]);
}

void build(int i, int l, int r) {
  if (l == r) {
    tr[1][i] = 0;
    if (a[l] > b[l]) {
      tr[2][i] = b[l];
    }
    else {
      tr[2][i] = a[l];
    }
    return ;
  }
  int mid = (l + r) >> 1;
  build(i * 2, l, mid);
  build(i * 2 + 1, mid + 1, r);
  Merge(i);
}

int query_max(int i, int l, int r, int x, int y) {
  if (x > y) {
    return -INF;
  }
  if (l > y || r < x) {
    return -INF;
  }
  if (l >= x && r <= y) {
    return tr[2][i];
  }
  int mid = (l + r) >> 1;
  return max(query_max(i * 2, l, mid, x, y), query_max(i * 2 + 1, mid + 1, r, x, y));
}

int query_sum(int i, int l, int r, int x, int y) {
  if (x > y) {
    return 0;
  }
  if (l > y || r < x) {
    return 0;
  }
  if (l >= x && r <= y) {
    return tr[1][i];
  }
  int mid = (l + r) >> 1;
  return query_sum(i * 2, l, mid, x, y) + query_sum(i * 2 + 1, mid + 1, r, x, y);
}

void modify(int i, int l, int r, int p, int x) {
  if (l == r) {
    tr[1][i] += x;
    return ;
  }
  int mid = (l + r) >> 1;
  if (mid >= p) modify(i * 2, l, mid, p, x);
  else modify(i * 2 + 1, mid + 1, r, p, x);
  Merge(i);
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + (a[i] - b[i]);
    suma[i] = suma[i - 1];
    sumb[i] = sumb[i - 1];
    if (a[i] > b[i]) {
      suma[i] += a[i];
      sumb[i] += b[i];
    }
    p[i] = sum[i];
  }
  sort(p + 1, p + n + 1);
  for (int i = 0; i <= n; i++) {
    pos[i] = lower_bound(p + 1, p + n + 1, sum[i]) - p;
    pos1[i] = upper_bound(p + 1, p + n + 1, sum[i]) - p;
  }
  for (int i = 1, j = 1; i <= n; i++) {
    while (j <= n && m - ((suma[j] - suma[i - 1]) - (sumb[j] - sumb[i - 1])) - query_max(1, 1, n, i, j) >= 0) {
      j++;
    }
    res[i] = j;
  }
  for (int i = 1, j = 1; i <= n; i++) {
    while (j < res[i]) {
      modify(1, 1, n, pos[j], 1);
      j++;
    }
    ans += (n - res[i] + 1);
    ans += query_sum(1, 1, n, pos1[i - 1], n);
    if (i <= res[i]) {
      modify(1, 1, n, pos[i], -1);
    }
  }
  cout << (n * (n + 1) / 2) - ans;
  return 0;
}

标签:return,int,sum,tr,mid,query,20240929
From: https://www.cnblogs.com/libohan/p/18440592

相关文章