F题 哈希+容斥 复杂度
#include <stdio.h>
#include <algorithm>
#define int long long
using namespace std;
const int MAX = 1e5 + 10;
int n, a[MAX], h[MAX], cnt[MAX], cnt2[MAX];
signed main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld", a + i);
h[a[i]]++;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= 1e5; i++)
{
for (int j = 1; i * j <= 1e5; j++)
{
cnt[i] += h[i * j];
cnt2[i * j] += h[i];
}
}
int tmp = 0;
for (int i = 1; i <= 1e5; i++)
{
tmp += cnt[i] * h[i];
}
int ans = tmp * tmp;
for (int i = 1; i <= 1e5; i++)
{
ans -= cnt[i] * cnt[i] * h[i];
ans -= cnt2[i] * cnt2[i] * h[i];
ans -= 2 * cnt[i] * cnt2[i] * h[i];
ans -= 2 * n * cnt[i] * h[i];
ans += h[i] * h[i];
}
ans += 8 * tmp;
ans += tmp;
ans += n * n;
ans -= 6 * n;
printf("%lld", ans);
}
G题 LCA+求出根节点到每个节点各种颜色的数量 复杂度
#include <stdio.h>
#include <vector>
using namespace std;
const int MAX = 1e5 + 10;
int n, q, c[MAX], head[MAX], dep[MAX], fa[MAX], fir[MAX], cnt, vis[MAX], sz[MAX], num[MAX];
vector<int> sons[MAX], sum[MAX];
struct Edge
{
int to, nxt;
} E[MAX << 1];
void build(int s)
{
sz[s] = vis[s] = 1;
sum[s][c[s]]++;
for (int i = fir[s]; i; i = E[i].nxt)
{
int to = E[i].to;
if (vis[to])
continue;
sons[s].push_back(to);
fa[to] = s;
for (int i = 0; i <= 20; i++)
{
sum[to][i] = sum[s][i];
}
build(to);
sz[s] += sz[to];
}
}
void dfs(int s)
{
if (sons[s].empty())
return;
int max = 0, maxson;
for (int son : sons[s])
{
dep[son] = 0, head[son] = son, num[son] = num[s] + 1;
if (sz[son] > max)
{
maxson = son, max = sz[son];
}
}
head[maxson] = head[s], dep[maxson] = dep[s] + 1, num[maxson] = num[s];
for (int son : sons[s])
{
dfs(son);
}
}
int lca(int x, int y)
{
if (head[x] == head[y])
return dep[x] < dep[y] ? x : y;
return num[x] < num[y] ? lca(x, fa[head[y]]) : lca(fa[head[x]], y);
}
int main()
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
{
scanf("%d", c + i);
sum[i].resize(21);
}
for (int i = 0; i < n - 1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
E[++cnt] = {v, fir[u]}, fir[u] = cnt;
E[++cnt] = {u, fir[v]}, fir[v] = cnt;
}
head[1] = 1;
build(1), dfs(1);
while (q--)
{
int s, t;
scanf("%d%d", &s, &t);
int l = lca(s, t), ans[21] = {}, cnt = 0;
for (int i = 0; i <= 20; i++)
{
ans[i] += sum[s][i] - sum[l][i];
ans[i] += sum[t][i] - sum[l][i];
}
ans[c[l]]++;
for (int i = 0; i <= 20; i++)
{
if (ans[i])
cnt++;
}
printf("%d\n", cnt);
}
}
H题 贪心+线段树维护最大值和次大值 复杂度
#include <stdio.h>
#include <algorithm>
#include <memory.h>
using namespace std;
#define MAX 100010
struct p
{
int fn, fv, sn, sv;
} t[MAX << 2];
int n, k, a[MAX], ans[MAX];
p max(const p &p1, const p &p2)
{
p res;
if (p1.fv > p2.fv)
{
res.fv = p1.fv, res.fn = p1.fn;
if (p1.sv > p2.fv)
res.sv = p1.sv, res.sn = p1.sn;
else if (p1.sv < p2.fv)
res.sv = p2.fv, res.sn = p2.fn;
else
res.sv = p1.sv, res.sn = min(p1.sn, p2.fn);
}
else if (p1.fv < p2.fv)
{
res.fv = p2.fv, res.fn = p2.fn;
if (p2.sv > p1.fv)
res.sv = p2.sv, res.sn = p2.sn;
else if (p2.sv < p1.fv)
res.sv = p1.fv, res.sn = p1.fn;
else
res.sv = p1.fv, res.sn = min(p1.fn, p2.sn);
}
else
{
res.fv = p1.fv, res.fn = min(p1.fn,p2.fn);
if (p1.sv > p2.sv)
res.sv = p1.sv, res.sn = p1.sn;
else if (p1.sv < p2.sv)
res.sv = p2.sv, res.sn = p2.sn;
else
res.sv = p1.sv, res.sn = min(p1.sn, p2.sn);
}
return res;
}
void pushup(int m)
{
t[m] = max(t[m << 1], t[m << 1 | 1]);
}
void build(int l, int r, int m)
{
if (l == r)
{
t[m] = {l, a[l]};
return;
}
int mid = (l + r) >> 1;
build(l, mid, m << 1), build(mid + 1, r, m << 1 | 1);
pushup(m);
}
p query(int L, int R, int l, int r, int m)
{
if (L <= l && R >= r)
return t[m];
p ans = {};
int mid = (l + r) >> 1;
if (L <= mid)
ans = max(ans, query(L, R, l, mid, m << 1));
if (R > mid)
ans = max(ans, query(L, R, mid + 1, r, m << 1 | 1));
return ans;
}
void update(int x, int l, int r, int m)
{
if (l == r)
{
t[m] = {};
return;
}
int mid = (l + r) >> 1;
if (x <= mid)
update(x, l, mid, m << 1);
else
update(x, mid + 1, r, m << 1 | 1);
pushup(m);
}
int main()
{
memset(ans, 255, sizeof ans);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
{
scanf("%d", a + i);
}
build(1, n, 1);
for (int i = 1; i <= n; i++)
{
p tmp = query(i, min(i + k, n), 1, n, 1);
if (tmp.fv == ans[i - 1])
{
if (tmp.sv)
{
ans[i] = tmp.sv, k -= tmp.sn - i;
update(tmp.sn, 1, n, 1);
}
}
else if (tmp.fv)
{
ans[i] = tmp.fv, k -= tmp.fn - i;
update(tmp.fn, 1, n, 1);
}
printf("%d ", ans[i]);
}
}