异或和(第14届蓝桥杯)

原题链接

https://www.lanqiao.cn/problems/3549/learning/?page=1&first_category_id=1&name=%E5%BC%82%E6%88%96%E5%92%8C

线段树做法(WR)

不知道为什么不对…(已AC)

package practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class 异或和{
	static int tot = 0,n,m;
	static int N = 100001;
	static List<Integer>[] list = new ArrayList [N];
	static int in[] = new int[N];
	static int out[] = new int[N];
	static int a[] = new int[N];
	static int tree[] = new int[4*N+1];
	static PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 	public static void main(String args[]) throws IOException {
		MyScanner scanner = new MyScanner();
		n = scanner.nextInt();
		m = scanner.nextInt();
		int w[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			w[i] = scanner.nextInt();
			list[i] = new ArrayList<Integer>();
		}
		for(int i=0;i<n-1;i++) {
			int u = scanner.nextInt();
			int v = scanner.nextInt();
			list[u].add(v);
			list[v].add(u);
		}
		dfs(1,0);
		for(int i=1;i<=n;i++) {
			a[in[i]] = w[i];
		}
		create(1, 1, n);
		for(int i=0;i<m;i++) {
			if(scanner.nextInt()==2) {
				int x = scanner.nextInt();
				pw.println(search(1, 1, n, in[x], out[x]));
			}
			else {
				int x = scanner.nextInt();
				int y = scanner.nextInt();
				add(1, 1, n, in[x], y);
				a[in[x]] = y;
			}
		}
		pw.flush();
	}
 	public static void create(int k,int l,int r) {
 		if(l==r) {
 			tree[k] = a[l];
 			return;
 		}
 		int mid = (l+r)/2;
 		create(k+k, l, mid);
 		create(k+k+1, mid+1, r);
 		tree[k] = tree[k+k]^tree[k+k+1];
 	}
 	public static int search(int k,int l,int r,int ll,int rr) {
 		if(l==ll&&r==rr) {
 			return tree[k];
 		}
 		int mid = (l+r)/2;
 		if(mid>=rr)
 			return search(k+k, l, mid, ll, rr);
 		else {
 			if(mid<ll) 
 				return search(k+k+1, mid+1, r, ll, rr);
 			else {
				return search(k+k, l, mid, ll, mid)^search(k+k+1, mid+1, r, mid+1, rr);
			}
 		}
 	}
 	public static void add(int k,int l,int r,int x,int y) {
 		tree[k]^=a[x]^y;
 		if(l==r) {
 			tree[k] = y;
 			return;
 		}
 		int mid = (l+r)/2;
 		if(mid>=x)
 			add(k+k, l, mid, x, y);
 		else
 			add(k+k+1, mid+1, r, x, y); 		
 	}
	public static void dfs(int i,int j) {
		in[i] = ++tot;
		for(int x:list[i]) {
			if(x == j)
				continue;
			dfs(x, i);
		}
		out[i] = tot;
	}
}
class MyScanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer st = new StreamTokenizer(br);

    public int nextInt() throws IOException {
        st.nextToken();
        return (int) st.nval;
    }
    public long nextLong() throws IOException {
        st.nextToken();
        return (long) st.nval;
    }
    public String readLine() throws IOException {
        return br.readLine();
    }
}

树状数组(AC)

package practice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class test{
	static int tot = 0,n,m;
	static int N = 100001;
	static List<Integer>[] list = new ArrayList [N];
	static int in[] = new int[N];
	static int out[] = new int[N];
	static int a[] = new int[N];
	static int tree[] = new int[4*N+1];
	static PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 	public static void main(String args[]) throws IOException {
		MyScanner scanner = new MyScanner();
		n = scanner.nextInt();
		m = scanner.nextInt();
		int w[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			w[i] = scanner.nextInt();
			list[i] = new ArrayList<Integer>();
		}
		for(int i=0;i<n-1;i++) {
			int u = scanner.nextInt();
			int v = scanner.nextInt();
			list[u].add(v);
			list[v].add(u);
		}
		dfs(1,0);
		for(int i=1;i<=n;i++) {
			add(in[i],w[i]);
		}
		for(int i=0;i<m;i++) {
			if(scanner.nextInt()==1) {
				int x = scanner.nextInt();
				int y = scanner.nextInt();
				add(in[x], y^(sum(in[x]-1)^sum(in[x])));
			}
			else {
				int x = scanner.nextInt();
				System.out.println(sum(in[x]-1)^sum(out[x]));
			}
		}
 	}
 	public static void add(int x,int y) {
 		while(x<=n) {
 			a[x]^=y;
 			x+=x&(-x);
 		}
 	}
 	public static int sum(int x) {
 		int res = 0;
 		if(x==0)
 			return 0;
 		while(x>0) {
 			res^=a[x];
 			x-=x&(-x);
 		}
 		return res;
 	}
	public static void dfs(int i,int j) {
		in[i] = ++tot;
		for(int x:list[i]) {
			if(x == j)
				continue;
			dfs(x, i);
		}
		out[i] = tot;
	}
}
class MyScanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer st = new StreamTokenizer(br);

    public int nextInt() throws IOException {
        st.nextToken();
        return (int) st.nval;
    }
    public long nextLong() throws IOException {
        st.nextToken();
        return (long) st.nval;
    }
    public String readLine() throws IOException {
        return br.readLine();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值