You want to prepare test cases for a problem with the following description:
"Given an array of n positive integers, and a number of queries. Each query will ask about a range [l, r] (1 ≤ l ≤ r ≤ n), where each of the values between index l and index r (both inclusive) occurs an even number of times except one value. The answer to each query is the value that occurs an odd number of times in the given range."
You have generated an array of n positive integers, you need to know the number of valid queries you can ask on this array. A query is valid if it has an answer, and that answer is unique.
Input
The first line of input contains a single integer T (1 ≤ T ≤ 64), the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 5000), the size of the array.
The next line contains n space-separated integers, a1, a2, ..., an (1 ≤ ai ≤ 106), the values of the array.
The total sum of n overall test cases doesn't exceed 72000.
Output
For each test case, print a single line with the number of valid queries you can ask.
Example
Input
1
7
9 1 1 2 1 9 9
Output
12
题意:输入数据的组数,和每组有多少个数据,然后输入这个数组,要求合法区间的个数(合法区间是指这个区间里出现奇数次的数存在且只有一个)
解题思路:桶排,两层for暴力,每次跟随区间的变化更新桶。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int buck[1010000];
int main()
{
freopen("cases.in","r",stdin);
int T,n,i,j,a[50000];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(buck,0,sizeof(buck));
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
int ans = 0,count = 0;
for(i=1; i<=n; i++)
{
ans = 0;
for(j=i; j<=n; j++)
{
buck[a[j]]++;
if(buck[a[j]]%2==1)
ans++;
else
ans--;//例如2 1 1,一开始是2 1 当再进来一个相同的时候就要捡到,因为1这时候从出现奇数次变成出现偶数次了。
if(ans == 1)
count++;
}
for (j=i; j<=n; j++)
buck[a[j]]=0;
}
printf("%d\n",count);
}
fclose(stdin);
return 0;
}