Tranvick

Croatian

Apr 9th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <string>
  4. #define forn(I,B) for (int I=1;I<=(B);I++)
  5. #define rep(I,B) for (int I=0;I<(B);I++)
  6. #define N 111111
  7. using namespace std;
  8.  
  9. int s[N],n,a[N],m;
  10. pair<long long,long long> t[N];
  11. long long sorted[N],ans;
  12. char buf[10];
  13.  
  14. inline void upd(int pos){
  15.     for (;pos<=n;pos=(pos|(pos-1))+1) ++s[pos];
  16. }
  17.  
  18. inline int get_sum(int pos){
  19.     int res=0; 
  20.     for (;pos;pos&=pos-1) res+=s[pos];
  21.     return res;
  22. }
  23.  
  24. int main(){
  25.     scanf("%d\n",&n);
  26.     forn(i,n){
  27.         gets(buf);
  28.         m=strlen(buf);
  29.         rep(j,m){
  30.             t[i].first=t[i].first*27ll+buf[j]-'a'+1;
  31.             t[i].second=t[i].second*27ll+buf[m-j-1]-'a'+1;
  32.         }
  33.         rep(j,10-m){
  34.             t[i].first*=27ll;
  35.             t[i].second*=27ll;
  36.         }
  37.         sorted[i]=t[i].second;
  38.     }
  39.     sort(t+1,t+n+1);
  40.     sort(sorted+1,sorted+n+1);
  41.     forn(i,n) a[i]=lower_bound(sorted+1,sorted+n+1,t[i].second)-sorted;
  42.     forn(i,n){
  43.         ans+=get_sum(a[i]);
  44.         upd(a[i]);
  45.     }
  46.     printf("%I64d\n",n*1ll*(n-1)/2-ans);
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment