Advertisement
coder0687

Untitled

May 14th, 2021 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. /*
  4. * Hackerearth Question: Tree Query
  5. * Problem Link: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/tree-query-3-5d98588f/
  6. */
  7. // Time Complexity: For query=> O(1), For update=> O(log(n))
  8. public class Tree_Query2
  9. {
  10.  
  11.     List<Integer>[] tree;
  12.     int[] A,parent,values;
  13.     void solve() {
  14.         int tc=1;
  15.         StringBuilder sb = new StringBuilder();
  16.         while(tc-->0) {
  17.             int n=ni(),q=ni();
  18.             tree=new ArrayList[n+1];
  19.             A=new int[n+1];
  20.             parent=new int[n+1]; parent[0]=-1;
  21.             values=new int[n+1];
  22.             Arrays.fill(A,1);
  23.             tree[1]=new ArrayList<>();
  24.             for(int i=0;i<n-1;i++) {
  25.                 int u=ni(),v=ni();
  26.                 if(tree[u]==null) tree[u]=new ArrayList<>();
  27.                 if(tree[v]==null) tree[v]=new ArrayList<>();
  28.                 tree[u].add(v);
  29.                 tree[v].add(u);
  30.             }
  31.             // for storing parents of each node and their values
  32.             d_f_s(1,0);
  33.             int v;
  34.             while(q-->0) {
  35.                 if(ni()==1) {
  36.                     A[v=ni()]^=1;
  37.                     if(A[v]==0) update(v,parent[v],-values[v]);
  38.                     else {
  39.                         for(int child:tree[v]) if(child!=parent[v]) values[v]+=values[child];
  40.                         update(parent[v],parent[parent[v]],++values[v]);
  41.                     }
  42.                 }
  43.                 else sb.append(values[ni()]).append("\n");
  44.             }
  45.         }
  46.         out.println(sb);
  47.         out.flush();
  48.         out.close();
  49.     }
  50.     void d_f_s(int cur, int par) {
  51.         parent[cur]=par;
  52.         values[cur]=1;
  53.         for(int child:tree[cur])
  54.             if(child!=par) {
  55.                 d_f_s(child,cur);
  56.                 values[cur]+=values[child];
  57.             }
  58.     }  
  59.     void update(int cur, int par,int val) {
  60.         while(par!=-1 && values[cur]!=0) {
  61.             values[cur]+=val;
  62.             cur=par;
  63.             par=parent[cur];
  64.         }
  65.     }
  66.     public static void main(String[] args)throws Exception {
  67.         // new Thread(null, new Tree_Query2()::solve, "1", 1 << 26).start();
  68.         new Thread(null, new Tree_Query2("OJ")::solve, "1", 1 << 26).start();
  69.     }
  70.  
  71.     FastReader sc=null;PrintWriter out = null;
  72.     public Tree_Query2()throws Exception {
  73.         sc = new FastReader(new FileInputStream("input.txt"));
  74.         out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
  75.     }
  76.     public Tree_Query2(String JUDGE) {
  77.         sc = new FastReader(System.in);
  78.         out = new PrintWriter(System.out);      
  79.     }
  80.    
  81.     String ns() { return sc.next(); }
  82.     int ni() { return sc.nextInt(); }
  83.     long nl() { return sc.nextLong(); }
  84.     int[] ni(int n) {
  85.         int a[]=new int[n];
  86.         for(int i=0;i<n;a[i++]=ni());
  87.         return a;
  88.     }
  89.     long[] nl(int n) {
  90.         long a[]=new long[n];
  91.         for(int i=0;i<n;a[i++]=nl());
  92.         return a;
  93.     }
  94.    
  95.     int[][] ni(int n,int m) {
  96.         int a[][]=new int[n][m];
  97.         for(int i=0;i<n;i++)
  98.             for(int j=0;j<m;j++)
  99.                 a[i][j]=ni();
  100.         return a;
  101.     }
  102.     long[][] nl(int n,int m) {
  103.         long a[][]=new long[n][m];
  104.         for(int i=0;i<n;i++)
  105.             for(int j=0;j<m;j++)
  106.                 a[i][j]=nl();
  107.         return a;
  108.     }
  109.     static class FastReader {
  110.         private InputStream stream;
  111.         private byte[] buf = new byte[1024];
  112.         private int curChar;
  113.         private int numChars;
  114.         private FastReader.SpaceCharFilter filter;
  115.         public FastReader(InputStream stream) {
  116.             this.stream = stream;
  117.         }
  118.  
  119.         public int read() {
  120.             if (numChars == -1) throw new InputMismatchException();
  121.             if (curChar >= numChars) {
  122.                 curChar = 0;
  123.                 try {
  124.                     numChars = stream.read(buf);
  125.                 } catch (IOException e) {
  126.                     throw new InputMismatchException();
  127.                 }
  128.                 if (numChars <= 0) return -1;
  129.             }
  130.             return buf[curChar++];
  131.         }
  132.  
  133.         public int nextInt() {
  134.             int c = read();
  135.             while (isSpaceChar(c)) c = read();
  136.             int sgn = 1;
  137.             if (c == '-') {
  138.                 sgn = -1;
  139.                 c = read();
  140.             }
  141.             int res = 0;
  142.             do {
  143.                 if (c < '0' || c > '9') throw new InputMismatchException();
  144.                 res *= 10;
  145.                 res += c - '0';
  146.                 c = read();
  147.             }
  148.             while (!isSpaceChar(c));
  149.             return res * sgn;
  150.         }
  151.        
  152.         public long nextLong() {
  153.             int c = read();
  154.             while (isSpaceChar(c)) c = read();
  155.             int sgn = 1;
  156.             if (c == '-') {
  157.                 sgn = -1;
  158.                 c = read();
  159.             }
  160.             long res = 0;
  161.             do {
  162.                 if (c < '0' || c > '9') throw new InputMismatchException();
  163.                 res = res*1L*10;
  164.                 res += c - '0';
  165.                 c = read();
  166.             }
  167.             while (!isSpaceChar(c));
  168.             return res *1L* sgn;
  169.         }
  170.        
  171.         public String next() {
  172.             int c = read();
  173.             while (isSpaceChar(c)) c = read();
  174.             StringBuilder res = new StringBuilder();
  175.             do {
  176.                 res.appendCodePoint(c);
  177.                 c = read();
  178.             } while (!isSpaceChar(c));
  179.             return res.toString();
  180.         }
  181.  
  182.         public boolean isSpaceChar(int c) {
  183.             if (filter != null) return filter.isSpaceChar(c);
  184.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  185.         }
  186.  
  187.         public interface SpaceCharFilter {
  188.             public boolean isSpaceChar(int ch);
  189.  
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement