Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #include <string>
- #define forn(I,B) for (int I=1;I<=(B);I++)
- #define rep(I,B) for (int I=0;I<(B);I++)
- #define N 111111
- using namespace std;
- int s[N],n,a[N],m;
- pair<long long,long long> t[N];
- long long sorted[N],ans;
- char buf[10];
- inline void upd(int pos){
- for (;pos<=n;pos=(pos|(pos-1))+1) ++s[pos];
- }
- inline int get_sum(int pos){
- int res=0;
- for (;pos;pos&=pos-1) res+=s[pos];
- return res;
- }
- int main(){
- scanf("%d\n",&n);
- forn(i,n){
- gets(buf);
- m=strlen(buf);
- rep(j,m){
- t[i].first=t[i].first*27ll+buf[j]-'a'+1;
- t[i].second=t[i].second*27ll+buf[m-j-1]-'a'+1;
- }
- rep(j,10-m){
- t[i].first*=27ll;
- t[i].second*=27ll;
- }
- sorted[i]=t[i].second;
- }
- sort(t+1,t+n+1);
- sort(sorted+1,sorted+n+1);
- forn(i,n) a[i]=lower_bound(sorted+1,sorted+n+1,t[i].second)-sorted;
- forn(i,n){
- ans+=get_sum(a[i]);
- upd(a[i]);
- }
- printf("%I64d\n",n*1ll*(n-1)/2-ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment