Created
December 3, 2012 06:48
-
-
Save m00dy/4193213 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
#include<math.h> | |
#include<string.h> | |
class SegmentTree | |
{ | |
int *A,size; | |
public: | |
SegmentTree(int N) | |
{ | |
int x = (int)(ceil(log2(N)))+1; | |
size = 2*(int)pow(2,x); | |
A = new int[size]; | |
memset(A,-1,sizeof(A)); | |
} | |
void initialize(int node, int start, | |
int end, int *array,int a) | |
{ | |
if (start==end) | |
A[node] = start; | |
else | |
{ | |
int mid = (start+end)/2; | |
initialize(2*node,start,mid,array,a); | |
initialize(2*node+1,mid+1,end,array,a); | |
if ((array[A[2*node]]^a)>(array[A[2*node+1]]^a)) | |
A[node] = A[2 * node]; | |
else | |
A[node] = A[2 * node + 1]; | |
} | |
} | |
int query(int node, int start, | |
int end, int i, int j, int *array,int a) | |
{ | |
int id1,id2; | |
if (i>end || j<start) | |
return -1; | |
if (start>=i && end<=j) | |
return A[node]; | |
int mid = (start+end)/2; | |
id1 = query(2*node,start,mid,i,j,array,a); | |
id2 = query(2*node+1,mid+1,end,i,j,array,a); | |
if (id1==-1) | |
return id2; | |
if (id2==-1) | |
return id1; | |
if ((array[id1]^a)>(array[id2]^a)) | |
return id1; | |
else | |
return id2; | |
} | |
}; | |
int main() | |
{ | |
int T,N,Q; | |
int a,p,q; | |
int* keys; | |
scanf("%d",&T); | |
for(int a=0;a<T;a++) | |
{ | |
scanf("%d %d",&N,&Q); | |
keys = new int[N]; | |
for(int j=0;j<N;j++) | |
{ | |
int keytemp = 0; | |
scanf("%d",&keytemp); | |
keys[j] = keytemp; | |
} | |
for(int z = 0; z < Q; z++){ | |
scanf("%d %d %d",&a,&p,&q); | |
SegmentTree s(N); | |
s.initialize(1,0,N-1,keys,a); | |
printf("%d\n",(keys[s.query(1,0,N-1,p-1,q-1,keys,a)]^a)); | |
} | |
} | |
system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this solution will get TLE as building the tree costs N Lg(N) .. so this solutions costs Q N lg(N) which is too big..