Guest User

Untitled

a guest
May 26th, 2018
1,780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. map<pair<int,int>,int>mp,tk[5005];
  4. int n,arr[5005];
  5. typedef long long ll;
  6. ll base1=304933,base2=4395853,p1=1e9+7,p2=1e9+9,pow1[5005],pow2[5005];
  7. int main()
  8. {
  9.     pow1[0]=pow2[0]=1;
  10.     for(int i=1;i<5005;++i){
  11.         pow1[i]=(pow1[i-1]*base1)%p1;
  12.         pow2[i]=(pow2[i-1]*base2)%p2;
  13.     }
  14.     int t;
  15.     cin>>t;
  16.     while(t--){
  17.         mp.clear();
  18.         cin>>n;
  19.         for(int i=0;i<n;++i){
  20.             tk[i].clear();
  21.             int u,v;
  22.             scanf("%d%d",&u,&v);
  23.             if(u>v)swap(u,v);
  24.             if(!mp.count({u,v}))mp[{u,v}]=mp.size();
  25.             arr[i]=mp[{u,v}];
  26.         }
  27.         int ans=0;
  28.         for(int i=0;i<n;++i){
  29.             int hsh1=0,hsh2=0;
  30.             for(int j=i;j<n;++j){
  31.                 hsh1+=pow1[arr[j]];hsh1%=p1;
  32.                 hsh2+=pow2[arr[j]];hsh2%=p2;
  33.                 ans+=tk[j-i][{hsh1,hsh2}];
  34.                 ++tk[j-i][{hsh1,hsh2}];
  35.             }
  36.         }
  37.         cout<<ans<<endl;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment