版本比较
传代码
正文
增加 164 行删除 26 行
145 行未改动全部展开14614714814915015015115215315486 行未改动2412422432442452452462472482482492502512522532542552562572582582592602612627 行未改动27027127227327427427527627727827928028028128228328417 行未改动30230330430530630630730830830931031131231331431431531631731831931932032132232322 行未改动34634734834935035035135235335335435535635635735835936036136236236336436536636736836937037137137237337437530 行未改动4064074084094104104114124134144154154164174184184194204214224234244254264274284284294304314329 行未改动44244344444544644644744844945045145145245345345445545645745845945946046146246315 行未改动47948048148248348348448548648721 行未改动509510511512513513514515516516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658145 行未改动全部展开
return 0;}```# 树套树解[狗狗](https://www.luogu.com.cn/problem/P1533) # 树套树解[狗狗](https://www.luogu.com.cn/problem/P1533)(Ds代码,会[TLE和MLE](https://www.luogu.com.cn/record/243558478))```cpp#include <iostream>#include <cstdio>86 行未改动 struct SegNode { Node* root; SegNode() : root(nullptr) {} } tree[MAXN * 4];
int n; vector<int> arr;
void build(int p, int l, int r) { for (int i = l; i <= r; i++) { insert(tree[p].root, arr[i]); } if (l == r) return; int mid = (l + r) / 2; build(p * 2, l, mid); build(p * 2 + 1, mid + 1, r); }
// 查询区间内小于等于 x 的数的个数 int queryRank(int p, int l, int r, int ql, int qr, int x) { if (ql <= l && r <= qr) { // 在平衡树中查询小于等于 x 的数的个数7 行未改动 if (ql <= mid) res += queryRank(p * 2, l, mid, ql, qr, x); if (qr > mid) res += queryRank(p * 2 + 1, mid + 1, r, ql, qr, x); return res; }
public: SegmentTree(const vector<int>& a) : arr(a) { n = a.size() - 1; build(1, 1, n); }
// 查询区间第 k 小值 int queryKth(int ql, int qr, int k) { // 二分答案 int left = 0, right = 1e9, ans = 0;17 行未改动 vector<int> a(n + 1); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); }
SegmentTree segTree(a);
while (m--) { int i, j, k; scanf("%d%d%d", &i, &j, &k); printf("%d\n", segTree.queryKth(i, j, k)); }
return 0;}```
# Splay # Splayds,CE
```cpp#include<bits/stdc++.h>22 行未改动void rotate(int i, bool d) { // d=0: 左旋, d=1: 右旋 int f = fth[i], gf = fth[f]; pushdown(i); pushdown(f);
son[f][!d] = son[i][d]; if (son[i][d]) fth[son[i][d]] = f;
son[i][d] = f; fth[f] = i;
fth[i] = gf; if (gf) { if (son[gf][0] == f) son[gf][0] = i; else son[gf][1] = i; }
update(f); update(i);}
void splay(int i, int goal) { while (fth[i] != goal) { int f = fth[i], gf = fth[f]; pushdown(gf); pushdown(f); pushdown(i);
if (gf == goal) { rotate(i, son[f][0] == i); } else { bool d1 = (son[f][0] == i);30 行未改动int build(int l, int r, int fa) { if (l > r) return 0; int mid = (l + r) >> 1; int i = ++cnt;
fth[i] = fa; val[i] = cp[mid]; siz[i] = 1; laz[i] = 0;
son[i][0] = build(l, mid - 1, i); son[i][1] = build(mid + 1, r, i);
update(i); return i;}
void reverse(int l, int r) { int L = findkth(l - 1); int R = findkth(r + 1); splay(L, 0); splay(R, L);
int pos = son[R][0]; laz[pos] ^= 1;}
9 行未改动
int main() { int n, m; cin >> n >> m;
// 添加哨兵节点 cp[1] = 0; // 左哨兵 for (int i = 1; i <= n; i++) cp[i + 1] = i; cp[n + 2] = n + 1; // 右哨兵
rot = build(1, n + 2, 0);
while (m--) { int l, r; cin >> l >> r; reverse(l + 1, r + 1); // 因为有两个哨兵 }
dfs(rot); return 0;}```15 行未改动int n,m;
int cl[maxn];bool flg=1;void dfs(int u,int c,int p) { cl[u]=c; cl[u]=c; for(int i=hed[u];i;i=nxt[i]) { if(dii[i]==p) continue; if(!flg) return; int v=too[i];21 行未改动 for(int u,v,i=1; i<=m; i++) { cin>>u>>v; add(u,v,i);add(v,u,i); }
for(int i=1; i<=m; i++) { if(ck(i)){ans[++ant]=i;} } } cout<<ant<<'\n'; for(int i=1;i<=ant;i++) cout<<ans[i]<<' ';}```# 2025 CSP-S T1```#include<iostream>#include<algorithm>using namespace std;
struct people{ int a,b,c; int fri, friid, sec, secid, dis;} peo[100010];
bool cmp(people a, people b){ return a.dis > b.dis;}
int main(){ ios::sync_with_stdio(false); cin.tie(0);
int t; cin >> t; while(t--) { int n, _n, size[3] = {0}, ans = 0; cin >> n; _n = n / 2;
for(int i = 0; i < n; i++) { cin >> peo[i].a >> peo[i].b >> peo[i].c;
// 更简洁的写法确定最优和次优 if(peo[i].a >= peo[i].b && peo[i].a >= peo[i].c) { peo[i].fri = peo[i].a; peo[i].friid = 0; peo[i].sec = max(peo[i].b, peo[i].c); peo[i].secid = (peo[i].b >= peo[i].c) ? 1 : 2; } else if(peo[i].b >= peo[i].a && peo[i].b >= peo[i].c) { peo[i].fri = peo[i].b; peo[i].friid = 1; peo[i].sec = max(peo[i].a, peo[i].c); peo[i].secid = (peo[i].a >= peo[i].c) ? 0 : 2; } else { peo[i].fri = peo[i].c; peo[i].friid = 2; peo[i].sec = max(peo[i].a, peo[i].b); peo[i].secid = (peo[i].a >= peo[i].b) ? 0 : 1; } peo[i].dis = peo[i].fri - peo[i].sec; }
sort(peo, peo + n, cmp);
for(int i = 0; i < n; i++) { if(size[peo[i].friid] < _n) { ans += peo[i].fri; size[peo[i].friid]++; } else { ans += peo[i].sec; size[peo[i].secid]++; // 这里应该也要记录次优部门的人数 } } cout << ans << "\n"; } return 0;}```
# dij
```cpp#include <bits/stdc++.h>
using namespace std;
#define maxn 100040#define INF INT_MAX
struct nd { int v, w;};vector<nd> e[maxn];
int n, m, s;
int dis[maxn];bool vis[maxn];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
void djstr(int s) { for (int i = 1; i <= n; i++) dis[i] = INF; dis[s] = 0; pq.push({0, s}); while (!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if (vis[u]) continue; vis[u] = 1; for (auto j : e[u]) { if (!vis[j.v] && dis[u] + j.w < dis[j.v]) { dis[j.v] = dis[u] + j.w; pq.push({dis[j.v], j.v}); } } }}
signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s; for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; e[u].push_back({v, w}); } djstr(s); for (int i = 1; i <= n; i++) cout << dis[i] << ' '; return 0;}```