POJ3321 dfs序+树状数组
来源:互联网 发布:java 迭代器 for循环 编辑:程序博客网 时间:2024/05/16 13:51
题意:给一棵n个节点的树,每个节点开始有一个苹果,m次操作
1.将某个结点的苹果数异或 1
2.查询一棵子树内的苹果数
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.StringTokenizer;public class Main {public static void main(String[] args) {new Task().solve() ;}}class Task {InputReader in = new InputReader(System.in);PrintWriter out = new PrintWriter(System.out);List<Integer>[] adj ;int[] left , right ;int dfn ;void dfs(int u){left[u] = ++dfn ;for(int v : adj[u]){if(left[v] == -1) dfs(v) ;}right[u] = dfn ;}int n ;int[] val ;void add(int id , int c){for(; id <= n ; id += Integer.lowestOneBit(id)) val[id] += c ;}int sum(int id){int res = 0 ;for(; id >= 1 ; id -= Integer.lowestOneBit(id)) res += val[id] ;return res ;}void solve(){ n = in.nextInt() ; adj = new List[n+1] ;left = new int[n+1] ;right = new int[n+1] ;boolean[] hasApple = new boolean[n+1] ; for(int i = 1 ; i <= n ; i++){adj[i] = new ArrayList<Integer>() ;hasApple[i] = true ;}for(int i = 1 ; i < n ; i++){int u = in.nextInt() ;int v = in.nextInt() ;adj[u].add(v) ;adj[v].add(u) ;}Arrays.fill(left , -1) ;dfn = 0 ;dfs(1) ;val = new int[n+1] ;Arrays.fill(val , 0) ;for(int i = 1 ; i <= n ; i++) add(i , 1) ;int m = in.nextInt() ; while(m-- > 0){if("Q".equals(in.next())){int v = in.nextInt() ;out.println(sum(right[v]) - sum(left[v]-1)) ;}else{int v = in.nextInt() ;add(left[v] , hasApple[v] ? -1 : 1) ;hasApple[v] = !hasApple[v] ;}}out.flush() ;}}class InputReader {public BufferedReader reader;public StringTokenizer tokenizer;public InputReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = new StringTokenizer("");}private void eat(String s) {tokenizer = new StringTokenizer(s);}public String nextLine() {try {return reader.readLine();} catch (Exception e) {return null;}}public boolean hasNext() {while (!tokenizer.hasMoreTokens()) {String s = nextLine();if (s == null)return false;eat(s);}return true;}public String next() {hasNext();return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public double nextDouble() {return Double.parseDouble(next());}public BigInteger nextBigInteger() {return new BigInteger(next());}}
0 0
- POJ3321 dfs序+树状数组
- POJ3321(dfs序+树状数组)
- poj3321 dfs+树状数组
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- POJ3321[苹果树]--DFS序+树状数组
- Poj3321 Apple Tree【DFS序】【树状数组】
- 【树状数组--dfs序转化】poj3321 Apple Tree
- [jzoj1016][poj3321]苹果树(dfs序+树状数组维护)
- poj3321 树状数组+dfs(时间戳)
- poj3321 Apple Tree(DFS+树状数组)
- 【POJ3321】Apple Tree-DFS+树状数组维护
- poj3321 Apple Tree(树状数组+dfs)
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- POJ3321——Apple Tree(DFS+树状数组)
- POJ3321 Apple Tree(树状数组 + dfs + 线性表)
- 【poj3321】Apple Tree——树状数组&DFS
- (POJ3321)Apple Tree <dfs序列 树状数组>
- 文件寄生——寻找宿主的不归路(NTFS文件流实际应用)
- Linux内核链接文件分析(zz) -----vmlinux.lds文件链接文件
- App架构设计经验谈:数据层的设计
- C语言中的强符号与弱符号
- 14. Spring Boot 定义过滤器、监听器、拦截器
- POJ3321 dfs序+树状数组
- qml键盘按键事件
- 蓝桥杯 蚂蚁感冒 (思维)
- redhat 在终端中输入ifconfig,提示command not found
- 按键查询法驱动程序分析
- Android 进阶发展方向-知识体系阅览(最新)
- 大数据的奇特世界—数据分析
- mysql下载与安装
- DC 平衡双向控制解串器 转接口IC GM8914:FPD-LINK III转LVTTL芯片