Submission #65874953


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct BIT {
int n;
vector<ll> t;
BIT(int _n): n(_n), t(n+1,0) {}
void add(int i, ll v){ for(; i<=n; i+=i&-i) t[i]+=v; }
ll sum(int i){ ll s=0; for(; i>0; i-=i&-i) s+=t[i]; return s; }
ll sum(int l, int r){ return sum(r) - sum(l-1); }
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin>>N;
vector<vector<pair<int,int>>> g(N+1);
vector<int> U(N), V(N), child(N);
for(int i=1;i<N;i++){
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct BIT {
    int n;
    vector<ll> t;
    BIT(int _n): n(_n), t(n+1,0) {}
    void add(int i, ll v){ for(; i<=n; i+=i&-i) t[i]+=v; }
    ll sum(int i){ ll s=0; for(; i>0; i-=i&-i) s+=t[i]; return s; }
    ll sum(int l, int r){ return sum(r) - sum(l-1); }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin>>N;
    vector<vector<pair<int,int>>> g(N+1);
    vector<int> U(N), V(N), child(N);
    for(int i=1;i<N;i++){
        cin>>U[i]>>V[i];
        g[U[i]].push_back({V[i],i});
        g[V[i]].push_back({U[i],i});
    }

    vector<int> tin(N+1), tout(N+1), parent(N+1);
    int timer = 0;
    function<void(int,int)> dfs = [&](int u, int p){
        tin[u]=++timer;
        parent[u]=p;
        for(auto [v, idx]: g[u]){
            if(v==p) continue;
            dfs(v,u);
            child[idx] = v;
        }
        tout[u]=timer;
    };
    
    dfs(1,0);
    
    BIT bit(N);
    for(int i=1;i<=N;i++){
        bit.add(tin[i],1);
    }
    ll total = N;
    int Q; cin>>Q;
    while(Q--){
        int type; cin>>type;
        if(type==1){
            int x; ll w;
            cin>>x>>w;
            bit.add(tin[x], w);
            total += w;
        } else {
            int y; cin>>y;
            int c = child[y];
            ll s = bit.sum(tin[c], tout[c]);
            ll t = total - s;
            cout << llabs(s - t) << "\n";
        }
    }
    return 0;
}

Submission Info

Submission Time
Task F - Compare Tree Weights
User OYU__0YU
Language C++ 20 (gcc 12.2)
Score 500
Code Size 1550 Byte
Status AC
Exec Time 225 ms
Memory 61744 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 43
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3520 KB
hand_00.txt AC 193 ms 61744 KB
hand_01.txt AC 144 ms 31172 KB
hand_02.txt AC 172 ms 30812 KB
hand_03.txt AC 152 ms 30740 KB
hand_04.txt AC 162 ms 30776 KB
hand_05.txt AC 168 ms 30700 KB
hand_06.txt AC 1 ms 3588 KB
hand_07.txt AC 177 ms 39788 KB
hand_08.txt AC 152 ms 32344 KB
hand_09.txt AC 159 ms 30580 KB
hand_10.txt AC 168 ms 31384 KB
hand_11.txt AC 136 ms 32060 KB
random_00.txt AC 168 ms 35848 KB
random_01.txt AC 173 ms 35796 KB
random_02.txt AC 167 ms 35756 KB
random_03.txt AC 157 ms 35764 KB
random_04.txt AC 175 ms 35768 KB
random_05.txt AC 137 ms 31500 KB
random_06.txt AC 129 ms 31236 KB
random_07.txt AC 128 ms 31536 KB
random_08.txt AC 133 ms 31604 KB
random_09.txt AC 137 ms 31304 KB
random_10.txt AC 158 ms 30844 KB
random_11.txt AC 155 ms 30776 KB
random_12.txt AC 166 ms 30760 KB
random_13.txt AC 158 ms 30728 KB
random_14.txt AC 155 ms 30780 KB
random_15.txt AC 163 ms 30848 KB
random_16.txt AC 196 ms 30776 KB
random_17.txt AC 157 ms 30736 KB
random_18.txt AC 149 ms 30736 KB
random_19.txt AC 148 ms 30776 KB
random_20.txt AC 151 ms 30772 KB
random_21.txt AC 149 ms 30812 KB
random_22.txt AC 161 ms 30856 KB
random_23.txt AC 152 ms 30768 KB
random_24.txt AC 154 ms 30800 KB
random_25.txt AC 158 ms 30784 KB
random_26.txt AC 155 ms 30848 KB
random_27.txt AC 152 ms 30700 KB
random_28.txt AC 225 ms 30728 KB
random_29.txt AC 200 ms 30720 KB


2025-05-17 (Sat)
23:15:08 +09:00