首页 > 其他分享 >20240927

20240927

时间:2024-09-27 16:46:11浏览次数:10  
标签:lazy return int tr mid 20240927 dfn

Fun is Counting

我们可以发现数组 \(a\) 必须是 \(x\) 或 \(x - 1\),然后分类讨论即可

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 5, mod = 998244353;

int inv[N], f[N], g[N], t, n, a[N];

int C(int a, int b) {
  if (a < b) {
    return 0;
  }
  return f[a] % mod * g[b] % mod * g[a - b] % mod;
}

void Solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  if (a[1] + 1 < a[n]) {
    cout << "0\n";
    return ;
  }
  if (a[1] + 1 == a[n]) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      cnt += (a[i] == a[1]);
    }
    if (a[n] < cnt + 1) {
      cout << "0\n";
      return ;
    }
    cout << C(n, a[n]) * C(a[n], cnt) % mod * C(n - a[n] - 1, (a[n] - cnt) - 1) % mod << "\n";
  }
  else {
    if (a[1] == 1) {
      cout << n + (n == 2) << "\n";
      return ;
    }
    if (a[1] == n - 1) {
      cout << "1\n";
      return ;
    }
    cout << C(n - a[1] - 1, a[1] - 1) * C(n, a[1]) % mod << "\n";
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  inv[1] = f[0] = g[0] = 1;
  for (int i = 1; i <= 1000000; i++) {
    f[i] = f[i - 1] * i % mod;
    if (i > 1) {
      inv[i] = inv[mod % i] * (mod - mod / i) % mod;
    }
    g[i] = g[i - 1] * inv[i] % mod;
  }
  cin >> t;
  while (t--) {
    Solve();
  }
  return 0;
}

Mad MAD Sum II

我们可以画出一个抽象的图,来表示他的值得变化
image
然后就可以想到一些更抽象的操作
image
那么我们就可以根据图所示,更改一段区间的值,有线段树维护即可

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 2e5 + 5, INF = 1e18;

struct node {
  int x, sum, lazy;
}tr[N * 4];

int t, n, a[N];

map<int, int> mp;

node Merge(node x, node y) {
  return {min(x.x, y.x), x.sum + y.sum, INF};
}

void pushdown(int i, int l, int r, int mid) {
  if (tr[i].lazy != INF) {
    tr[i * 2] = {tr[i].lazy, (tr[i].lazy) * (mid - l + 1), tr[i].lazy};
    tr[i * 2 + 1] = {tr[i].lazy, (tr[i].lazy) * (r - mid), tr[i].lazy};
  }
  tr[i].lazy = INF;
}

void modify(int i, int l, int r, int x, int y, int z) {
  if (l > y || r < x) {
    return ;
  }
  if (l >= x && r <= y) {
    tr[i] = {z, z * (r - l + 1), z};
    return ;
  }
  int mid = (l + r) >> 1;
  pushdown(i, l, r, mid);
  modify(i * 2, l, mid, x, y, z);
  modify(i * 2 + 1, mid + 1, r, x, y, z);
  tr[i] = Merge(tr[i * 2], tr[i * 2 + 1]);
}

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

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

int Find(int i, int l, int r, int x) {
  if (l == r) {
    return l;
  }
  int mid = (l + r) >> 1;
  pushdown(i, l, r, mid);
  if (tr[i * 2].x > x) {
    return Find(i * 2 + 1, mid + 1, r, x);
  }
  return Find(i * 2, l, mid, x);
}

void Solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(1, 1, n);
  mp.clear();
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    modify(1, 1, n, i, i, 0);
    int k = Find(1, 1, n, a[i]);
    if (k <= mp[a[i]]) {
      modify(1, 1, n, k, mp[a[i]], a[i]);
    }
    mp[a[i]] = i;
    ans += query(1, 1, n, 1, i);
  }
  cout << ans << "\n";
}

signed main() {
  cin >> t;
  while (t--) {
    Solve();
  }
  return 0;
}

Not An SQRT Problem

说实话,就是更改一下 \(dfs\) 序即可,我们可以先更新儿子节点的 \(dfs\) 序,然后再递归

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 3e5 + 5, INF = 1e18;

struct node {
  int x, lazy;
}tr[N * 4];

