Gym - 101608D(区间问题)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值