总结
T1
思路
差分+倍增LCA秒了。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, k, fa[N][30], dep[N], sum[N], u[N], v[N];
vector<int> E[N];
void dfs1(int x, int f) {
dep[x] = dep[f] + 1;
fa[x][0] = f;
for(int i = 1; i <= 20; i++) {
fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i];
if(y == f) {
continue;
}
dfs1(y, x);
}
}
int lca(int x, int y) {
if(dep[x] < dep[y]) {
swap(x, y);
}
for(int i = 20; i >= 0; i--) {
if(dep[fa[x][i]] >= dep[y]) {
x = fa[x][i];
}
}
if(x == y) {
return x;
}
for(int i = 20; i >= 0; i--) {
if(fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
void dfs2(int x, int fa) {
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i];
if(y == fa) {
continue;
}
dfs2(y, x);
sum[x] += sum[y];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++) {
cin >> u[i] >> v[i];
E[u[i]].push_back(v[i]);
E[v[i]].push_back(u[i]);
}
dfs1(1, 0);
cin >> k;
while(k--) {
int a, b;
cin >> a >> b;
sum[a]++;
sum[b]++;
sum[lca(a, b)] -= 2;
}
dfs2(1, 0);
for(int i = 1; i < n; i++) {
cout << sum[dep[u[i]] > dep[v[i]] ? u[i] : v[i]] << " ";
}
cout << "\n";
return 0;
}
T2
思路
因为只有一条出边,所以可以直接倍增。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, k, nxt[N][70], minn[N][70], sum[N][70];
//vector<pair<int, int> > E[N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> nxt[i][0];
nxt[i][0]++;
}
for(int i = 1; i <= n; i++) {
cin >> sum[i][0];
minn[i][0] = sum[i][0];
}
for(int i = 1; i <= 60; i++) {
for(int j = 1; j <= n; j++) {
nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
minn[j][i] = min(minn[j][i - 1], minn[nxt[j][i - 1]][i - 1]);
sum[j][i] = sum[j][i - 1] + sum[nxt[j][i - 1]][i - 1];
}
}
for(int i = 1; i <= n; i++) {
int ans1 = 1e18, ans2 = 0, now = i;
for(int j = 60; j >= 0; j--) {
if((k >> j) & 1) {
ans1 = min(ans1, minn[now][j]);
ans2 += sum[now][j];
now = nxt[now][j];
}
}
cout << ans2 << " " << ans1 << "\n";
}
return 0;
}
T3
思路
先做一遍倍增,求出当包含
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, q, a[N], len[N], vis[N], nxt[N][30], ans[N][30];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
nxt[i][0] = n + 1;
for(int j = 2; j * j <= a[i]; j++) {
if(a[i] % j == 0) {
nxt[vis[j]][0] = min(nxt[vis[j]][0], i);
vis[j] = i;
while(a[i] % j == 0) {
a[i] /= j;
}
}
}
if(a[i] > 1) {
nxt[vis[a[i]]][0] = min(nxt[vis[a[i]]][0], i);
vis[a[i]] = i;
}
}
for(int i = 1; i <= 20; i++) {
for(int j = 1; j + (1 << i) - 1 <= n; j++) {
nxt[j][i] = min(nxt[j][i - 1], nxt[j + (1 << i - 1)][i - 1]);
}
}
for(int i = 1; i <= n; i++) {
int now = 1e9;
ans[i][0] = i;
for(int j = 20; j >= 0; j--) {
if(min(now, nxt[ans[i][0]][j]) >= ans[i][0] + (1 << j)) {
now = min(now, nxt[ans[i][0]][j]);
ans[i][0] += (1 << j);
}
}
}
ans[n + 1][0] = n + 1;
for(int i = 1; i <= 20; i++) {
for(int j = 1; j <= n + 1; j++) {
ans[j][i] = ans[ans[j][i - 1]][i - 1];
}
}
while(q--) {
int l, r;
cin >> l >> r;
int now = l, sum = 1, wyy = 1e9;
for(int j = 20; j >= 0; j--) {
if(min(wyy, nxt[now][j]) >= now + (1 << j)) {
wyy = min(wyy, nxt[now][j]);
now += (1 << j);
}
}
if(now > r) {
cout << sum << "\n";
continue;
}
for(int j = 20; j >= 0; j--) {
if(ans[now][j] <= r) {
now = ans[now][j];
sum += (1 << j);
}
}
sum++;
cout << sum << "\n";
}
return 0;
}
T4
思路
我没看QWQ
对于两个相等的点,只有中间的才可能有可能合法,其余的统统淘汰 踩死两百多个罗老师,然后dfs序+差分即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, a[N], dfn[N], out[N], cnt, ans[N];
map<int, int> sum1, sum2;
vector<int> E[N];
void dfs(int x, int fa) {
dfn[x] = ++cnt;
int now = sum2[a[x]];
sum2[a[x]]++;
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i];
if(y == fa) {
continue;
}
int hke = sum2[a[x]];
dfs(y, x);
if(hke != sum2[a[x]]) {
ans[1]++;
ans[dfn[y]]--;
ans[out[y] + 1]++;
}
}
out[x] = cnt;
if(sum2[a[x]] - now < sum1[a[x]]) {
ans[dfn[x]]++;
ans[out[x] + 1]--;
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
sum1[a[i]]++;
}
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
E[u].push_back(v);
E[v].push_back(u);
}
dfs(1, 0);
int lyt = 0;
for(int i = 1; i <= n; i++) {
ans[i] += ans[i - 1];
if(!ans[i]) {
lyt++;
}
}
cout << lyt << "\n";
return 0;
}