Accoders 6.6 NOI模拟
perm
所有循环大小之和是n,种类数是根号级别。lcm可以类似莫队
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 5e5 + 5;
const int kS = 1e6 + 5;
const int kB = 1500;
const int lim = 1e6;
const int P = 1e9 + 7;
int T, n, a[kN];
int m, tot, sz[kN], cnt[kS];
bool vis[kN];
int p[kS], pc, inv[kS], big[kS];
bool np[kS];
std::vector<int> has;
void init() {
np[1] = true;
big[1] = 1;
For(i, 2, lim) {
if (!np[i]) p[pc++] = i, big[i] = i;
for (int j = 0; j < pc && i * p[j] <= lim; ++j) {
np[i * p[j]] = true;
big[i * p[j]] = big[i];
if (i % p[j] == 0) break;
}
}
inv[1] = 1;
For(i, 2, lim) inv[i] = P - (ll)inv[P % i] * (P / i) % P;
}
int rem[kS][22], mxe[kS], prod;
std::vector<int> tmp;
void add(int x) {
while (x > 1) {
int t = big[x], te = 0;
while (x % t == 0) x /= t, ++te;
For(i, 1, mxe[t]) prod = (ll)prod * inv[t] % P;
++rem[t][te];
mxe[t] = 0;
rFor(i, 20, 0) if (rem[t][i]) {
mxe[t] = i;
break;
}
For(i, 1, mxe[t]) prod = (ll)prod * t % P;
}
}
void del(int x) {
while (x > 1) {
int t = big[x], te = 0;
while (x % t == 0) x /= t, ++te;
For(i, 1, mxe[t]) prod = (ll)prod * inv[t] % P;
--rem[t][te];
mxe[t] = 0;
rFor(i, 20, 0) if (rem[t][i]) {
mxe[t] = i;
break;
}
For(i, 1, mxe[t]) prod = (ll)prod * t % P;
}
}
const int BS = 1e7;
char ibuf[BS], *i1 = ibuf, *i2 = ibuf;
#define gc() (i1 == i2 && (i2 = ibuf + fread(i1 = ibuf, 1, BS, stdin), i1 == i2) ? EOF : *i1++)
int read() {
int x = 0;
char c = gc();
while (!isdigit(c)) c = gc();
while (isdigit(c)) x = x * 10 + c - '0', c = gc();
return x;
}
int main() {
freopen("perm.in", "r", stdin);
freopen("perm.out", "w", stdout);
init();
T = read();
while (T--) {
n = read();
For(i, 1, n) a[i] = read();
memset(vis, false, sizeof(vis));
has.clear();
memset(cnt, 0, sizeof(cnt));
tot = 0;
For(i, 1, n) if (!vis[i]) {
int x = i;
sz[++tot] = 0;
while (!vis[x]) {
++sz[tot];
vis[x] = true;
x = a[x];
}
if (!cnt[sz[tot]]) has.push_back(sz[tot]);
++cnt[sz[tot]];
}
memset(rem, 0, sizeof(rem));
memset(mxe, 0, sizeof(mxe));
prod = 1;
int ans = 0;
for (int x : has) add(x);
for (int i = 0; i < has.size(); ++i) {
if (cnt[has[i]] == 1) del(has[i]);
if (cnt[has[i]] >= 2) {
if (cnt[has[i]] == 2) del(has[i]);
int tprod = prod;
int x = has[i] * 2;
while (x > 1) {
int t = big[x], te = 0;
while (x % t == 0) x /= t, ++te;
For(i, 1, te - mxe[t]) prod = (ll)prod * t % P;
}
ans = (ans + (ll)cnt[has[i]] * (cnt[has[i]] - 1) / 2 % P * has[i] % P * has[i] % P * prod) % P;
prod = tprod;
if (cnt[has[i]] == 2) add(has[i]);
}
for (int j = i + 1; j < has.size(); ++j) {
if (cnt[has[j]] == 1) del(has[j]);
int tprod = prod;
int x = has[i] + has[j];
while (x > 1) {
int t = big[x], te = 0;
while (x % t == 0) x /= t, ++te;
For(i, 1, te - mxe[t]) prod = (ll)prod * t % P;
}
ans = (ans + (ll)cnt[has[i]] * cnt[has[j]] % P * has[i] % P * has[j] % P * prod) % P;
prod = tprod;
if (cnt[has[j]] == 1) add(has[j]);
}
if (cnt[has[i]] == 1) add(has[i]);
}
printf("%d\n", ans * 2 % P);
}
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */
keys
枚举每种颜色建虚树,以该颜色所有钥匙为根dfs虚树,对矩形算贡献,复杂度
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC target("avx","sse")
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 5e5 + 5;
const int kM = 1e6 + 5;
int n, m, tp[kN], c[kN], f[kN][20], dep[kN], dfn[kN], s[kN], clk;
std::vector<int> G[kN];
void dfs(int u) {
dfn[u] = ++clk;
s[u] = 1;
for (int i = 0; f[u][i]; ++i) f[u][i + 1] = f[f[u][i]][i];
for (int v : G[u]) if (v != f[u][0]) {
f[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
s[u] += s[v];
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) std::swap(u, v);
int delt = dep[u] - dep[v];
rFor(i, 19, 0) if (delt >> i & 1) u = f[u][i];
if (u == v) return u;
rFor(i, 19, 0) if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
return f[u][0];
}
int adj(int u, int v) {
int delt = dep[u] - dep[v] - 1;
For(i, 0, 19) if (delt >> i & 1) u = f[u][i];
return u;
}
struct BIT {
int c[kN];
inline int lowbit(int x) {
return x & -x;
}
void add(int l, int r, int v) {
if (l > r) return;
for (; l <= n; l += lowbit(l)) c[l] += v;
for (++r; r <= n; r += lowbit(r)) c[r] -= v;
}
int query(int x) {
int res = 0;
for (; x; x -= lowbit(x)) res += c[x];
return res;
}
} tr;
std::vector<pii> add[kN], del[kN], qry[kN];
std::vector<int> key[kN], val[kN];
int head[kN], ecnt = 1;
struct Arc {
int next, to;
} e[kN << 1];
void addEdge(int from, int to) {
// fprintf(stderr, "%d -> %d\n", from, to);
e[ecnt] = (Arc) {
head[from], to
};
head[from] = ecnt++;
}
double tb = 0;
void build(int id) {
// double tim = clock();
static int st[kN], ord[kN];
int top = 0, tot = 0;
ecnt = 1;
for (int u : key[id]) if (u != 1) ord[++tot] = u, head[u] = 0;
for (int u : val[id]) if (u != 1) ord[++tot] = u, head[u] = 0;
std::sort(ord + 1, ord + tot + 1, [](int i, int j) {
return dfn[i] < dfn[j];
});
head[1] = 0;
st[++top] = 1;
For(i, 1, tot) {
int p = lca(ord[i], st[top]);
while (p != st[top]) {
if (dep[p] > dep[st[top - 1]]) {
head[p] = 0;
addEdge(p, st[top]), addEdge(st[top], p);
--top;
st[++top] = p;
}
else {
addEdge(st[top], st[top - 1]), addEdge(st[top - 1], st[top]);
--top;
p = lca(ord[i], st[top]);
}
}
st[++top] = ord[i];
}
while (top > 1) {
addEdge(st[top], st[top - 1]), addEdge(st[top - 1], st[top]);
--top;
}
// tb += clock() - tim;
}
void addRec(int u, int v) {
// fprintf(stderr, "add %d -> %d\n", u, v);
int near;
if (dfn[u] < dfn[v] && dfn[v] < dfn[u] + s[u]) {
add[1].emplace_back(dfn[v], dfn[v] + s[v] - 1);
near = adj(v, u);
del[dfn[near]].emplace_back(dfn[v], dfn[v] + s[v] - 1);
add[dfn[near] + s[near]].emplace_back(dfn[v], dfn[v] + s[v] - 1);
} else if (dfn[v] < dfn[u] && dfn[u] < dfn[v] + s[v]) {
near = adj(u, v);
add[dfn[u]].emplace_back(1, dfn[near] - 1);
add[dfn[u]].emplace_back(dfn[near] + s[near], n);
del[dfn[u] + s[u]].emplace_back(1, dfn[near] - 1);
del[dfn[u] + s[u]].emplace_back(dfn[near] + s[near], n);
} else {
add[dfn[u]].emplace_back(dfn[v], dfn[v] + s[v] - 1);
del[dfn[u] + s[u]].emplace_back(dfn[v], dfn[v] + s[v] - 1);
}
}
void dfs1(int u, int sum, int fa, int rt) {
// fprintf(stderr, "dfs %d %d %d %d\n", u, sum, fa, rt);
if (c[rt] == c[u] && tp[u] == 1) ++sum;
else if (c[rt] == c[u]) --sum;
if (sum == 0) {
addRec(rt, u);
return;
}
eFor(i, u, v) if (v != fa) dfs1(v, sum, u, rt);
}
double tw = 0;
void work(int id) {
// double tim = clock();
build(id);
for (int u : key[id]) dfs1(u, 0, 0, u);
// tw += clock() - tim;
}
int ans[kM];
void solve() {
// double tim = clock();
For(i, 1, n) {
for (auto x : add[i]) tr.add(x.fi, x.se, 1);
for (auto x : del[i]) tr.add(x.fi, x.se, -1);
for (auto x : qry[i]) ans[x.se] = tr.query(x.fi);
}
For(i, 1, m) printf("%d\n", ans[i]);
// fprintf(stderr, "solve: %lf\n", (clock() - tim) / CLOCKS_PER_SEC);
}
const int BS = 1e7;
char ibuf[BS], *i1 = ibuf, *i2 = ibuf;
#define gc() (i1 == i2 && (i2 = ibuf + fread(i1 = ibuf, 1, BS, stdin), i1 == i2) ? EOF : *i1++)
int read() {
int x = 0;
char c = gc();
while (!isdigit(c)) c = gc();
while (isdigit(c)) x = x * 10 + c - '0', c = gc();
return x;
}
int main() {
// freopen("in", "r", stdin);
freopen("keys.in", "r", stdin);
freopen("keys.out", "w", stdout);
n = read(), m = read();
For(i, 1, n) {
tp[i] = read(), c[i] = read();
if (tp[i] == 1) key[c[i]].push_back(i);
else val[c[i]].push_back(i);
}
// double tim = clock();
For(i, 1, n - 1) {
int u, v;
u = read(), v = read();
G[u].push_back(v), G[v].push_back(u);
}
dfs(1);
For(i, 1, m) {
int s, t;
s = read(), t = read();
if (s == t)
continue;
qry[dfn[s]].emplace_back(dfn[t], i);
}
For(i, 1, n) if (!key[i].empty()) work(i);
// fprintf(stderr, "build: %lf\n", tb / CLOCKS_PER_SEC);
// fprintf(stderr, "work: %lf\n", tw / CLOCKS_PER_SEC);
solve();
// fprintf(stderr, "tmain: %lf\n", (clock() - tim) / CLOCKS_PER_SEC);
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */