fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using ld = long double;
  4. #define int long long
  5. #define endl '\n'
  6. #define all(x) x.begin(), x.end()
  7.  
  8. const int mod = 1e9 + 7;
  9.  
  10. template <class T>
  11. struct Point {
  12. typedef Point P;
  13. T x, y;
  14. explicit Point(T x = 0, T y = 0) : x(x), y (y) {}
  15. bool operator==(P p) const {return tie(x, y) == tie(p.x, p.y); }
  16. bool operator<(P p) const {return tie(x, y) < tie(p.x, p.y); }
  17. P operator+(P p) const {return P(x+p.x, y+p.y);}
  18. P operator-(P p) const {return P(x-p.x, y-p.y);}
  19. T cross(P p) const {return x*p.y - y*p.x;}
  20. T dot(P p) const {return x*p.x + y*p.y;}
  21. T cross(P a, P b) const {return (a-*this).cross(b-*this);}
  22. T dist2() {return x*x + y*y; }
  23. };
  24.  
  25. using P = Point<int>;
  26.  
  27. bool half(P p) {
  28. return p.y < 0 || (p.y == 0 && p.x < 0);
  29. }
  30.  
  31. struct FT {
  32. int n; vector<int> s;
  33. FT(int n) : n(n+1) , s(n+1) {}
  34. void update(int pos, int dif) {
  35. for(; pos < n; pos += pos & -pos) s[pos] += dif;
  36. }
  37. int query(int pos) {
  38. int sm = 0;
  39. for(; pos > 0; pos -= pos & -pos) sm += s[pos];
  40. return sm;
  41. }
  42. int query(int l, int r) {
  43. return query(l) - query(l-1);
  44. }
  45. };
  46.  
  47. void polar_sort(vector<P> & v, P o = P(0,0)) {
  48. sort(v.begin()+1, v.end(), [&](P a, P b) {
  49. P u = a - o, w = b - o;
  50. return make_tuple(half(u), 0, u.dist2()) < make_tuple(half(w), u.cross(w), w.dist2());
  51. });
  52. }
  53.  
  54. void solve() {
  55. int n, d; cin >> n >> d;
  56. vector<int> w(n+1);
  57.  
  58. map<P, int> ind;
  59. vector<P> pts(n+1);
  60. P cen1 = P(0,0), cen2 = P(d, 0);
  61. for(int i = 1; i <= n; i++) {
  62. cin >> pts[i].x >> pts[i].y >> w[i];
  63. ind[pts[i]] = i;
  64. }
  65. vector<int> aa(n+1), bb(n+1);
  66. polar_sort(pts, cen1);
  67. for(int i = 1; i <= n; i++) {
  68. aa[i] = ind[pts[i]];
  69. }
  70. polar_sort(pts, cen2);
  71. for(int i = 1; i <= n; i++) {
  72. bb[i] = ind[pts[i]];
  73. }
  74. reverse(bb.begin()+1, bb.end());
  75.  
  76. // for(int i = 1; i <= n; i++) cout << aa[i] << ' ';
  77. // cout << '\n';
  78. // for(int i = 1; i <= n; i++) cout << bb[i] << ' ';
  79. // cout << '\n';
  80.  
  81. vector<int> nw(n+1);
  82. vector<int> perm(n+1);
  83. for(int i = 1; i <= n; i++) {
  84. perm[aa[i]] = i;
  85. nw[i] = w[aa[i]];
  86. }
  87. for(int i = 1; i <= n; i++) {
  88. bb[i] = perm[bb[i]];
  89. }
  90. for(int i = 1; i <= n; i++) {
  91. cout << bb[i] << ' ';
  92. }
  93. cout << '\n';
  94.  
  95. int ans = 0;
  96. FT ft(n+5);
  97. for(int i = n; i >= 1; i--) {
  98. ans += ft.query(1, bb[i]-1) * bb[i];
  99. ft.update(bb[i], nw[i]);
  100. }
  101. cout << ans << '\n';
  102.  
  103. }
  104.  
  105. signed main() {
  106. ios_base::sync_with_stdio(0); cin.tie(nullptr);
  107. int tc = 1;
  108. // cin >> tc;
  109. for(int i = 1; i <= tc; i++) {
  110. solve();
  111. }
  112. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
0