QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#96404 | #2918. Tree Number Generator | zezoo050# | WA | 11ms | 17704kb | C++20 | 3.0kb | 2023-04-13 21:00:36 | 2023-04-13 21:02:47 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
const int N = 2e5 + 10, LOG = 21;
int m10[(1 << LOG) + 10];
int m;
ll mul(ll x, ll y)
{
return ((x % m) * (y % m)) % m;
}
ll plu(ll x, ll y)
{
return ((x % m) + (y % m)) % m;
}
void pre()
{
m10[0] = 1;
for (int i = 1; i < N; i++)
{
m10[i] = mul(m10[i - 1], 10);
}
return;
}
vector<int> adj[N];
int par[N][LOG];
int arr[N];
ll up[N][LOG];
ll down[N][LOG];
int in[N], out[N];
// u is par of v
bool ispar(int u, int v)
{
return in[v] >= in[u] and out[v] <= out[u];
}
int timer = 0;
void dfs(int node, int p)
{
in[node] = timer++;
par[node][0] = p;
up[node][0] = arr[node];
down[node][0] = arr[node];
for (int i = 1; i < LOG; i++)
{
par[node][i] = par[par[node][i - 1]][i - 1];
up[node][i] = plu(mul(up[node][i - 1], m10[1 << (i - 1)]), up[par[node][i - 1]][i - 1]);
down[node][i] = plu(mul(down[par[node][i - 1]][i - 1], m10[1 << (i - 1)]), down[node][i - 1]);
}
for (auto i : adj[node])
{
if (i == p)
continue;
dfs(i, node);
}
out[node] = timer++;
}
int lca(int u, int v)
{
if (ispar(u, v))
return u;
if (ispar(v, u))
return v;
for (int i = LOG - 1; i >= 0; i--)
{
if (ispar(par[u][i], v))
continue;
u = par[u][i];
}
return par[u][0];
}
int n, q;
ll ans(int u, int v)
{
if (u == v)
return arr[u];
int lc = lca(u, v);
ll a = 0, b = 0;
for (int i = LOG - 1; i >= 0; i--)
{
if (ispar(par[u][i], lc))
continue;
a = plu(mul(a, m10[1 << i]), up[u][i]);
u = par[u][i];
}
int cnt = 0;
for (int i = LOG - 1; i >= 0; i--)
{
if (ispar(par[v][i], lc))
continue;
b = plu(mul(down[v][i], m10[cnt]), b);
v = par[v][i];
cnt += 1 << i;
}
a = plu(mul(a,10),arr[u]);
if (u == lc)
{
b = plu(mul(arr[v],10),b);
cnt++;
return plu(mul(a,m10[cnt]),b);
}
else if (v == lc)
{
return plu(mul(a, 10), arr[lc]);
}
else
{
b = plu(mul(arr[v],m10[cnt]),b);
cnt++;
a = plu(mul(a, 10), arr[lc]);
a = plu(mul(a, m10[cnt]), b);
}
return a;
}
void solve()
{
cin >> n >> m >> q;
pre();
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
}
dfs(1, 1);
// cout<<down[2][1]<<endl;
while (q--)
{
int u, v;
cin >> u >> v;
cout << ans(u, v) << endl;
}
}
int main()
{
fast;
int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 11ms
memory: 17704kb
input:
2 1 4 1 2 1 3 1 2 2 1 1 1 2 2
output:
0 0 1 3
result:
wrong answer 3rd lines differ - expected: '0', found: '1'