QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#97531#2918. Tree Number Generatormegz112233WA 242ms80452kbC++204.1kb2023-04-17 05:39:422023-04-17 05:39:46

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-17 05:39:46]
  • 评测
  • 测评结果:WA
  • 用时:242ms
  • 内存:80452kb
  • [2023-04-17 05:39:42]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define all(v)  v.begin(),v.end()
#define sz(x)  (int) (x).size()
#define el '\n'
#define Megz ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
int mod = 1e9 + 7, N = 1e6+5;
int mul  (int x , int y ){
    x%=mod  ;
    y%=mod  ;
    return 1ll* (x*y)%mod;
}
int plu  (int x , int y ){
    x%=mod  ;
    y%=mod  ;
    return 1ll* (x+y)%mod;
}
struct vertex {
    int anc ,  up  ,  down   ;
};
struct lca {
    vector<vector<vertex>>  yup_lifting ;
    vector<int> depth   ;
    vector<int> adj[400005] ;
    vector<int> val  ;
    vector<int> pow  ;
    const  int   LOG  =  19 ;
    void init (int n ){
        depth.assign(n+5, 0) ;
        val.assign(n+5,0) ;
        yup_lifting.assign(n+5,  vector<vertex>(LOG, {0,0,0})) ;
        pow.push_back(1) ;
        for (int i =0 ;  i<= (int) 1e7+5 ; i++){
            pow.push_back(mul(pow.back(),10)) ;
        }
    }
    void dfs (int node , int par ){
        yup_lifting[1][0] =  {par, val[1],  val[1]};
        for (auto child  :  adj[node]){
            if (child == par)continue;
            depth[child] =  depth[node]+1 ;
            yup_lifting[child][0] =  {node, val[child],  val[child]};
            for (int i =1 ; i < LOG ;i++){
                yup_lifting[child][i].anc =  yup_lifting[yup_lifting[child][i-1].anc][i-1].anc ;
                yup_lifting[child][i].up = plu(mul (yup_lifting[child][i-1].up , pow[(1<<i-1)]) , yup_lifting[yup_lifting[child][i-1].anc][i-1].up ) ;
                yup_lifting[child][i].down = plu(mul  (yup_lifting[yup_lifting[child][i-1].anc][i-1].down ,  pow[(1<<i-1)]) , yup_lifting[child][i-1].down)  ;
            }
            dfs(child, node) ;
        }
    }
    vertex go_up (int k  ,  int  node  ){
        vertex curr =  {node , 0,0} ;
        ll cnt = 0 ;
        for (int i =0 ; i < LOG  ; i++){
            if (k&(1<<i)){
                curr.up = plu(mul(curr.up , pow[(1<<i)]) , yup_lifting[curr.anc][i].up) ;
                curr.down = plu(mul  (yup_lifting[curr.anc][i].down , pow[cnt]) , curr.down)  ;
                cnt+=(1<<i) ;
                curr.anc = yup_lifting[curr.anc][i].anc ;
            }
        }
        return curr ;
    }
    int get_lca (int a ,  int b){
        if (depth[a]<depth[b])swap(a,b) ;
        int k =  depth[a]-depth[b] ;
        a  = go_up(k ,  a).anc ;
        if (a==b){
            return a  ;
        }
        for (int i = LOG -1  ; i>=0  ; i--){
            if (yup_lifting[a][i].anc!=yup_lifting[b][i].anc){
                a = yup_lifting[a][i].anc;
                b = yup_lifting[b][i].anc ;
            }
        }
        return yup_lifting[a][0].anc ;
    }
    int get_query (int from ,int to){
        int lca  = get_lca(from ,  to ) ;
        if (lca==from){
            vertex  ans  = go_up(depth[to]-depth[from]+1 ,  to) ;
            return ans.down ;
        }
        else if (lca==to){

            vertex  ans  = go_up(depth[from]-depth[to]+1 ,  from) ;
            return ans.up ;
        }
        else {
            int left  = go_up(depth[from]-depth[lca]+1 , from).up , right  = go_up((depth[to]-depth[lca]) , to).down ;
           // cout <<left <<" " <<right<<" "<<depth[lca]<<" " <<depth[to]<<" "  <<lca  <<el ;
            return plu(mul(left , pow[( depth[to]-depth[lca])] ),  right) ;
        }
    }


} lca;
void acc() {
    int n , q ;cin >>n>>mod >>q ;
    lca.init(n+5) ;
    for (int  i =0 ; i < n-1; i++){
        int x , y ;cin>>x>>y ;
        lca.adj[x].push_back(y) ;
        lca.adj[y].push_back(x) ;
    }

    for (int i =1 ; i<=n ; i++){
        cin >>lca.val[i] ;
        lca.val[i]%=mod ;
    }
    lca.dfs(1, 1) ;
    //cout <<lca.get_lca(4,2) <<"hahaahah"<< el ;
    while (q--){
        int from ,  to ;cin >>from >>to ;
        cout <<lca.get_query(from  ,to)<<el ;
    }
}

int main() {
    Megz
    int t = 1;
    while (t--) {
        acc();
    }

}


//5 100 4
//1 2
//1 3
//1 4
//5 3
//1
//2
//3
//0
//4
//1 5
//5 1
//4 2
//3 3
// ananananananana 5aaaaaawlwlwwllwlwl

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 218ms
memory: 78876kb

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: 226ms
memory: 80024kb

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: 0
Accepted
time: 242ms
memory: 79408kb

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
1
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:

ok 2000 lines

Test #4:

score: 0
Accepted
time: 237ms
memory: 79404kb

input:

2000 1000 2000
1664 1028
763 1664
1541 1028
1544 1664
69 1544
1473 1541
475 1541
1551 1541
579 1473
262 475
1777 475
1916 475
391 763
807 1028
1357 1028
1682 69
345 1544
234 1541
63 391
480 807
1762 1544
306 1916
1436 1777
891 391
1852 306
523 1852
264 475
313 306
1139 391
1792 69
1604 69
398 313
10...

output:

263
429
976
56
31
940
168
28
487
658
273
218
944
664
498
705
709
490
61
931
421
664
632
538
876
282
145
61
430
984
589
436
780
641
69
126
625
208
629
603
566
57
355
843
705
781
514
898
804
290
366
642
429
899
716
466
371
620
252
606
690
500
412
226
495
380
61
580
805
132
423
845
618
862
924
729
637
...

result:

ok 2000 lines

Test #5:

score: -100
Wrong Answer
time: 224ms
memory: 80452kb

input:

2000 1000000 2000
676 154
686 154
357 154
187 676
299 686
1508 357
1515 1508
972 686
105 357
748 1515
1711 686
1692 154
1869 299
1017 187
829 1017
809 1515
1505 676
383 1515
1002 972
1448 829
1657 1515
1824 1508
1271 1711
545 1515
1099 748
1255 748
556 545
388 1017
1290 357
992 1824
66 1017
1812 972...

output:

911946
658749
941314
251735
177727
241911
734055
143108
-365424
899637
173402
264918
304122
632775
991506
402910
517268
914587
379978
462220
622382
658742
329239
43729
56506
192359
541795
221056
866374
142798
124989
947854
413121
510933
602943
488815
292373
183960
602947
199025
301900
603305
260397
...

result:

wrong answer 5th lines differ - expected: '719871', found: '177727'