版本比较
板子
正文
增加 370 行删除 0 行
94 行未改动9596979894 行未改动work();return 0;}```94 行未改动959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746894 行未改动work();return 0;}```## 离散化```cppint Q (int x) {return lower_bound(ls + 1, ls + tot + 1, x) - ls;}void lsh () {sort(ls + 1, ls + 1 + tot);tot = unique(ls + 1, ls + tot + 1) - ls -1;}```### 组合数```cppint jc[N],inv[N];int Pow(int a, int b = mod - 2) {int x = 1;while (b) {if (b & 1) x = x * a % mod;a = a * a % mod;b >>= 1;}return x;}void init(int n) {inv[0] = jc[0] = 1;for (int i = 1; i <= n; i++) jc[i] = jc[i-1] * i % mod;int num = Pow(jc[n]);for (int i = n; i >= 1; i--)inv[i] = num * jc[i-1] % mod, (num *= i) %= mod;for (int i = 1; i <= n; i++) (inv[i] *= inv[i-1]) %= mod;}int C(int n, int m) {if (n < m) return 0;return jc[n] * inv[m] % mod * inv[n-m] % mod;}```### 虚树```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 1e5 + 5;int dfn[N],dep[N],anc[N][21],Time;int stk[N],tp,x,y,T,m,n,q;vector<int> d[N],c[N],k;void link(int x, int y) {d[x].push_back(y);}bool cmp(int x, int y){return dfn[x] < dfn[y];}void dfs (int u, int fa) {dfn[u] = ++Time, dep[u] = dep[fa] + 1, anc[u][0] = fa;for (int i = 1; (1 << i) <= dep[u]; i++)anc[u][i] = anc[anc[u][i-1]][i-1];for (auto v:c[u]) if (v != fa)dfs(v, u);}int lca (int x,int y){if (dep[x] < dep[y]) swap(x,y);for (int i = 20; i >= 0; i--)if (dep[anc[x][i]] >= dep[y])x = anc[x][i];if (x == y) return x;for (int i = 20; i >= 0; i--)if (anc[x][i] != anc[y][i])x = anc[x][i], y = anc[y][i];return anc[x][0];}void work(){tp = 0;sort(k.begin(), k.end(), cmp);stk[++tp] = 1;for (auto v:k) {int lc = lca(stk[tp], v);if(lc != stk[tp]) {while(dep[lc] < dep[stk[tp-1]]) {link(stk[tp-1], stk[tp]);tp--;}link(lc, stk[tp]);tp--;if(stk[tp] != lc)stk[++tp] = lc;}stk[++tp] = v;}while (tp > 1) {link(stk[tp-1], stk[tp]);tp--;}}signed main(){cin >> n;for(int i = 1; i < n; i++) {cin >> x >> y;c[x].push_back(y);c[y].push_back(x);}dfs(1, 0);cin >> q;while (q--) {cin >> m;for (int i = 1; i <= m; i++) {cin >> x;k.push_back(x);}work();}return 0;}```## 最短路```cppstruct node {int x,w;bool operator < (const node a) const {return w > a.w;}};int dis[N],vis[N],n,m,x,y,w,s;vector<node> c[N];priority_queue<node> q;void Dij (int s) {memset(dis, 0x3f, sizeof(dis));dis[s] = 0;q.push({1, 0});while (q.size()) {int u = q.top().x;q.pop();if (vis[u]) continue;vis[u] = 1;for (auto i:c[u]) {int v = i.x, w = i.w;if (dis[v] > dis[u] + w) {dis[v] = dis[u] + w;q.push({v, dis[v]});}}}}```## 随机排列```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 5;mt19937 rnd(time(0));int n,a[N];void rdp (int a[], int n) {for (int i = 1; i <= n; i++) a[i] = i;shuffle(a + 1, a + 1 + n, rnd);}signed main () {return 0;}```## ST表```cpp#include<bits/stdc++.h>using namespace std;const int N = 1e6+5;inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}int n,m,st[20][N],a[N],l,r;int get (int l, int r) {int k = log2(r - l + 1);return max(st[k][l], st[k][r-(1<<k)+1]);}int main(){n = read(); m = read();for (int i = 1; i <= n; i++) {a[i] = read();st[0][i] = a[i];}for (int i = 1; (1 << i) <= n; i++)for(int j = 1; j + (1 << i) - 1 <= n; j++)st[i][j] = max(st[i-1][j], st[i-1][j+(1<<i-1)]);for (int i = 1; i <= m; i++) {l = read(), r = read();cout << get(l, r) << '\n';}return 0;}```## 点分治```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 1e6 + 5, inf = 1e18;bool ok[N];int to[N],wei[N],head[N],nxt[N],tot;int zon[N],siz[N],root,dxn,dp[N];int ans = inf,m,w,n,x,y,k;struct node {int x,y;};vector<node> st,stk;void add (int x, int y, int w){to[++tot] = y, wei[tot] = w;nxt[tot] = head[x], head[x] = tot;}void get_weight (int u, int fa){siz[u] = 1, zon[u] = 0;for(int i = head[u]; i; i = nxt[i]) {int v = to[i];if (v == fa || ok[v]) continue;get_weight(v, u);siz[u] += siz[v];zon[u] = max(zon[u], siz[v]);}zon[u] = max(zon[u], dxn - siz[u]);if(zon[u] < zon[root])root = u;}void dfs(int u,int fa){siz[u] = 1;for (int i = head[u]; i; i = nxt[i]) {int v = to[i];if (v == fa || ok[v]) continue;dfs(v, u);siz[u] += siz[v];}}void get (int u, int fa, int dep, int dis) {if (dis <= k) stk.push_back({dis, dep});for (int i = head[u]; i; i = nxt[i]) {int v = to[i], w = wei[i];if(v == fa || ok[v]) continue;get(v, u, dep + 1, dis + w);}}void cale(int u){root = 0;get_weight(u, 0);ok[u = root] = 1;dfs(u, 0);dp[0] = 0;st.clear();for (int i = head[u]; i; i = nxt[i]) {int v = to[i], w = wei[i];if (ok[v]) continue;stk.clear();get(v, u, 1, w);for (auto v:stk)ans = min(ans, v.y + dp[k - v.x]);for (auto v:stk) {dp[v.x] = min(dp[v.x], v.y);st.push_back(v);}}for (auto v:st) dp[v.x] = inf;for (int i = head[u]; i; i = nxt[i]){int v = to[i];if (ok[v]) continue;dxn = siz[v];cale(v);}}signed main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> k;if (k == 0) {cout << 0 ;return 0;}for (int i = 1; i < n; i++) {cin >> x >> y >> w;x++, y++;add(x, y, w);add(y, x, w);}zon[0] = 114514;dxn = n;for (int i = 1; i <= k; i++) dp[i] = inf;cale(1);cout << (ans == inf? -1: ans);return 0;}``````cpp#include<bits/stdc++.h>#define mid (L + R >> 1)#define int long longusing namespace std;const int N = 2e5 + 5;struct node{int a,b,c,id,w;}c[N];int n,ans[N],t[N],k;bool cmp1 (node a, node b) {if (a.a == b.a) { // a b cif (a.b == b.b)return a.c < b.c;return a.b < b.b;}return a.a < b.a;}bool cmp2 (node a, node b) { // b cif (a.b == b.b)return a.c < b.c;return a.b < b.b;}namespace Bit {int t[N];int res (int x) {int ans = 0;for(;x > 0; x -= x & -x)ans += t[x];return ans;}void add (int x, int w) {for (; x <= k; x += x & -x)t[x] += w;}}void CDQ (int L, int R){if (L == R) return ;int p = L;CDQ(L, mid), CDQ(mid+1,R);sort(c + L, c + mid + 1, cmp2);sort(c + mid + 1, c + R + 1, cmp2) ;for (int i = mid + 1; i <= R; i++) {while (p <= mid && c[p].b <= c[i].b)Bit::add(c[p].c, c[p].w), p++;ans[c[i].id] += Bit::res(c[i].c);}for (int i = L; i < p; i++)Bit::add(c[i].c, -c[i].w);}signed main () {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> k;for (int i = 1; i <= n; i++) {cin >> c[i].a >> c[i].b >> c[i].c;c[i].id = i;}sort(c + 1, c + 1 + n, cmp1);int tot = 0;for (int i = 1; i <= n; i++) {if (c[i].a != c[i-1].a || c[i].c != c[i-1].c || c[i].b != c[i-1].b)c[++tot] = c[i];c[tot].w++;}CDQ(1, tot);for (int i = 1; i <= tot; i++)t[ans[c[i].id] + c[i].w - 1] += c[i].w;for (int i = 0; i < n; i++)cout << t[i] << '\n';return 0;}## CDQ```