QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328668#2918. Tree Number Generatorngungu46WA 6ms11488kbC++144.2kb2024-02-15 23:08:492024-02-15 23:08:50

Judging History

你现在查看的是最新测评结果

  • [2024-02-15 23:08:50]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:11488kb
  • [2024-02-15 23:08:49]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <sstream>
#include <complex>
#include <ctime>
#include <cassert>
#include <functional>

#define REP(i,s,t) for(i=(s);i<(t);i++);
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<long long, long long> pi;
typedef vector<vector<ll> > mat;
typedef priority_queue<ll, vector<ll>, greater<ll> > minpq;
const ll inf = 1ll << 60;
const ll mod = 1e9 + 7;
const long long maxn = 300030;
const double eps = 1e-9;

long long n, m, q;
vector<vector<long long> > adj(maxn);
long long timer;
vector<long long> tin;
vector<long long> tout;
long long l;
vector<vector<long long> > up;
vector<vector<long long> > resup;
vector<vector<long long> > resdown;
vector<long long> digits;
vector<long long> pow10(30000);

long long log2(long long x){
    long long j = 0;
    for(j=0; (1<<j)<x; j++);

    return j;
}

long long pow(long long x, long long p){
    if (p == 0) return 1;
    if (p%2 == 1){
        return (x*pow(x, p-1))%m;
    }
    long long half = pow(x, (long long) (p/2));
    return (half*half)%m;
}

void dfs(long long v, long long p){
    tin[v] = ++timer;

    up[v][0] = p;
    resup[v][0] = digits[v]%m;
    resdown[v][0] = digits[v]%m;
    for(long long i = 1; i <= l; i++){
        up[v][i] = up[up[v][i-1]][i-1];
        resup[v][i] = (resup[v][i-1]*pow10[1<<(i-1)])%m;
        resup[v][i] = (resup[v][i]+resup[up[v][i-1]][i-1])%m;
        resdown[v][i] = (resdown[up[v][i-1]][i-1]*pow10[1<<(i-1)])%m;
        resdown[v][i] = (resdown[v][i]+resdown[v][i-1])%m;
        // cout << "yolo " << v << " " << p << " " << i << " " << resup[v][i] << " " << resdown[v][i] <<  endl;
    }
    for(long long u: adj[v]){
        if (u!=p){
            dfs(u,v);
        }
    }

    tout[v] = ++timer;
}

bool is_ancestor(long long u, long long v){
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

long long lca(long long u, long long v){
    if(is_ancestor(u, v)) return u;
    if(is_ancestor(v, u))  return v;
    for(long long i = l; i >= 0; i--){
        if(!is_ancestor(up[u][i], v)){
            u = up[u][i];
        }
    }

    return up[u][0];
}

long long query(long long u, long long v){
    long long ca = lca(u, v);
    long long res = 0;

    for(long long i = l; i >= 0; i--){
        if(!is_ancestor(up[u][i], ca)){
            long long x = resup[u][i];
            res = (res*pow10[1<<i])%m;
            res = (res+x)%m;
            u = up[u][i];
        }
    }
    u!=ca? res = (res*10+digits[u])%m: res=res;
    long long power = 0;
    long long res2 = 0;
    for(long long i = l; i >= 0; i--){
        if(!is_ancestor(up[v][i], ca)){
            long long x = resdown[v][i];
            x = (x*pow10[power])%m;
            res2 = (res2+x)%m;
            v = up[v][i];
            power += 1<<i;
        }
    }

    // cout << "left: " << res << endl;
    // cout << "right: " << res2 << endl;
    long long new_x;
    v!=ca? new_x = resdown[v][1]%m: new_x = digits[v]%m;
    new_x = (new_x*pow10[power])%m;
    res2 = (res2+new_x)%m;
    v!=ca? res = (res*pow10[power+2])%m: res = (res*pow10[power+1])%m;
    res  = (res+res2)%m;
    return res;
}

int main(){
    cin >> n >> m >> q;
    long long x, y;
    long long i;
    for(long long i = 0; i < n-1; i++){
        cin >> x >> y;
        adj[x-1].push_back(y-1);
        adj[y-1].push_back(x-1);
    }

    digits.assign(n, 0);
    for(long long i = 0; i <n; i++){
        cin >> digits[i];
    }

    timer = 1;
    tin.resize(n);
    tout.resize(n);

    l = log2(n);
    up.assign(n, vector<long long> (l+1,0));
    for(long long i = 0; i <= n; i++){
        pow10[i] = pow(10, i);
    }
    resup.assign(n, vector<long long>(l+1,0));
    resdown.assign(n, vector<long long>(l+1,0));

    dfs(0, 0);
    // cout << "done build" << endl;
    while(q--){
        long long a, b;
        cin >> a >> b;
        a--, b--;
        cout << query(a, b) << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 10560kb

input:

2 1 4
1 2
1
3
1 2
2 1
1 1
2 2

output:

0
0
0
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 10728kb

input:

3 10 5
1 2
2 3
0
0
0
1 3
3 1
2 2
2 3
2 1

output:

0
0
0
0
0

result:

ok 5 lines

Test #3:

score: -100
Wrong Answer
time: 6ms
memory: 11488kb

input:

2000 2 2000
937 1471
1672 937
356 1672
976 356
1257 356
1503 1257
783 1503
1783 937
1284 976
1955 1503
802 1257
583 1471
526 356
701 1783
393 1955
307 1955
386 1955
1070 937
1724 802
1397 1724
1140 937
422 526
1941 1955
1638 937
1469 526
1800 526
1035 1800
1009 1140
1195 1800
142 1471
1225 1469
1524...

output:

0
1
1
1
0
0
1
0
0
1
0
1
0
0
1
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
1
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
1
0
0
0
1
1
0
1
0
0
0
0
0
1
0
1
0
0
0
0
0
1
1
1
1
1
0
0
1
1
1
0
1
1
1
0
0
1
1
1
1
0
0
0
1
0
1
0
0
1
1
1
0
1
1
0
1
0
1
0
0
1
0
1
0
0
1
1
0
1
1
1
1
0
0
0
1
0
0
0
0
1
1
0
1
1
0
1
1
0
1
1
0
0
0
1
1
0
0
1
...

result:

wrong answer 67th lines differ - expected: '1', found: '0'