#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#define endl '\n'
#define int long long
#define mod 1000000007
using namespace std;
vector<int>adj[200001];
int g[200001],sub[200001];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,q; cin >> n >> q;
map<pair<int,int>,int>d;
for(int i = 0; i < n - 1; i++){
int a,b,c; cin >> a >> b >> c;
d[{a,b}] = c;
d[{b,a}] = c;
}
for(int i = 1; i <= n; i++) sub[i] = 1e18;
while(q--){
int t; cin >> t;
if(t == 1){
int node; cin >> node;
int ans = sub[node], dist = 0;
while(node > 1){
dist += d[{node / 2, node}];
if(sub[node / 2] != 1e18){
ans = min(ans, sub[node / 2] + dist);
}
node /= 2;
}
if(ans == 1e18) ans = -1;
cout << ans << endl;
}else if(t == 2){
int grape; cin >> grape;
g[grape] = !g[grape];
if(g[grape]){
sub[grape] = 0;
while(grape){
sub[grape / 2] = min(sub[grape / 2], sub[grape] + d[{grape, grape / 2}]);
grape /= 2;
}
}else{
sub[grape] = 1e18;
if(grape * 2 <= n) sub[grape] = sub[grape * 2] + d[{grape, 2 * grape}];
if(grape * 2 + 1 <= n) sub[grape] = min(sub[grape],sub[grape * 2 + 1] + d[{grape * 2 + 1, grape}]);
while(grape /= 2){
sub[grape] = min(sub[grape * 2] + d[{grape * 2, grape}], sub[grape * 2 + 1] + d[{grape * 2 + 1, grape}]);
if(g[grape]) sub[grape] = 0;
}
}
}else{
int a,b,c; cin >> a >> b >> c;
d[{a,b}] = c;
d[{b,a}] = c;
a = min(a,b);
while(a){
sub[a] = min(sub[a * 2] + d[{a * 2, a}], sub[a * 2 + 1] + d[{a * 2 + 1, a}]);
if(g[a]) sub[a] = 0;
a /= 2;
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
5460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
395 ms |
25412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
19236 KB |
Output is correct |
2 |
Correct |
400 ms |
22072 KB |
Output is correct |
3 |
Correct |
474 ms |
21236 KB |
Output is correct |
4 |
Incorrect |
420 ms |
21040 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
520 ms |
28048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
527 ms |
28240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
5460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |