zoukankan      html  css  js  c++  java
  • HUST——1106xor的难题之二(异或树状数组单点修改和区间查询)

    1106: xor的难题之二

    时间限制: 2 Sec  内存限制: 128 MB
    提交: 8  解决: 3

    题目描述

    上次Alex学长的问题xor难题很简单吧,现在hkhv学长有个问题想问你们。
    他现在有n个数,q个操作。操作分两种,操作一是查询下标L到下标R之间的xor值是多少,操作二是将第i个数变为x。

    输入

    输入T(T <= 100)组数据,每组数据第一行输入n(1 <=n <= 10^4)和q(1 <=q <= 10^4),接下来一行输入n个数字ai(0 <=ai <= 10^9),接下来是q个操作:"1 L R"表示询问L到R之间的xor值(1 <=L <= R <= n),"2 i x"表示将第i个数变为x(1 <=x <= 10^9)。

    输出

    对于每一组询问,输出对应的答案(输出格式见样例)。

    样例输入

    1
    3 3
    1 2 3
    1 1 3
    2 2 0
    1 1 3

    样例输出

    Case 1:
    0
    2

    本地搞了半天终于搞对了,单点若需要二次修改的话需要另外一个数组D[i]来记录原本正常非树状结构下的值,在修改的时候跟add结合处理一下。

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<sstream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<deque>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long LL;
    #define INF 0x3f3f3f3f
    #define MM(x) memset(x,0,sizeof(x))
    const int N=10010;
    int c[N],d[N];
    inline void add(int k,int val,int n)
    {
    	while (k<=n)
    	{
    		c[k]^=val;
    		k+=(k&-k);
    	}
    }
    inline int getsum(int k)
    {
    	int sum=0;
    	while (k)
    	{
    		sum^=c[k];
    		k-=(k&-k);
    	}
    	return sum;
    }
    int main(void)
    {
    	int tcase,i,j,ops,x,val,q,n,l,r;
    	scanf("%d",&tcase);
    	for (int w=1; w<=tcase; w++)
    	{
    		MM(c);
    		MM(d);
    		scanf("%d%d",&n,&q);
    		for (i=1; i<=n; i++)
    		{
    			scanf("%d",&d[i]);
    			add(i,d[i],n);
    		}
    		printf("Case %d:
    ",w);
    		for (i=1; i<=q; i++)
    		{
    			scanf("%d",&ops);
    			if(ops==1)
    			{
    				scanf("%d%d",&l,&r);
    				printf("%d
    ",getsum(r)^getsum(l-1));
    			}
    			else if(ops==2)
    			{
    				scanf("%d%d",&x,&val);
    				add(x, val^d[x],n);
    				d[x]=val;
    			}
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    前端基础之BOM和DOM
    JavaScript
    css-属性、样式调节
    计算机操作系统
    计算机组成原理
    计算机基础之编程
    css-选择器
    HTML-标签
    python打印有色字体
    mysql 数据库语法详解
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766358.html
Copyright © 2011-2022 走看看