This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 <= 2 * n + 1; i++) sub[i] = 1e18;
while(q--){
int t; cin >> t;
if(t == 1){
int node; cin >> node;
int ans = 1e18, dist = 0;
while(node > 0){
if(sub[node] != 1e18) ans = min(ans, sub[node] + dist);
dist += d[{node / 2, node}];
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]){
while(grape > 0){
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;
grape /= 2;
}
}else{
sub[grape] = 1e18;
while(grape > 0){
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;
grape /= 2;
}
}
}else{
int a,b,c; cin >> a >> b >> c;
d[{a,b}] = c;
d[{b,a}] = c;
a = min(a,b);
while(a > 0){
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |