This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int n,sol;
map<pair<string,int>,int> m; //prefix, up/down, id
map<pair<string,string>,int> ob;
map<pair<string,int>,int> mf; //prefix, up/down, id
map<pair<string,string>,int> obf;
map<pair<string,string>,int> isti;
map<pair<char,char>,int> gay1, gay2, gay3, gay4, hm;
pair<string,string> Flip(string x, string y){
    return {string()+y[0]+x[0], string()+y[1]+x[1]};
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 0;i<n;i++){
        string a,b; cin >> a >> b;
        sol+=m[{a,1}]+m[{b,0}]-ob[{b,a}];
        m[{a,0}]++;
        m[{b,1}]++;
        ob[{a,b}]++;
        pair<string,string> f = Flip(a,b);
        a = f.X; b = f.Y;
        sol+=mf[{a,1}]+mf[{b,0}]-obf[{b,a}];
        mf[{a,0}]++;
        mf[{b,1}]++;
        obf[{a,b}]++;
        if(b[0]==a[1]){
            sol-=gay4[{b[1],b[0]}];
            sol-=gay1[{b[0],a[0]}];
            gay1[{b[1],b[0]}]++;
            gay4[{b[0],a[0]}]++;
            if(a[0]==b[1]){
                sol+=hm[{a[1],a[0]}];
                hm[{a[0],a[1]}]++;
            }
        }
        if(a[0]==b[1]){
            sol-=gay3[{a[1],a[0]}];
            sol-=gay2[{a[0],b[0]}];
            gay2[{a[1],a[0]}]++;
            gay3[{a[0],b[0]}]++;
            if(a[1]==b[0]){
                sol+=hm[{a[1],a[0]}];
//                hm[{a[0],a[1]}]++;
            }
        }
//        if(a[0]==a[1] && a[1] == b[0] && b[0] == b[1]){
//            sol+=isti[{a,b}];
//            isti[{a,b}]++;
//        }
    }
    cout << sol;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |