voidmerge(int sm, int la, int val) { for (auto temp : que[sm]) { int v = temp.first; int id = temp.second; if (get_fa(v) == la) ans[id] = val; else que[la].push_back(temp); } fa[sm] = la; // 合并 }
structtruck { int u, v; int val; truck(int a, int b, int c) { u = a; v = b; val = c; } truck() { u = 0; v = 0; val = 0; } };
vector<truck> edge; // 直接存储无向边 vector<pair<int, int>> que[N]; // 保存从N到<first>的第<second>次询问 int fa[N]; // 并查集的父亲代表元素 int ans[300005];
boolcmp(truck a, truck b) { return a.val > b.val; }
intget_fa(int a) { return a == fa[a] ? a : fa[a] = get_fa(fa[a]); }
voidmerge(int sm, int la, int val) { for (auto temp : que[sm]) { int v = temp.first; int id = temp.second; if (get_fa(v) == la) ans[id] = val; else que[la].push_back(temp); } fa[sm] = la; }
intmain() { fastio; freopen("truck.in", "r", stdin); freopen("truck.out", "w", stdout); int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); edge.push_back(truck(u, v, w)); } for (int i = 1; i <= n; i++) fa[i] = i; int q; scanf("%d", &q); for (int i = 1; i <= q; i++) { int u, v; ans[i] = -1; scanf("%d%d", &u, &v); que[u].push_back({v, i}); que[v].push_back({u, i}); } sort(edge.begin(), edge.end(), cmp); for (truck temp : edge) // 从大开始合并 { int sm = get_fa(temp.u); int la = get_fa(temp.v); if (sm == la) continue; if (que[sm].size() > que[la].size()) swap(sm, la); // 启发式合并 merge(sm, la, temp.val); } for (int i = 1; i <= q; i++) printf("%d\n", ans[i]); return0; }
#include<iostream> #include<cstring> #include<cstdio> #define N 1048576 // 注意 2^20 #define ll long long
usingnamespace std;
ll n, ans; ll cnt[N]; int a[N];
intmain() { freopen("xor.in", "r", stdin); freopen("xor.out", "w", stdout); scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int k = 1; k <= n; k++) { for (int l = k + 1; l <= n; l++) { ans += cnt[a[k] ^ a[l]]; } for (int i = 1; i < k; i++) cnt[a[i] ^ a[k]]++; } printf("%lld", ans); return0; }
#include<unordered_map> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define ll long long
usingnamespace std;
constint N = 2000 + 5; int num[N]; int p;
intmain() { freopen("multiplication.in", "r", stdin); freopen("multiplication.out", "w", stdout); scanf("%d", &p); for (int i = 1; i <= p; i++) { int tot = 0; bool zero = true; unordered_map<int, int> hash; for (int j = 1; j <= p; j++) { int a, b; scanf("%d%d", &a, &b); if (a != b) zero = false; if (hash.count(a)) continue; else { tot++; hash[a] = a; } } if (!zero) num[i] = tot; } for (int i = 1; i <= p; i++) printf("%d ", num[i]); return0; }
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<array> #define ll long long
usingnamespace std;
int a[44]; vector<array<int, 2>> le; // left vector<array<int, 2>> ri; // right ll ans; int n;
vector<array<int, 2>> init(int l, int r) { vector<int> temp{0}; // init 0 for (int i = l; i < r; i++) { vector<int> t = temp; // instead dfs for (int x : temp) t.push_back(x + a[i]); temp = t; } // 第一位是我们取出来的,第二位则是我们保存的这个数原有的值 vector<array<int, 2>> ret; // first save now. second save old for (int x : temp) ret.push_back({0, x}); return ret; }
voidreorder(vector<array<int, 2>> &str, int pw) { vector<array<int, 2>> v[10]; for (auto [_, x] : str) { // 这里我们重新取出pw + 1位并用第一个数字排序 int head = (x / pw) % 10; // str_head -> |*^****...| int val = x % (pw * 10); // str_val -> (int)|*****...| v[head].push_back({val, x}); } str.clear(); // remove for (int i = 0; i <= 9; i++) for (auto temp : v[i]) str.push_back(temp); }
ll calc(int d) { // 假设都符合 int r = ri.size(); // if all ll temp = 0; for (int l = 0; l < le.size(); l++) { // cmp now > d ? check->NO : check->YES // 我们相加判断一下这个是否符合要求 while (r > 0 && ri[r - 1][0] + le[l][0] > d) r--; // cmp fail -> r-- -> find other temp += r; } return temp; }
intmain() { freopen("four.in", "r", stdin); freopen("four.out", "w", stdout); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); le = init(0, n / 2); // 20 -> 2^20 ri = init(n / 2, n); // 20 -> 2^20