int n, q, dfn[N], cnt[N], sz[N], dcnt, first[N];

vector<int> g[N];

void dfs1(int u, int f) {
  sz[u] = 1;
  for (auto v : g[u]) {
    if (v == f) {
      continue;
    }
    dfs1(v, u);
    sz[u] += sz[v];
  }
}

void dfs2(int u, int f) {
  for (auto v : g[u]) {
    if (v == f) {
      continue;
    }
    if (!first[u]) {
      first[u] = v;
    }
    dfn[v] = ++dcnt;
    cnt[u]++;
  }
  for (auto v : g[u]) {
    if (v == f) {
      continue;
    }
    dfs2(v, u);
  }
}

void pushdown(int i) {
  tr[i * 2].x += tr[i].lazy;
  tr[i * 2].lazy += tr[i].lazy;
  tr[i * 2 + 1].x += tr[i].lazy;
  tr[i * 2 + 1].lazy += tr[i].lazy;
  tr[i].lazy = 0;
}

node Merge(node x, node y) {
  return {max(x.x, y.x), 0};
}

void modify(int i, int l, int r, int x, int y, int z) {
  if (l > y || r < x) {
    return ;
  }
  if (l >= x && r <= y) {
    tr[i].x += z;
    tr[i].lazy += z;
    return ;
  }
  int mid = (l + r) >> 1;
  pushdown(i);
  modify(i * 2, l, mid, x, y, z);
  modify(i * 2 + 1, mid + 1, r, x, y, z);
  tr[i] = Merge(tr[i * 2], tr[i * 2 + 1]);
}

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

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 1, u, v; i < n; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1, 0);
  dfn[1] = 1;
  dcnt = 1;
  dfs2(1, 0);
  while (q--) {
    int opt;
    cin >> opt;
    if (opt == 2) {
      int x, v;
      cin >> x >> v;
      int cur = first[x];
      modify(1, 1, n, dfn[cur], dfn[cur] + cnt[x] - 1, v);
    }
    else if (opt == 1) {
      int x, v;
      cin >> x >> v;
      modify(1, 1, n, dfn[x], dfn[x], v);
      if (!g[x].empty()) {
        int cur = first[x];
        modify(1, 1, n, dfn[cur], dfn[cur] + sz[x] - 2, v);
      }
    }
    else if (opt == 3) {
      int x;
      cin >> x;
      int ans = query(1, 1, n, dfn[x], dfn[x]);
      if (!g[x].empty()) {
        int cur = first[x];
        ans = max(ans, query(1, 1, n, dfn[cur], dfn[cur] + sz[x] - 2));
      }
      cout << ans << '\n';
    }
    else {
      int x;
      cin >> x;
      int cur = first[x];
      cout << query(1, 1, n, dfn[cur], dfn[cur] + cnt[x] - 1) << "\n";
    }
  }
  return 0;
}

01 on Tree

和一战到底或者魔塔一模一样

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 2e5 + 5;

struct node {
  int u, a, b;
  bool operator < (const node &_x) const {
    return b * _x.a > _x.b * a;
  }
};

int n, ans, fa[N], f[N], a[N], b[N];

priority_queue<node> q;

int find(int x) {
  if (fa[x] == x) {
    return x;
  }
  return fa[x] = find(fa[x]);
}

signed main() {
  cin >> n;
  for (int i = 2, u; i <= n; i++) {
    cin >> u;
    f[i] = u;
  }
  for (int i = 1, s; i <= n; i++) {
    cin >> s;
    a[i] = (s == 0);
    b[i] = (s == 1);
    q.push({i, (s == 0), (s == 1)});
    fa[i] = i;
  }
  while (!q.empty()) {
    if (q.top().u == 1 || a[q.top().u] != q.top().a || b[q.top().u] != q.top().b) {
      q.pop();
      continue;
    }
    int u = q.top().u, a1 = q.top().a, b1 = q.top().b;
    q.pop();
    int father = find(f[u]);
    ans += b[father] * a1;
    a[father] += a1;
    b[father] += b1;
    fa[u] = f[u];
    q.push({father, a[father], b[father]});
  }
  cout << ans;
  return 0;
}

标签:lazy,return,int,tr,mid,20240927,dfn
From: https://www.cnblogs.com/libohan/p/18436093

相关文章