hdu-5683 zxa and xor (位运算)

题目链接:

zxa and xor

Time Limit: 16000/8000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)


Problem Description
 
zxa had a great interest in exclusive disjunction(i.e. XOR) recently, therefore he took out a non-negative integer sequence a1,a2,,an of length n.

zxa thought only doing this was too boring, hence a function funct(x,y) defined by him, in which ax would be changed into y irrevocably and then compute 1i<jn(ai+aj) as return value.

zxa is interested to know, assuming that he called such function m times for this sequence, then what is the return value for each calling, can you help him?

tips:1i<jn(ai+aj) means that (a1+a2)(a1+a3)(a1+an)(a2+a3)(a2+a4)(a2+an)(an1+an).
 
Input
 
The first line contains an positive integer T, represents there are T test cases.

For each test case:

The first line contains two positive integers n and m.

The second line contains n non-negative integers, represent a1,a2,,an.

The next m lines, the i-th line contains two non-negative integers x and y, represent the i-th called function is funct(x,y).

There is a blank between each integer with no other extra space in one line.

1T1000,2n210^4,1m210^4,0ai,y10^9,1xn,1n,m10^5
 
Output
 
For each test case, output in m lines, the i-th line a positive integer, repersents the return value for the i-th called function.
 
Sample Input
 
1
3 3
1 2 3
1 4
2 5
3 6
 
Sample Output
 
4
6
8
 
题意:
 
给一个数列,每次把a[x]变成y,问每次的fun是多少;
 
思路:
 
先算一次fun,再在每次变换的时候运用a^a=0的性质把a[x]+a[i]再异或一边,然后把y+a[i]异或和上去就好了;
 
AC代码:
 
//#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e4+25;
int n,m;
int a[N],ans;
int get_ans()
{
     ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int sum=a[i]+a[j];
            ans^=sum;
        }
    }
}
int fun(int fx,int fy)
{
    for(int i=1;i<=n;i++)
    {
       if(i!=fx) ans^=(a[fx]+a[i])^(fy+a[i]);
    }
    a[fx]=fy;
}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&m);
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&a[i]);
       }
       int x,y;
       get_ans();

       while(m--)
       {
           scanf("%d%d",&x,&y);
           fun(x,y);
           printf("%d
",ans);
       }
   }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5499450.html