Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /*
- * Hackerearth Question: Tree Query
- * Problem Link: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/tree-query-3-5d98588f/
- */
- // Time Complexity: For query=> O(1), For update=> O(log(n))
- public class Tree_Query2
- {
- List<Integer>[] tree;
- int[] A,parent,values;
- void solve() {
- int tc=1;
- StringBuilder sb = new StringBuilder();
- while(tc-->0) {
- int n=ni(),q=ni();
- tree=new ArrayList[n+1];
- A=new int[n+1];
- parent=new int[n+1]; parent[0]=-1;
- values=new int[n+1];
- Arrays.fill(A,1);
- tree[1]=new ArrayList<>();
- for(int i=0;i<n-1;i++) {
- int u=ni(),v=ni();
- if(tree[u]==null) tree[u]=new ArrayList<>();
- if(tree[v]==null) tree[v]=new ArrayList<>();
- tree[u].add(v);
- tree[v].add(u);
- }
- // for storing parents of each node and their values
- d_f_s(1,0);
- int v;
- while(q-->0) {
- if(ni()==1) {
- A[v=ni()]^=1;
- if(A[v]==0) update(v,parent[v],-values[v]);
- else {
- for(int child:tree[v]) if(child!=parent[v]) values[v]+=values[child];
- update(parent[v],parent[parent[v]],++values[v]);
- }
- }
- else sb.append(values[ni()]).append("\n");
- }
- }
- out.println(sb);
- out.flush();
- out.close();
- }
- void d_f_s(int cur, int par) {
- parent[cur]=par;
- values[cur]=1;
- for(int child:tree[cur])
- if(child!=par) {
- d_f_s(child,cur);
- values[cur]+=values[child];
- }
- }
- void update(int cur, int par,int val) {
- while(par!=-1 && values[cur]!=0) {
- values[cur]+=val;
- cur=par;
- par=parent[cur];
- }
- }
- public static void main(String[] args)throws Exception {
- // new Thread(null, new Tree_Query2()::solve, "1", 1 << 26).start();
- new Thread(null, new Tree_Query2("OJ")::solve, "1", 1 << 26).start();
- }
- FastReader sc=null;PrintWriter out = null;
- public Tree_Query2()throws Exception {
- sc = new FastReader(new FileInputStream("input.txt"));
- out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
- }
- public Tree_Query2(String JUDGE) {
- sc = new FastReader(System.in);
- out = new PrintWriter(System.out);
- }
- String ns() { return sc.next(); }
- int ni() { return sc.nextInt(); }
- long nl() { return sc.nextLong(); }
- int[] ni(int n) {
- int a[]=new int[n];
- for(int i=0;i<n;a[i++]=ni());
- return a;
- }
- long[] nl(int n) {
- long a[]=new long[n];
- for(int i=0;i<n;a[i++]=nl());
- return a;
- }
- int[][] ni(int n,int m) {
- int a[][]=new int[n][m];
- for(int i=0;i<n;i++)
- for(int j=0;j<m;j++)
- a[i][j]=ni();
- return a;
- }
- long[][] nl(int n,int m) {
- long a[][]=new long[n][m];
- for(int i=0;i<n;i++)
- for(int j=0;j<m;j++)
- a[i][j]=nl();
- return a;
- }
- static class FastReader {
- private InputStream stream;
- private byte[] buf = new byte[1024];
- private int curChar;
- private int numChars;
- private FastReader.SpaceCharFilter filter;
- public FastReader(InputStream stream) {
- this.stream = stream;
- }
- public int read() {
- if (numChars == -1) throw new InputMismatchException();
- if (curChar >= numChars) {
- curChar = 0;
- try {
- numChars = stream.read(buf);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- if (numChars <= 0) return -1;
- }
- return buf[curChar++];
- }
- public int nextInt() {
- int c = read();
- while (isSpaceChar(c)) c = read();
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- int res = 0;
- do {
- if (c < '0' || c > '9') throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = read();
- }
- while (!isSpaceChar(c));
- return res * sgn;
- }
- public long nextLong() {
- int c = read();
- while (isSpaceChar(c)) c = read();
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- long res = 0;
- do {
- if (c < '0' || c > '9') throw new InputMismatchException();
- res = res*1L*10;
- res += c - '0';
- c = read();
- }
- while (!isSpaceChar(c));
- return res *1L* sgn;
- }
- public String next() {
- int c = read();
- while (isSpaceChar(c)) c = read();
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = read();
- } while (!isSpaceChar(c));
- return res.toString();
- }
- public boolean isSpaceChar(int c) {
- if (filter != null) return filter.isSpaceChar(c);
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- public interface SpaceCharFilter {
- public boolean isSpaceChar(int ch);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement