fork download
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long LL;
  6.  
  7. const int N = 200100;
  8. int a[N], b[N], num[N];
  9. int m, n, tn, tmp[N], T[N];
  10. LL ans;
  11.  
  12. int find(int x) {int ret = 0; for (; x; x -= (x & -x)) ret += T[x]; return ret;}
  13. void plus(int x) {for (; x <= n; x += (x & -x)) T[x]++;}
  14.  
  15. int main()
  16. {
  17.  
  18. scanf("%d", &m);
  19. for (int i = 1; i <= m; i++) scanf("%d%d", &a[i], &b[i]), tmp[++tn] = a[i], tmp[++tn] = b[i];
  20. sort(tmp + 1, tmp + 1 + tn);
  21. tn = unique(tmp + 1, tmp + 1 + tn) - tmp - 1;
  22.  
  23. n = tn;
  24. for (int i = 1; i <= n; i++) num[i] = i;
  25. for (int i = 1; i <= m; i++)
  26. {
  27. a[i] = lower_bound(tmp + 1, tmp + 1 + tn, a[i]) - tmp;
  28. b[i] = lower_bound(tmp + 1, tmp + 1 + tn, b[i]) - tmp;
  29.  
  30. swap(num[a[i]], num[b[i]]);
  31. }
  32.  
  33. ans = 0;
  34. for (int i = n; i >= 1; i--)
  35. {
  36. ans = ans + find(num[i] - 1);
  37. plus(num[i]);
  38. }
  39.  
  40. for (int i = 1; i <= n; i++)
  41. if (num[i] != i)
  42. {
  43. if (num[i] > i)
  44. {
  45. ans = ans + (tmp[num[i]] - tmp[i]) - (num[i] - i);
  46. } else
  47. {
  48. ans = ans + (tmp[i] - tmp[num[i]]) - (i - num[i]);
  49. }
  50. }
  51.  
  52. printf("%I64d\n", ans);
  53.  
  54. return 0;
  55. }
  56.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:37:3: error: reference to 'plus' is ambiguous
   plus(num[i]);
   ^
prog.cpp:13:6: note: candidates are: void plus(int)
 void plus(int x) {for (; x <= n; x += (x & -x)) T[x]++;}
      ^
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/random:40,
                 from /usr/include/c++/5/bits/stl_algo.h:66,
                 from /usr/include/c++/5/algorithm:62,
                 from prog.cpp:3:
/usr/include/c++/5/bits/stl_function.h:147:12: note:                 template<class _Tp> struct std::plus
     struct plus;
            ^
stdout
Standard output is empty