-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
55 lines (51 loc) · 1.2 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define R register
#define LL long long
#define U unsigned
#define FOR(i,a,b) for(R int i = a;i <= b;++i)
#define RFOR(i,a,b) for(R int i = a;i >= b;--i)
#define CLR(i,a) memset(i,a,sizeof(i))
#define BR printf("--------------------\n")
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int BASE = 19260817;
const int MAXN = 1000000 + 5;
int str[MAXN],stack[MAXN];
int N,top;
U LL hash[MAXN],a[MAXN];
inline void Solve(){
scanf("%d",&N);
FOR(i,1,N) scanf("%d",str+i);
FOR(i,1,N){
if(top && stack[top] == str[i])
--top;
else{
stack[++top] = str[i];
hash[top] = hash[top-1] * BASE + str[i];
}
a[i] = hash[top];
}
std::sort(a,a + N + 1);
LL cnt = 0,ans = 0;
FOR(i,0,N){
if(!i || a[i] != a[i-1]) cnt = 1;
else
ans += cnt++;
}
printf("%lld\n",ans);
FOR(i,0,N) a[i] = 0,hash[i] = 0,top = 0,stack[i] = 0;
}
int main(){
int T;scanf("%d",&T);
while(T--) Solve();
return 0;
}