TOJ 4114 Evaluate Xor

For AC this problem ,I consult AClavin's code ,For clearly knowing the code's operational principle.I research the code ...hours.Too hard to understanding.

The portal:http://acm.tju.edu.cn/toj/showp4114.html

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

const int N = 1e5+5;
const int M = 1e6+5;

int a[N];
int Bit[M<<1];

void Insert(int x,int value){
for(int i = x ; i<= M ; i+= i & (-i)){
Bit[i] += value;
}
}

int Query(int x){
int ret = 0;
if(x <= 0) return ret;
for(int i = x; i ; i -= i & (-i)){
ret += Bit[i];
}
return ret;
}

void Deal_with(){
int T;
scanf("%d",&T);
memset(Bit,0,sizeof(Bit));
while(T--){
int n;
scanf("%d",&n);
int tempa;
a[0] = 0;
for(int i=1;i<=n;i++){
scanf("%d",&tempa);
a[i] = a[i-1] + tempa;
}
int up = floor(log(a[n])/log(2.0));
//up is the floor of bit of answer.
int res = 0;
for(int l = up ;l >= 0;l--){
int lim = 1 << l , Mod = lim << 1;
int tmp = 0;
// tmp calculate there is how many '1' in this bit.
Insert(1,1);
//Add 1 for every Bit array's member just for avoid tree_array query '0'.
//Insert(1,1) is Insert 0 to Bit array , the value of '0' is 1;
for(int i=1;i<=n;i++){
int s = (a[i] + 1) % Mod;
int e = (a[i] + lim) % Mod;
// when bit is '1' , lim = 2 ,mod = 4;
// Array a = 1,3,5,6;
if(s > e){
tmp += i - Query(s);
//Mimic to get a higher bit digital.
// 6 = (110); a[i] = (10); s = (10) ;if you can find ki (10) .. (11);a[i] - ki '1' bit will change to 1.
tmp += Query(e+1);
//Mimic a[i] - a[k] k = 1..i Have or not i bit digital.
// 3 = (11);  a[i] = (11); e = (01) ;if you can find ki (00) ... (01);a[i] - ki '1' bit will still be '1'.
}
else {
tmp += Query(e+1) - Query(s);
//Mimic to get a higher bit digital.
//5 = (101) ;a[i] = (01); s = (01) ; e = (11) ;if you can find ki (01)..(11);a[i] -ki '1' bit will change to 1.
}
Insert(a[i]+1,1);
}
if(tmp & 1) res += lim;
Insert(1,-1);
for(int i=1;i<=n;i++){
Insert(a[i]+1,-1);
a[i] &= (lim - 1);
}
}
printf("%d\n",res);
}
}

int main(void){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}



TOJ 题目分类

2010-11-03 22:39:00

2014-08-19 16:36:23

TOJ-ACM

2013-12-16 21:58:37

hdu 4114 dp

2013-11-26 22:15:11

hdu 4114 状态压缩+最短路

2013-11-27 22:14:14

TOJ 1005

2013-12-10 15:21:07

TOJ 1163.Gone Fishing(经典题目)

2016-07-17 23:56:25

TOJ 1138. Binomial Showdown

2010-01-28 20:37:00

TOJ 3185 二叉树的遍历

2013-08-16 22:41:33

TOJ 2469 — Friends

2015-04-18 19:21:06

不良信息举报

TOJ 4114 Evaluate Xor