HDU5456 二维树状数组+nim博弈

本文介绍了一种使用二维稀疏数组实现高效异或查询的方法。通过预先处理数据,可以快速响应区间异或求和操作,并支持在线更新。文章详细解释了数据结构的设计原理及其在解决特定问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


异或和


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		new HDU5465().solve();
	}
}

class HDU5465 {

	InputReader in = new InputReader(System.in);
	PrintWriter out = new PrintWriter(System.out);
	
	Bit bit = new Bit() ;
	
	void solve() {
         int t = in.nextInt() ;
         while(t-- > 0){
        	 int n = in.nextInt() ;
        	 int m = in.nextInt() ;
        	 bit.resize(n, m) ;
        	 int q = in.nextInt() ;
        	 for(int i = 1 ; i <= n ; i++){
        		 for(int j = 1 ; j <= m ; j++)
        			 bit.add(i, j, in.nextInt()) ;
        	 }
        	 while(q-- > 0){
        		 if(in.nextInt() == 1){
        			  if(bit.query(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt())  != 0)
        				   out.println("Yes") ;
        			  else out.println("No") ;
        		 }
        		 else  bit.update(in.nextInt() , in.nextInt() , in.nextInt()) ;
        	 }
         }		 
         out.flush() ;
	}
}

class Bit{
	final int N = 508 ;
	int[][] val = new int[N][N] ;
	int n , m ;
	void resize(int n , int m){
		this.n = n ;
		this.m = m ;
	    for(int i = 1 ; i <= n ; i++)
	    	for(int j = 1 ; j <= m ; j++) val[i][j] = 0 ; 
	}
	
	int lowbit(int x){
		return x & (-x) ;
	}
	
	void add(int i , int j , int c){
		for(int x = i ; x <= n ; x += lowbit(x))
			for(int y = j ; y <= m ; y += lowbit(y))  val[x][y] ^= c ;
	}
	
	int sum(int i , int j){
		int res = 0 ;
		for(int x = i ; x > 0 ; x -= lowbit(x))
			for(int y = j ; y > 0 ; y -= lowbit(y))  res ^= val[x][y] ; 
	    return res ;
	}
	
	int query(int x1 , int y1 , int x2 , int y2){
		return sum(x2, y2) ^ sum(x2, y1-1) ^ sum(x1-1, y2) ^ sum(x1-1, y1-1)  ;
	}
	
	void update(int x , int y , int c){
		add(x, y, query(x, y, x, y)) ;
		add(x, y, c) ;
	}
}


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());
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值