1269-consecutive Sum
|
PDF (中文版) |
Statistics |
Forum |
| Time Limit:3 second (s) |
Memory limit:64 MB |
Little Jimmy is learning what to add integers. As in decimal the digits is 0 to 9, it makes a bit hard for him to understand the summation of all pair of digits. Since addition of numbers requires the knowledge of adding digits. So, he mother gave him a software that can convert a decimal integers to their binary and a binary to its corresponding deci Mal. So, Jimmy's idea was to convert the numbers into binaries, and then he adds them and turns the result back to decimal using The software. It's easy-to-add in binary, since-need to know how to add (0, 0), (0, 1), (1, 0), (1, 1). Jimmy doesn ' t has the idea of carry operation, so he thinks that
1 + 1 = 0
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0
Using These operations, he adds the numbers in binary. So, according to his calculations,
3 (011) + 7 (111) = 4 (100)
Now is given an array of n integers, indexed from 0 to n-1, and you has to find both indices C3>i J in the array (0≤i≤j < n), such that the summation (according to Jimmy) of all integers between Indices I and Jin the array, is maximum. And you also has to find a indices, p Q in the array (0≤p≤q < n), such that the summation (ACC Ording to Jimmy) of any integers between indices p and Q in the array, is minimum. You are only maximum and minimum integers.
Input
Input starts with an integer T (≤10), denoting the number of test cases.
Each case is starts with a line containing an integer n (1≤n≤50000). The next line contains n space separated non-negative integers, denoting the integers of the given array. Each of the integer fits into a is signed integer.
Output
For each case, print the case number, the maximum and minimum summation that can be made using Jimmy's addition.
| Sample Input |
Output for Sample Input |
2 5 6 8 2) 4 2 5 3 8 2) 6 5 |
Case 1:14 2 Case 2:15 1 |
Note
The Dataset is huge, and the use faster I/O methods.
Test instructions: The maximum and minimum values of the difference of the continuous interval are obtained;
Idea: trie tree, greedy;
First, the prefix XOR, and then first add 0 to the tire tree, 0 XOR or any value for that value itself, and then we know pre[i]^pre[j];j<i;
Ans[j+1]^ans[j+2]...^ans[i]; that is, the interval [i,j], then we add the preceding prefix tire, and then we in the tree greedy selection, if it is to find the largest, the greedy choice is different from the current position, because it is the highest bit
Down, so we guarantee the high number to be as large as possible, if it does not exist with the current bit the same, then can only go the same. Know how greedy the biggest, the smallest and this is similar.
Complexity is (N*log (1<<31-1));
1#include <stdio.h>2#include <algorithm>3#include <string.h>4#include <stdlib.h>5#include <iostream>6 using namespacestd;7typedefLong LongLL;8LL ans[500005];9 intid[ +];Ten structnode One { ANode *p[2]; - node () - { theMemset (P,0,sizeof(P)); - } - }; -Node *Root; + void inch(int*v) - { + inti,j,k; ANode *c=Root; at for(i= -; i>=0; i--) - { - intPre=V[i]; - if(c->p[v[i]]==NULL) - { -c->p[v[i]]=Newnode (); in } -C=c->P[v[i]]; to } + } -LL MA (int*v) the { *LL sum=0; $LL l=1;Panax NotoginsengNode*c=Root; - inti; the for(i= -; i>=0; i--) + { A intPre=V[i]; the if(C->p[(pre+1)%2]==NULL) + { -C=c->P[pre]; $sum*=2; $sum+=0; - } - Else the { -c=c->p[(pre+1)%2];Wuyisum*=2; thesum+=1; - } Wu } - returnsum; About } $LL mi (int*v) - { -LL sum=0; -LL l=2; ANode*c=Root; + inti; the for(i= -; i>=0; i--) - { $ intPre=V[i]; the if(c->p[pre]==NULL) the { thec=c->p[(pre+1)%2]; thesum*=2; -sum+=1; in } the Else the { AboutC=c->P[pre]; thesum*=2; thesum+=0; the } + } - returnsum; the }Bayi voidDel (node *c) the { the for(intI=0; i<2; i++) - { - if(c->p[i]!=NULL) theDel (c->p[i]); the } the Free(c); the } - intMainvoid) the { the inti,j,k; thescanf"%d",&k);94 ints; the intn,m; the for(s=1; s<=k; s++) the {98scanf"%d",&n); About for(i=1; i<=n; i++) - {101scanf"%lld",&ans[i]);102ans[i]^=ans[i-1];103 }104root=Newnode (); theLL maxx=0;106LL minn=1e12;107memset (ID,0,sizeof(ID));108 inch(ID);109 for(i=1; i<=n; i++) the {111memset (ID,0,sizeof(ID)); theLL kk=Ans[i];113 intCnt=0; the while(KK) the { theid[cnt++]=kk%2;117Kk/=2;118 }119maxx=Max (Maxx,ma (id)); -minn=min (Minn,mi (id));121 inch(ID);122 }del (root);123printf"Case %d:", s);124printf"%lld%lld\n", Maxx,minn); the}return 0;126}
1269-consecutive Sum