Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- map<pair<int,int>,int>mp,tk[5005];
- int n,arr[5005];
- typedef long long ll;
- ll base1=304933,base2=4395853,p1=1e9+7,p2=1e9+9,pow1[5005],pow2[5005];
- int main()
- {
- pow1[0]=pow2[0]=1;
- for(int i=1;i<5005;++i){
- pow1[i]=(pow1[i-1]*base1)%p1;
- pow2[i]=(pow2[i-1]*base2)%p2;
- }
- int t;
- cin>>t;
- while(t--){
- mp.clear();
- cin>>n;
- for(int i=0;i<n;++i){
- tk[i].clear();
- int u,v;
- scanf("%d%d",&u,&v);
- if(u>v)swap(u,v);
- if(!mp.count({u,v}))mp[{u,v}]=mp.size();
- arr[i]=mp[{u,v}];
- }
- int ans=0;
- for(int i=0;i<n;++i){
- int hsh1=0,hsh2=0;
- for(int j=i;j<n;++j){
- hsh1+=pow1[arr[j]];hsh1%=p1;
- hsh2+=pow2[arr[j]];hsh2%=p2;
- ans+=tk[j-i][{hsh1,hsh2}];
- ++tk[j-i][{hsh1,hsh2}];
- }
- }
- cout<<ans<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment