QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566951#5148. Tree DistanceCharizard2021WA 1535ms107592kbC++143.4kb2024-09-16 04:10:182024-09-16 04:10:18

Judging History

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

  • [2024-09-16 04:10:18]
  • 评测
  • 测评结果:WA
  • 用时:1535ms
  • 内存:107592kb
  • [2024-09-16 04:10:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > adj;
int n, q;
vector<int> bit;
vector<pair<int, int> > order;
vector<bool> visited;
vector<int> sz;
vector<int> maxSz;
vector<vector<pair<int, int> > > values;
void dfs(int u, int p, int cur, int& curRoot){
	sz[u] = 1;
    maxSz[u] = 0;
    for(pair<int, int> v : adj[u]){
        if(v.first != p && !visited[v.first]){
            dfs(v.first, u, cur, curRoot);
            maxSz[u] = max(maxSz[u], sz[v.first]);
            sz[u] += sz[v.first];
        }
    }
    maxSz[u] = max(maxSz[u], cur - sz[u]);
    if(curRoot == 0){
        curRoot = u;
    }
    if(maxSz[u] < maxSz[curRoot]){
        curRoot = u;
    }
}
void update(int x, int val){
	while(x <= n){
        bit[x] = min(bit[x], val);
        x += x & -x;
    }
}
int query(int x){
	int ans = INT_MAX;
	while(x != 0){
        ans = min(ans, bit[x]);
        x -= x & -x;
    }
	return ans;
}
void dfs2(int u, int p, int sum){
    order.push_back(make_pair(u, sum));
    for(pair<int, int> v : adj[u]){
        if(v.first != p && !visited[v.first]){
            dfs2(v.first, u, sum + v.second);
        }
    }
}
void dfs3(int u){
    int curRoot = 0;
    dfs(u, 0, sz[u], curRoot);
    u = curRoot;
    visited[u] = true;
    order.clear();
    dfs2(u, 0, 0);
    sort(order.begin(), order.end());
    stack<int> curStack;
    for(int i = 0; i < (int)order.size(); i++){
        while(!curStack.empty() && order[i].second < order[curStack.top()].second){
            curStack.pop();
        }
        if(!curStack.empty()){
            values[order[i].first].push_back(make_pair(order[curStack.top()].first, order[i].second + order[curStack.top()].second));
        }
        curStack.push(i);
    }
    while(!curStack.empty()){
        curStack.pop();
    }
    for(int i = (int)order.size() - 1; i >= 0; i--){
        while(!curStack.empty() && order[i].second < order[curStack.top()].second){
            curStack.pop();
        }
        if(!curStack.empty()){
            values[order[curStack.top()].first].push_back(make_pair(order[i].first, order[i].second + order[curStack.top()].second));
        }
        curStack.push(i);
    }
    for(pair<int, int> v : adj[u]){
        if(!visited[v.first]){
            dfs3(v.first);
        }
    }
}
int main(){
    cin >> n;
    adj.resize(1 + n);
    bit.resize(1 + n);
    visited.resize(1 + n);
    sz.resize(1 + n);
    maxSz.resize(1 + n);
    values.resize(1 + n);
    for(int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back(make_pair(v, w));
        adj[v].push_back(make_pair(u, w));
    }
    for(int i = 1; i <= n; i++){
        bit[i] = INT_MAX;
    }
    sz[1] = n;
    dfs3(1);
    cin >> q;
    vector<pair<int, int> > queries[1 + n];
    int ans[q];
    for(int i = 0; i < q; i++){
        int l, r;
        cin >> l >> r;
        queries[r].push_back(make_pair(l, i));
    }
    for(int i = 1; i <= n; i++){
        for(pair<int, int> x : values[i]){
            update(n - x.first + 1, x.second);
        }
		for(pair<int, int> x : queries[i]){
			if(x.first == i){
                ans[x.second]=-1;
            }
			else{
                ans[x.second] = query(n - x.first + 1);
            }
		}
	}
    for(int i = 0; i < q; i++){
        cout << ans[i] << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

-1
3
7
7
2

result:

ok 5 number(s): "-1 3 7 7 2"

Test #2:

score: -100
Wrong Answer
time: 1535ms
memory: 107592kb

input:

199999
31581 23211 322548833
176307 196803 690953895
34430 82902 340232856
36716 77480 466375266
7512 88480 197594480
95680 61864 679567992
19572 14126 599247796
188006 110716 817477802
160165 184035 722372640
23173 188594 490365246
54801 56250 304741654
10103 45884 643490340
127469 154479 214399361...

output:

-2147410668
-1677423355
-2147483271
-2147481188
-2147483152
-2146539605
-2147481721
-2147475862
-2147163976
-2140071841
-2147219038
-2147483152
-2147481721
-2147476385
-2147483152
-2147415440
-2147458654
-2144090692
-2147467897
-2147483152
-2147481721
-2147483152
-2147481721
-2147304290
-2147469271
...

result:

wrong answer 1st numbers differ - expected: '29573323', found: '-2147410668'