1 solutions

  • 0
    @ 2024-10-03 23:31:36
    #include <bits/stdc++.h>
    using namespace std;
    #define SC               scanf
    #define PF               printf
    #define ull              unsigned long long
    #define ld               long double
    #define F                first
    #define S                second
    #define pb               push_back
    #define sort_a(a)        sort(a.begin(),a.end());
    #define sort_d(a)        sort(a.rbegin(),a.rend());
    #define READ(f)          freopen(f, "r", stdin)
    #define WRITE(f)         freopen(f, "w", stdout)
    #define rev(s)           reverse(s.begin(),s.end())
    #define P(ok)            cout << (ok ? "YES\n": "NO\n")
    #define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
    #define ll long long
    typedef pair< ll , ll>                   PII;
    const int sz = 2e5 + 5 ;
    int Light[sz] , st[sz] , en[sz] , a[sz] , Time ;
    vector < int > Edge[sz] ;
    void dfs(int id , int par) {
    
       st[id] = ++Time ;
       Light[Time] = a[id] ;
       for(auto child : Edge[id]){
        if(child != par) dfs(child , id) ;
       }
       en[id] = Time ;
    
    }
    
    struct info {
        int prop, sum;
    } Tree[sz * 3];
    void init(int node, int b, int e) {
        if (b == e) {
            Tree[node].sum = Light[b];
            return;
        }
    
        int Left = node << 1;
        int Right = (node << 1) + 1;
        int Mid = (b + e) >> 1;
    
        init(Left, b, Mid);
        init(Right, Mid + 1, e);
        Tree[node].sum = Tree[Left].sum + Tree[Right].sum;
    }
    
    void Update(int node, int b, int e, int i, int j) {
        if (i > e || j < b) return;
        if (b >= i && e <= j) {
            Tree[node].sum = ((e - b + 1)) - Tree[node].sum;
            Tree[node].prop ^= 1;
            return;
        }
    
        int Left = node << 1;
        int Right = (node << 1) + 1;
        int Mid = (b + e) >> 1;
    
        if( Tree[node].prop) {
            Update(Left, b, Mid, b, Mid);
            Update(Right, Mid + 1, e, Mid + 1, e);
            Tree[node].prop = 0;
            Tree[node].sum = Tree[Left].sum + Tree[Right].sum ;
        }
        Update(Left, b, Mid, i, j);
        Update(Right, Mid + 1, e, i, j);
        Tree[node].sum = Tree[Left].sum + Tree[Right].sum ;
    
    }
    
    
    int Query(int node, int b, int e, int i, int j) {
        if (i > e || j < b) return 0;
        if (b >= i and e <= j) return Tree[node].sum ;
    
        int Left = node << 1;
        int Right = (node << 1) + 1;
        int Mid = (b + e) >> 1;
    
        if( Tree[node].prop) {
            Update(Left, b, Mid, b, Mid);
            Update(Right, Mid + 1, e, Mid + 1, e);
            Tree[node].prop = 0;
            Tree[node].sum = Tree[Left].sum + Tree[Right].sum ;
        }
    
        int p1 = Query(Left, b, Mid, i, j);
        int p2 = Query(Right, Mid + 1, e, i, j);
    
        return p1 + p2;
    }
    
    void solve()
    {
       int n , u , v , k ,ty , q;  cin >> n ;
       for(int i = 1 ; i <= n ; i++) cin >> a[i] ;
       for(int i = 0 ; i < n - 1 ; i++){
        cin >> u >> v ;
        Edge[u].pb(v) ;
        Edge[v].pb(u) ;
       }
    
       dfs(1 , -1) ;
       init(1 , 1 , Time) ;
       cin >> q ;
       while(q--){
        cin >> ty >> k ;
        if(ty == 2) cout << Query(1 , 1 , Time , st[k] , en[k]) << endl;
        else Update(1 , 1 , Time , st[k] , en[k]) ;
       }
    
    }
    int main()
    {
         __Heart__
         //READ("input0.txt");
         //WRITE("output0.txt") ;
         int t ; t = 1 ; while(t--) solve() ;
    }
    
    
  • 1

Information

ID
1101
Difficulty
5
Category
Graph_Theory | Data_Structure | Segment_Tree , Binary_Indexed_Tree , Sparse_Table Click to Show
Tags
# Submissions
53
Accepted
17
Accepted Ratio
32%
Uploaded By