usaco

Clean implementations of solutions to USACO problems

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

struct point {
	ll x, y;
	point() { x = y = 0; }
	point(ll _x, ll _y) : x(_x), y(_y) {}
	bool operator < (point p) const {
		return (x == p.x && y < p.y) || x < p.x;
	}
	bool operator == (point p) const {
		return x == p.x && y == p.y;
	}
} T[305];

struct vec {
	ll x, y;
	vec(ll _x, ll _y) : x(_x), y(_y) {}
};

vec to_vec(point a, point b) {
	return vec(b.x - a.x, b.y - a.y);
}

ll dot(vec a, vec b) {
	return (a.x * b.x + a.y * b.y);
}

ll norm_sq(vec v) {
	return v.x * v.x + v.y * v.y;
}

ll cross(vec a, vec b) {
	return a.x * b.y - a.y * b.x;
}

bool ccw(point p, point q, point r) {
	return cross(to_vec(p, q), to_vec(p, r)) > 0;
}

struct line { ll a, b, c; };

line to_line(point p1, point p2) {
	return { p2.y - p1.y, p1.x - p2.x, p1.x * p2.y - p1.y * p2.x };
}

class fenwick_tree {
private: vector<int> FT;
public:
	fenwick_tree(int N) { FT.assign(N + 1, 0); }
	void update(int x, int val) { for (; x < FT.size(); x += x & -x) FT[x] += val; }
	int query(int x) { int ret = 0; for (; x > 0; x -= x & -x) ret += FT[x]; return ret; }
	int query(int x, int y) { return query(y) - (x == 1 ? 0 : query(x - 1)); }
};

int ans[305] = { 0 };

int main() {
	ifstream cin("triangles.in");
	ofstream cout("triangles.out");
	
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) cin >> T[i].x >> T[i].y;
	sort(T, T + N);

	for (int i = 0; i < N - 1; i++) {
		for (int j = i + 1; j < N; j++) {
			line L = to_line(T[i], T[j]);

			vector<pair<ii, point>> A, B;
			for (int k = i + 1; k < N; k++) {
				if (k != i && k != j) {
					if (T[k].x * L.a + T[k].y * L.b < L.c) {
						A.emplace_back(ii(k, k), T[k]);
					}
				}
			}

			vector<ii> l, r;
			int N = A.size();
			for (int i = 0; i < N; i++) {
				l.emplace_back(A[i].first.first, i);
				r.emplace_back(A[i].first.second, i);
			}
			sort(l.begin(), l.end(), [i](const ii& a, const ii& b) { return ccw(T[b.first], T[i], T[a.first]); });
			sort(r.begin(), r.end(), [j](const ii& a, const ii& b) { return ccw(T[a.first], T[j], T[b.first]); });
			for (int i = 0; i < N; i++) {
				A[l[i].second].first.first = i + 1;
				A[r[i].second].first.second = i + 1;
			}

			sort(A.begin(), A.end());

			fenwick_tree FT(N);
			for (auto& p : A) {
				ans[FT.query(p.first.second)]++;
				FT.update(p.first.second, 1);
			}
		}
	}

	for (int i = 0; i < N - 2; i++) cout << ans[i] << endl;
}