QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#97511#2918. Tree Number GeneratorYoussefmorsi1RE 8ms14908kbC++203.2kb2023-04-17 01:52:212023-04-17 01:52:24

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 01:52:24]
  • 评测
  • 测评结果:RE
  • 用时:8ms
  • 内存:14908kb
  • [2023-04-17 01:52:21]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define el '\n'
#define Morsi ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key ------> return index
// find_by_order -----> use index


const int mod = 1e9 + 7, N = 1e5 + 10, SQ = 150;
int n, m, q, ancestor[N][20], sparseDown[N][20], sparseUp[N][20], in[N], out[N], timer = 0;
int pw[N];
vector<int> adj[N];

int mul(long long x, long long y){
    return (x * y) % m;
}

int add(long long x, long long y){
    return (x + y) % m;
}

int Concatenate(int x, int y, int length){
    x = mul(x , pw[length]);
    x = add(x , y);
    return x;
}

void dfs(int node, int parent){
    in[node] = timer++;
    ancestor[node][0] = parent;

    //compute ancestor and sparse
    for(int i = 1; i <= 18; i++){
        int prev = ancestor[node][i - 1];
        ancestor[node][i] = ancestor[prev][i - 1];
        sparseUp[node][i] = Concatenate(sparseUp[node][i - 1], sparseUp[prev][i - 1], (1 << (i - 1)));
        sparseDown[node][i] = Concatenate(sparseDown[prev][i - 1], sparseDown[node][i - 1], (1 << (i - 1)));
    }

    for(auto i : adj[node])
        if(i != parent)
            dfs(i, node);

    out[node] = timer++;
}

bool Subtree(int parent, int child){
    return in[parent] <= in[child] && out[parent] >= out[child];
}

int LCA(int u, int v){
    if(Subtree(u, v))
        return u;

    if(Subtree(v, u))
        return v;

    for(int i = 18; i >= 0; i--)
        if(!Subtree(ancestor[u][i], v))
            u = ancestor[u][i];
    
    return ancestor[u][0];
}

int Get(int a, int b){
    int lca = LCA(a, b);

    int ret = 0;

    for(int i = 18; i >= 0; i--)
        if(!Subtree(ancestor[a][i], lca)){
            ret = Concatenate(ret, sparseUp[a][i], (1 << i));
            a = ancestor[a][i];
        }
    if(a != lca)
      ret = Concatenate(ret, sparseUp[a][0], 1);

    stack<pair<int, int>> st;
    for(int i = 18; i >= 0; i--)
        if(!Subtree(ancestor[b][i], lca)){
            st.push({sparseDown[b][i], (1 << i)});
            b = ancestor[b][i];
        }
    if(b != lca)
        st.push({sparseDown[b][0], 1});

    ret = Concatenate(ret, sparseUp[lca][0], 1);
    
    while(!st.empty()){
        ret = Concatenate(ret, st.top().first, st.top().second);
        st.pop();
    }

    return ret;
}

void alg() {
    cin >> n >> m >> q;
    pw[0] = 1 % m;

    for(int i = 1; i < N; i++)
        pw[i] = mul(pw[i - 1], 10);

    for(int i = 0; i < n - 1; 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 >> sparseDown[i][0];
        sparseUp[i][0] = sparseDown[i][0];
    }

    dfs(1, 1);
    while(q--){
        int a, b;
        cin >> a >> b;
        cout << Get(a, b) << el;
    }
}

int main()
{
    Morsi
    int t = 1;
    while(t--){
        alg();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 12856kb

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: 6ms
memory: 11052kb

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: 0ms
memory: 14908kb

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: 8ms
memory: 11728kb

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: 0
Accepted
time: 4ms
memory: 11648kb

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
719871
241911
734055
143108
569168
899637
173402
264918
271418
632775
991506
402910
517268
914587
379978
462220
622382
658742
329239
43729
56506
192359
410979
57536
866374
142798
124989
947854
413121
183893
602943
488815
292373
183960
602947
199025
301900
603305
260397
64...

result:

ok 2000 lines

Test #6:

score: 0
Accepted
time: 6ms
memory: 11356kb

input:

2000 1000000000 2000
352 747
534 747
294 534
1548 534
1051 534
1850 747
574 1850
43 1548
1395 1850
1615 1051
1236 294
1869 43
1039 1051
288 294
299 747
217 1395
1220 1051
622 1850
673 574
1296 288
1331 1296
721 1039
1062 1615
518 721
1276 1296
1806 1276
1731 1869
477 574
461 1051
1703 622
358 1703
1...

output:

79101007
887079978
189124001
400049688
284
43253539
818072051
53354038
9042495
272009043
500270860
700462128
335009411
350090556
872000107
46209971
707883989
53354276
271083550
900288054
2682651
818832941
27070294
70720415
513017327
230170729
271033998
200904807
27104651
347046254
900272592
70720094...

result:

ok 2000 lines

Test #7:

score: -100
Runtime Error

input:

200000 2417 200000
146436 54121
82979 146436
129255 146436
99397 146436
148744 129255
125174 99397
55826 129255
13345 55826
131204 146436
59873 148744
148074 55826
23402 125174
24052 146436
93765 99397
177707 131204
33986 59873
51616 23402
144922 55826
92135 82979
63586 148744
45845 33986
199631 148...

output:


result: