UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196976#3441. 路径Zeardoe1008536ms135372kbC++113.0kb2023-11-04 12:08:252023-11-04 13:05:17

answer

/*
[templates]: 
duipai
spjdp
compre
addhis
floor_sum
treedfs
matrix
network_flow
polynomial
lca
bitset
valuesgt
fenwick
erbitree
*/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include<bits/stdc++.h>
using namespace std;
#define int long long
//use ll instead of int.
#define f(i, a, b) for(int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(),i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e18;
//#define cerr if(false)cerr
//#define freopen if(false)freopen
mt19937 rng(time(0)); 
int rnd(int l, int r) {return rng() % (r-l+1) + l; }
#define watch(x) cerr  << (#x) << ' '<<'i'<<'s'<<' ' << x << endl
void pofe(int number, int bitnum) {
    string s; f(i, 0, bitnum) {s += char(number & 1) + '0'; number >>= 1; } 
    reverse(s.begin(), s.end()); cerr << s << endl; 
    return;
}
template <typename TYP> void cmax(TYP &x, TYP y) {if(x < y) x = y;}
template <typename TYP> void cmin(TYP &x, TYP y) {if(x > y) x = y;}
//调不出来给我对拍!
//use std::array.
const int N = 200000; vector<int> t[N + 10]; int a[N + 10]; 
int anc[22][N + 10], dep[N + 10]; 
struct qj {
    int mx, mn, ans; 
}node[22][N + 10], e; 
qj merge(qj x, qj y) {
    return {max(x.mx, y.mx), min(x.mn, y.mn), max({x.ans, y.ans, y.mx - x.mn})}; 
}
// void print(qj x) {
    // cerr << "mn = " << x.mn << ", mx = " << x.mx << ", ans = " << x.ans << endl; 
// }
void dfs(int now, int fa) {
    anc[0][now] = fa; node[0][now] = {a[now], a[now], 0}; dep[now] = dep[fa] + 1; 
    for(int i = 1; i <= __lg(dep[now]); i ++) {
        anc[i][now] = anc[i - 1][anc[i - 1][now]]; 
        node[i][now] = merge(node[i - 1][now], node[i - 1][anc[i - 1][now]]); 
        // cerr << "node[" << i << "][" << now << "]: \n"; 
        // print(node[i][now]); 
    }
    for(int i : t[now]) {
        if(i != fa) {
            dfs(i, now); 
        }
    }
}
qj getmin(int now, int k) {
    qj res = e; 
    int c = 0; 
    while(k) {
        if(k & 1) {
            res = merge(res, node[c][now]); now = anc[c][now]; 
        }
        c ++; 
        k >>= 1; 
    }
    // cerr << res.ans << " " << res.mx << " " << res.mn << endl; 
    // cerr << "min = " << res << endl; 
    return res; 
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen();
    //freopen();
    //time_t start = clock();
    //think twice,code once.
    //think once,debug forever.
    int n, q; cin >> n >> q; e.mx = -inf, e.mn = inf, e.ans = 0; 
    f(i, 1, n - 1) {
        int u, v; cin >> u >> v; 
        t[u].push_back(v); t[v].push_back(u); 
    }
    f(i, 1, n) cin >> a[i]; 
    dfs(1, 0);
    f(i, 1, q) {
        int l, r; cin >> l >> r; 
        int k = dep[l] - dep[r] + 1; 
        // cerr << k << endl; 
        cout << getmin(l, k).ans << endl; 
    }
    //time_t finish = clock();
    //cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 5988kb

input:

9 6
1 2
1 3
2 4
4 5
1 6
5 7
4 8
6 9
3 5 3 1 3 6 10 8 6
5 2
9 1
9 1
6 6
4 4
8 8

output:

4
0
0
0
0
0

result:

ok 6 lines

Test #2:

score: 5
Accepted
time: 0ms
memory: 5984kb

input:

10 10
1 2
2 3
2 4
4 5
5 6
6 7
3 8
1 9
8 10
7 4 4 8 8 4 10 6 1 2
8 3
4 1
7 5
9 9
6 4
10 10
10 3
3 1
1...

output:

0
3
4
0
4
0
4
3
0
0

result:

ok 10 lines

Test #3:

score: 5
Accepted
time: 0ms
memory: 6172kb

input:

994 991
1 2
1 3
2 4
4 5
4 6
3 7
2 8
1 9
4 10
1 11
6 12
6 13
5 14
14 15
4 16
14 17
17 18
2 19
7 20
20...

output:

0
76
61
0
55
0
65
28
60
81
24
0
41
0
73
0
23
35
58
7
0
0
22
0
91
60
82
71
60
60
41
68
70
0
20
0
0
60...

result:

ok 991 lines

Test #4:

score: 5
Accepted
time: 0ms
memory: 6176kb

input:

997 994
1 2
1 3
3 4
2 5
2 6
4 7
1 8
5 9
1 10
5 11
7 12
4 13
13 14
5 15
5 16
11 17
7 18
9 19
18 20
3 ...

output:

936455963
0
803375903
588789236
913091021
0
0
684383890
245669194
600498485
674412258
308855627
3654...

result:

ok 994 lines

Test #5:

score: 5
Accepted
time: 3ms
memory: 6184kb

input:

995 991
1 2
2 3
3 4
2 5
5 6
6 7
7 8
7 9
8 10
5 11
4 12
9 13
6 14
5 15
15 16
8 17
4 18
2 19
15 20
18 ...

output:

72176025
48853349
70299185
88661370
87049799
15849234
72703417
93734580
62320986
48853349
61235577
4...

result:

ok 991 lines

Test #6:

score: 5
Accepted
time: 681ms
memory: 135348kb

input:

199965 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

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

result:

ok 500000 lines

Test #7:

score: 5
Accepted
time: 695ms
memory: 135312kb

input:

199908 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

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

result:

ok 500000 lines

Test #8:

score: 5
Accepted
time: 702ms
memory: 135372kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

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

result:

ok 500000 lines

Test #9:

score: 5
Accepted
time: 688ms
memory: 135312kb

input:

199904 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

999977286
996605780
999977286
999622957
999676473
999949729
999981864
999972427
999944922
999860383
...

result:

ok 500000 lines

Test #10:

score: 5
Accepted
time: 763ms
memory: 135368kb

input:

199997 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

999931525
999491336
999980026
999980026
999927984
999892075
999968575
999974219
999931525
999909809
...

result:

ok 500000 lines

Test #11:

score: 5
Accepted
time: 693ms
memory: 135368kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

499961752
499996063
499992026
499955230
499914810
499992026
499992026
499991276
499991276
499991276
...

result:

ok 500000 lines

Test #12:

score: 5
Accepted
time: 662ms
memory: 135368kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 500000 lines

Test #13:

score: 5
Accepted
time: 159ms
memory: 63112kb

input:

100000 100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100000 lines

Test #14:

score: 5
Accepted
time: 210ms
memory: 57200kb

input:

90000 100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100000 lines

Test #15:

score: 5
Accepted
time: 284ms
memory: 63112kb

input:

100000 99997
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 99997 lines

Test #16:

score: 5
Accepted
time: 309ms
memory: 63120kb

input:

100000 100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

980141843
721394611
962653038
980141843
980141843
980141843
721394611
900188017
944620554
0
94462055...

result:

ok 100000 lines

Test #17:

score: 5
Accepted
time: 671ms
memory: 126360kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

999932770
999932770
999932770
999491751
999932770
999932770
999929872
999751134
999929872
999932770
...

result:

ok 500000 lines

Test #18:

score: 5
Accepted
time: 699ms
memory: 126364kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

999839397
999912566
999701804
998588309
999681031
999912566
998235075
999912566
999864609
999864609
...

result:

ok 500000 lines

Test #19:

score: 5
Accepted
time: 650ms
memory: 126360kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

999944553
999921999
999921999
999944553
996578852
999944553
999944553
999793801
999819961
999924992
...

result:

ok 500000 lines

Test #20:

score: 5
Accepted
time: 667ms
memory: 126356kb

input:

200000 500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
1...

output:

999934844
999937704
999934844
999929931
999839169
999934844
999934844
999934844
999929931
999993948
...

result:

ok 500000 lines

Extra Test:

score: 0
Extra Test Passed