text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10, MAXK = 5005;
long long n, k, a[MAXN], dp[MAXK][MAXK], tmp;
int pos(int up, int lo) { return up * ((n + k - 1) / k) + lo * (n / k); }
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 1; i <= k; i++)
for (int j = 0; j <= k - n % k; j++) {
dp[i][j] = 1LL << 50;
if (j > i) break;
tmp = pos(i - j, j - 1);
if (j > 0 && tmp + (n / k) - 1 < n)
dp[i][j] =
min(dp[i][j], dp[i - 1][j - 1] + a[tmp + (n / k) - 1] - a[tmp]);
tmp = pos(i - j - 1, j);
if (i - j - 1 >= 0 && tmp + ((n + k - 1) / k) - 1 < n)
dp[i][j] = min(dp[i][j],
dp[i - 1][j] + a[tmp + ((n + k - 1) / k) - 1] - a[tmp]);
}
cout << dp[k][k - n % k];
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, a[300010], k, f, t;
long long num1, num2, dp[5010][5010], len1, len2;
int main() {
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
len1 = n / k;
len2 = n / k + 1;
num1 = n % k;
num2 = k - n % k;
for (long long i = 0; i <= num1; i++) {
for (long long j = 0; j <= num2; j++) {
if (i == 0 && j == 0) continue;
dp[i][j] = 1e17;
t = i * len2 + j * len1;
if (i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[t] - a[t - len1]);
if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[t] - a[t - len1 + 1]);
}
}
printf("%lld", dp[num1][num2]);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 * 1000 * 5;
const int MAX = 1e18;
int a[N];
int main() {
int n, k;
cin >> n >> k;
int dp[k + 10][n % k + 10];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int q = n / k, w = n % k;
for (int i = 0; i <= k; i++) {
for (int j = 0; j <= w; j++) {
dp[i][j] = MAX;
}
}
dp[0][0] = 0;
int g;
for (int i = 1; i <= k; i++) {
for (int j = 0; j <= w; j++) {
g = i * q + j - 1;
if (j > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + a[g] - a[g - q]);
if (i > j) dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[g] - a[g - q + 1]);
}
}
cout << dp[k][w];
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 300000 + 5;
const long long maxk = 5000 + 5;
const long long INF = (1LL << 60);
long long a[maxn], dp[maxk][maxk];
int main() {
long long n, k;
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) scanf("%lld", a + i);
sort(a + 1, a + n + 1);
long long L = n % k, S = k - L, sz = n / k;
for (long long i = 0; i <= S; i++)
for (long long j = 0; j <= L; j++) dp[i][j] = INF;
dp[0][0] = 0;
for (long long i = 0; i <= S; i++)
for (long long j = 0; j <= L; j++) {
long long p = i * (sz) + j * (sz + 1) + 1;
if (p + sz - 1 <= n)
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[p + sz - 1] - a[p]);
if (p + sz <= n)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a[p + sz] - a[p]);
}
printf("%lld\n", dp[S][L]);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 300111;
int n, k;
long long a[MN], sum[MN];
long long f[2][5011];
int main() {
ios ::sync_with_stdio(false);
while (cin >> n >> k) {
for (int i = (1), _b = (n); i <= _b; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = (2), _b = (n); i <= _b; i++) sum[i] = a[i] - a[i - 1];
for (int i = (2), _b = (n); i <= _b; i++) sum[i] = sum[i - 1] + sum[i];
int each = n / k;
int good = n % k;
for (int t = 0, _a = (2); t < _a; t++)
for (int i = (0), _b = (good); i <= _b; i++) f[t][i] = 1000111000111000LL;
f[0][0] = 0;
for (int i = (1), _b = (k); i <= _b; i++) {
int t = i % 2;
for (int j = (0), _b = (good); j <= _b; j++) {
int used = i * each + j;
f[t][j] = f[1 - t][j] + sum[used] - sum[used - each + 1];
if (j > 0) {
f[t][j] =
min(f[t][j], f[1 - t][j - 1] + sum[used] - sum[used - each]);
}
}
}
cout << f[k % 2][good] << endl;
}
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int MAXN = 300000 + 4;
int data[MAXN], n, k;
long long sum[MAXN];
bool cmp(int a, int b) { return a < b; }
long long dp[5000 + 5][5000 + 5];
int main() {
while (scanf("%d%d", &n, &k) != EOF) {
for (int i = 1; i <= n; i++) scanf("%d", &data[i]);
sort(data + 1, data + n + 1, cmp);
sum[0] = 0;
data[0] = data[1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + data[i] - data[i - 1];
}
for (int i = 0; i <= k; i++)
for (int j = 0; j <= k; j++) dp[i][j] = -1;
int up1, up2;
up1 = k - n % k;
up2 = n % k;
int len = n / k;
dp[0][0] = 0;
for (int i = 0; i <= up1; i++) {
for (int j = 0; j <= up2; j++) {
if (i == 0 && j == 0) continue;
if (i > 0) {
if (dp[i][j] == -1)
dp[i][j] = dp[i - 1][j] + sum[len * i + (len + 1) * j] -
sum[len * (i - 1) + (len + 1) * j + 1];
else
dp[i][j] =
((dp[i][j]) < (dp[i - 1][j] + sum[len * i + (len + 1) * j] -
sum[len * (i - 1) + (len + 1) * j + 1])
? (dp[i][j])
: (dp[i - 1][j] + sum[len * i + (len + 1) * j] -
sum[len * (i - 1) + (len + 1) * j + 1]));
}
if (j > 0) {
if (dp[i][j] == -1)
dp[i][j] = dp[i][j - 1] + sum[len * i + (len + 1) * j] -
sum[len * i + (len + 1) * (j - 1) + 1];
else
dp[i][j] =
((dp[i][j]) < (dp[i][j - 1] + sum[len * i + (len + 1) * j] -
sum[len * i + (len + 1) * (j - 1) + 1])
? (dp[i][j])
: (dp[i][j - 1] + sum[len * i + (len + 1) * j] -
sum[len * i + (len + 1) * (j - 1) + 1]));
}
}
}
printf("%I64d\n", dp[up1][up2]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long l = n % k, c = k - l, dis = n / k;
long long d[c + 1][l + 1];
for (int i = 0; i <= c; i++)
for (int j = 0; j <= l; j++) d[i][j] = 99999999999;
d[0][1] = a[n - 1] - a[0];
d[1][0] = a[n - 1] - a[0];
for (int i = 0; i <= c; i++)
for (int j = 0; j <= l; j++) {
dis = i * (n / k) + j * (n / k + 1);
if (i + 1 <= c and dis < n and dis > 0)
d[i + 1][j] = min(d[i + 1][j], d[i][j] - a[dis] + a[dis - 1]);
if (j + 1 <= l and dis < n and dis > 0)
d[i][j + 1] = min(d[i][j + 1], d[i][j] - a[dis] + a[dis - 1]);
}
cout << d[c][l] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
const int MAXK = 5005;
const long long INF = (long long)2e15;
int N, K;
int a[MAXN];
long long dp[MAXK][3];
void load() {
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++) scanf("%d", a + i);
}
long long solve() {
sort(a, a + N);
int c = 0;
if (N < 2 * K) {
for (int i = 1; i <= N - K; i++) dp[i][1] = dp[i][2] = INF;
c = 2;
for (int i = 2; i <= N; i++) {
c = (c + 1) % 3;
for (int j = 0; j <= N - K; j++) {
dp[j][c] = dp[j][(c + 2) % 3];
if (j)
dp[j][c] = min(dp[j][c], dp[j - 1][(c + 1) % 3] +
(long long)(a[i - 1] - a[i - 2]));
}
}
} else {
for (int i = 0; i <= K - N % K; i++) {
c ^= 1;
for (int j = 0; j <= N % K; j++) {
int hi = (i + j) * (N / K) + j;
if (i || j) dp[j][c] = INF;
if (i) dp[j][c] = dp[j][c ^ 1] + (long long)(a[hi - 1] - a[hi - N / K]);
if (j)
dp[j][c] =
min(dp[j][c],
dp[j - 1][c] + (long long)(a[hi - 1] - a[hi - N / K - 1]));
}
}
}
return dp[N % K][c];
}
int main() {
load();
printf("%I64d\n", solve());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300300, M = 5050;
const long long oo = 1000000000000000000ll;
int i, j, k, n, m, ch, ff, t, l;
int a[N];
long long f[M][M];
void R(int &x) {
x = ff = 0;
ch = getchar();
while (ch < '0' || '9' < ch) {
if (ch == '-') ff = 1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
if (ff) x = -x;
}
void W(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) W(x / 10);
putchar(x % 10 + '0');
}
int min(const int &x, const int &y) {
if (x < y) return x;
return y;
}
long long min(const long long &x, const long long &y) {
if (x < y) return x;
return y;
}
int main() {
R(n);
R(m);
memset(f, 60, sizeof f);
for (i = 1; i <= n; i++) R(a[i]);
sort(a + 1, a + n + 1);
t = n % m;
l = n / m;
f[0][0] = 0;
for (i = 0; i < m; i++)
for (j = min(t, i); j >= 0; j--)
if (f[i][j] < oo) {
if (j + 1 <= t)
f[i + 1][j + 1] =
min(f[i + 1][j + 1],
f[i][j] + a[(i + 1) * l + j + 1] - a[i * l + j + 1]);
f[i + 1][j] = f[i][j] + a[(i + 1) * l + j] - a[i * l + j + 1];
}
W(f[m][t]);
puts("");
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
const int MAXK = 5005;
int n, k, A[MAXN];
long long dp[2][MAXK];
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
sort(A, A + n);
int G1 = n % k, G2 = k - (n % k);
for (int a = 0; a <= G1; a++)
for (int b = 0; b <= G2; b++) {
int i = ((G1 - a) * ((n + k - 1) / k) + (G2 - b) * (n / k));
if (i == n)
dp[a & 1][b] = 0;
else {
dp[a & 1][b] = 1LL << 60;
if (a > 0)
dp[a & 1][b] =
min(dp[a & 1][b],
dp[(a - 1) & 1][b] + A[i + ((n + k - 1) / k) - 1] - A[i]);
if (b > 0)
dp[a & 1][b] =
min(dp[a & 1][b], dp[a & 1][b - 1] + A[i + (n / k) - 1] - A[i]);
}
}
printf("%I64d\n", dp[G1 & 1][G2]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, b, t, i, j, temp, temp1, ans1, ans2, f[5010][5010], a[300010];
long long min(long long a, long long b) { return a < b ? a : b; }
int main() {
scanf("%lld%lld", &n, &k);
t = n / k;
b = n % k;
if (b == 0) {
b = k;
t--;
}
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
for (i = 0; i <= b; i++)
for (j = 0; j <= k - b; j++) {
temp = n - i * (t + 1) - j * t;
if (i == 0 && j == 0) continue;
if (j != 0) {
temp1 = n - i * (t + 1) - (j - 1) * t;
ans2 = f[i][j - 1] + a[temp1] - a[temp + 1];
}
if (i != 0) {
temp1 = n - (i - 1) * (t + 1) - j * t;
ans1 = f[i - 1][j] + a[temp1] - a[temp + 1];
}
if (i == 0)
f[i][j] = ans2;
else if (j == 0)
f[i][j] = ans1;
else
f[i][j] = min(ans1, ans2);
}
printf("%lld\n", f[b][k - b]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int K = 5001;
long long dp[K][K];
bool vis[K][K];
int arr[N];
int n, k;
int besar, kecil;
int cbesar, ckecil;
long long solve(int a, int b) {
if (a == cbesar && b == ckecil) return 0;
if (vis[a][b]) return dp[a][b];
vis[a][b] = 1;
int start = a * besar + b * kecil + 1;
long long &ret = dp[a][b];
ret = (long long)1e18;
if (a < cbesar) ret = solve(a + 1, b) + arr[start + besar - 1] - arr[start];
if (b < ckecil)
ret = min(ret, solve(a, b + 1) + arr[start + kecil - 1] - arr[start]);
return ret;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
sort(arr + 1, arr + n + 1);
for (int i = 1; i <= n; i += k) besar++;
for (int j = k; j <= n; j += k) kecil++;
for (int i = 1; i <= k; i++) {
int cnt = 0;
for (int j = i; j <= n; j += k) cnt++;
if (cnt == besar)
cbesar++;
else
ckecil++;
}
printf("%I64d\n", solve(0, 0));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int nax = 3e5 + 7;
const long long INF = 1e18;
long long n, k, a[nax] = {0}, b[nax] = {0}, prf[nax] = {0}, dp[5003][5003], l,
m;
long long sum(long long x, long long du, int ch) {
if (l + ch == 1) {
return 0;
}
long long st = (x - 1) * l + du;
long long en = st + l + ch - 2;
long long res = prf[en] - ((st >= 1) ? prf[st - 1] : 0);
return res;
}
int main() {
for (int i = 0; i < 5003; ++i) {
for (int j = 0; j < 5003; ++j) dp[i][j] = INF;
}
cin >> n >> k;
l = (n) / k, m = n % k;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n - 1; ++i) b[i] = a[i + 1] - a[i];
prf[0] = b[0];
for (long long i = 1; i < n - 1; ++i) prf[i] = prf[i - 1] + b[i];
dp[0][0] = 0;
for (long long i = 1; i <= k; ++i) {
for (long long j = 0; j <= (min(i, m)); ++j) {
if (j >= 1) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + sum(i, j - 1, 1));
if (i - 1 >= j) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + sum(i, j, 0));
}
} else {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + sum(i, j, 0));
}
}
}
cout << dp[k][m] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 1;
const long long INF = 4e18;
long long n, k, l, x, y, ans, a[maxn], b[maxn], dp[5001][5001];
bool mark[maxn];
vector<long long> v;
long long solve(int i, int j) {
if (dp[i][j] != -1) return dp[i][j];
long long s1 = (long long)1e14, s2 = (long long)1e14, s = i * x + j * y;
if (i != 0) s1 = (long long)solve(i - 1, j) + abs(a[s - 1] - a[s - x]);
if (j != 0) s2 = (long long)solve(i, j - 1) + abs(a[s - 1] - a[s - y]);
return dp[i][j] = min(s1, s2);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < 5000; i++)
for (int j = 0; j < 5000; j++) dp[i][j] = -1;
sort(a, a + n);
for (int i = 0; i < n; i++)
if (!mark[i]) {
l = 0;
for (int j = i; j < n; j += k) l++, mark[j] = 1;
v.push_back(l), b[l]++;
}
sort(v.begin(), v.end());
x = v[0], y = v[v.size() - 1];
if (x == y) {
for (int i = 0; i < n; i += x) ans += abs(a[i] - a[i + x - 1]);
return cout << ans, 0;
}
dp[0][0] = 0;
dp[0][1] = abs(a[0] - a[y - 1]);
dp[1][0] = abs(a[0] - a[x - 1]);
cout << solve(b[x], b[y]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 3e5 + 5;
int n, k;
long long x, dp[5005][5005], a[MAXN];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
a[0] = a[1];
a[n + 1] = a[n];
int len1 = n / k;
int len2 = n / k + 1;
int cnt1 = k - n % k;
int cnt2 = n % k;
for (int i = 0; i <= cnt1; i++) {
for (int j = 0; j <= cnt2; j++) {
int pos1 = (i - 1) * len1 + j * len2;
if (i)
dp[i][j] = (dp[i][j]) > (dp[i - 1][j] + a[pos1 + 1] - a[pos1])
? (dp[i][j])
: (dp[i - 1][j] + a[pos1 + 1] - a[pos1]);
int pos2 = i * len1 + (j - 1) * len2;
if (j)
dp[i][j] = (dp[i][j]) > (dp[i][j - 1] + a[pos2 + 1] - a[pos2])
? (dp[i][j])
: (dp[i][j - 1] + a[pos2 + 1] - a[pos2]);
}
}
cout << a[n] - a[1] - dp[cnt1][cnt2] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[300010];
long long int dp[5010][5010];
int main() {
memset(dp, 0, sizeof(dp));
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dp[0][0] = 0;
sort(a + 1, a + 1 + n);
int l1 = n / k + 1;
int l2 = n / k;
int l = n % k;
int s = k - l;
for (int i = 1; i <= l; i++)
dp[i][0] = dp[i - 1][0] + a[i * l1] - a[(i - 1) * l1 + 1];
for (int j = 1; j <= s; j++)
dp[0][j] = dp[0][j - 1] + a[j * l2] - a[(j - 1) * l2 + 1];
for (int i = 1; i <= l; i++) {
for (int j = 1; j <= s; j++) {
int ind = i * l1 + j * l2;
dp[i][j] = min(dp[i - 1][j] + a[ind] - a[ind - l1 + 1],
dp[i][j - 1] + a[ind] - a[ind - l2 + 1]);
}
}
cout << dp[l][s];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using Pii = pair<int, int>;
constexpr int MAXN = 300100;
constexpr int MAXM = 5010;
int a[MAXN];
i64 dp[MAXM][MAXM];
i64 solve(int n, int k) {
int v = n % k, u = k - v, d = n / k;
sort(a + 1, a + n + 1);
memset(dp, 0x3F, sizeof(dp));
for (int i = 0; i <= u; ++i) {
for (int j = 0; j <= v; ++j) {
if (!i && !j)
dp[i][j] = 0;
else {
int p = i * d + j * (d + 1);
if (i) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[p] - a[p - d + 1]);
}
if (j) {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[p] - a[p - d]);
}
}
}
}
return dp[u][v];
}
int main() {
int n, k;
while (~scanf("%d%d", &n, &k)) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
cout << solve(n, k) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int ma = 1024, mi = 200000000;
long long n, k, ans[5100][5100], p, p1, ansss;
vector<long long> a;
int main() {
iostream::sync_with_stdio(0);
cin >> n >> k;
p = n / k;
p1 = p;
if (n % k) p1++;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
ans[1][0] = a[p - 1] - a[0];
ansss = ans[1][0];
if (p1 - 1 < n) {
ans[1][1] = a[p1 - 1] - a[0];
ansss = ans[1][1];
}
for (int i = 2; i <= k; i++) {
for (int j = i * p; j <= min(i * p1, n); j++) {
ans[i][j - i * p] = 1000000000000000000;
if (j - p <= (i - 1) * p1)
ans[i][j - i * p] = ans[i - 1][j - i * p] + a[j - 1] - a[j - p];
if (j - (i - 1) * p - p1 >= 0)
ans[i][j - i * p] =
min(ans[i][j - i * p],
ans[i - 1][j - (i - 1) * p - p1] + a[j - 1] - a[j - p1]);
ansss = ans[i][j - i * p];
}
}
cout << ansss;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 1;
const int K = 5000;
int mini[K][K], a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
int l = n / k, r = n % k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i <= r; ++i) {
for (int j = 0; j <= k - r; ++j) {
int prefix = i * (l + 1) + j * l;
mini[i][j] = (i == 0 && j == 0) ? 0 : INT_MAX;
if (i > 0) {
mini[i][j] =
min(mini[i][j], mini[i - 1][j] + a[prefix - 1] - a[prefix - l - 1]);
}
if (j > 0) {
mini[i][j] =
min(mini[i][j], mini[i][j - 1] + a[prefix - 1] - a[prefix - l]);
}
}
}
cout << mini[r][k - r] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, n1, n2, alpha;
long long a[300003];
long long dp[5005][5005];
long long memo(int need, int tp1) {
int tp2 = (k - need) - tp1;
if (need == 0) return 0;
if (dp[need][tp1] != -1) return dp[need][tp1];
int idx = n - 1 - tp1 * alpha - tp2 * (alpha + 1);
long long res1 = (long long)(1e15), res2 = (long long)(1e15);
if (tp1 < n1) {
int _idx = idx - alpha + 1;
res1 = min(res1, a[idx] - a[_idx] + memo(need - 1, tp1 + 1));
}
if (tp2 < n2) {
int _idx = idx - alpha;
res2 = min(res2, a[idx] - a[_idx] + memo(need - 1, tp1));
}
return dp[need][tp1] = min(res1, res2);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof dp);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long ans = 0;
alpha = n / k;
n2 = n - (n / k) * k;
n1 = k - n2;
cout << memo(k, 0) << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, dp[5005][5005], sizek, num, a[500005];
int main() {
cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
sizek = n / k;
num = n % k;
sort(a + 1, a + 1 + n);
for (int i = 0; i <= 5000; i++) {
for (int j = 0; j <= 5000; j++) dp[i][j] = LLONG_MAX;
}
dp[0][0] = 0;
for (int i = 1; i <= k; i++) {
for (int j = 0; j <= num; j++) {
long long posy = i * sizek + j, posx = (i - 1) * sizek + 1 + j,
posy1 = (i - 1) * sizek + j;
if (dp[i - 1][j] != LLONG_MAX)
dp[i][j] = dp[i - 1][j] + a[posy] - a[posx];
if (j && dp[i - 1][j - 1] != LLONG_MAX)
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + a[posy] - a[posy1]);
}
}
cout << dp[k][num];
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
int upmin(T1 &x, T2 v) {
if (x > v) {
x = v;
return 1;
}
return 0;
}
template <class T1, class T2>
int upmax(T1 &x, T2 v) {
if (x < v) {
x = v;
return 1;
}
return 0;
}
const int INF = 0x3f3f3f3f;
int N, K;
vector<int> A;
void init() {
cin >> N >> K;
A = vector<int>(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
}
vector<vector<long long> > dp;
void preprocess() {
sort(A.begin(), A.end());
int small_sz = N / K;
int large_cnt = N % K;
int small_cnt = K - large_cnt;
assert(small_sz * small_cnt + (small_sz + 1) * large_cnt == N);
dp = vector<vector<long long> >(small_cnt + 1,
vector<long long>(large_cnt + 1, 1LL << 62));
dp[0][0] = 0;
for (int i = 0; i <= small_cnt; ++i) {
for (int j = 0; j <= large_cnt; ++j) {
int st = i * small_sz + j * (small_sz + 1);
if (i + 1 <= small_cnt) {
upmin(dp[i + 1][j], dp[i][j] + abs(A[st + small_sz - 1] - A[st]));
}
if (j + 1 <= large_cnt) {
upmin(dp[i][j + 1], dp[i][j] + abs(A[st + small_sz] - A[st]));
}
}
}
cout << dp[small_cnt][large_cnt] << endl;
}
void solve() {}
signed main() {
ios::sync_with_stdio(0);
init();
preprocess();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 10, MAXK = 5010;
int N, K, L, a[MAXN], f[MAXK][MAXK];
int main() {
scanf("%d%d", &N, &K);
L = N / K;
for (int i = 0; i < N; ++i) scanf("%d", &a[i]);
sort(a, a + N);
memset(f, -1, sizeof(f));
f[0][0] = 0;
for (int n = 1; n <= K; ++n)
for (int m = 0; m <= n; ++m) {
if (f[n - 1][m] != -1)
f[n][m] = f[n - 1][m] + a[n * L + m - 1] - a[(n - 1) * L + m];
if (m) {
int x = f[n - 1][m - 1] + a[n * L + m - 1] - a[(n - 1) * L + m - 1];
f[n][m] = f[n][m] == -1 ? x : min(f[n][m], x);
}
}
printf("%d\n", f[K][N % K]);
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int N, K, a[300010], dp[5010][5010];
int main() {
N = read(), K = read();
for (int i = 1; i <= N; i++) a[i] = read();
sort(a + 1, a + N + 1);
int sz1 = N % K, sz2 = K - N % K, len1 = N / K + 1, len2 = N / K;
a[0] = a[1];
for (int i = 0; i <= sz1; i++)
for (int j = 0, k; j <= sz2; j++)
k = (i - 1) * len1 + j * len2,
dp[i][j] =
max(dp[i][j], (i ? (dp[i - 1][j] + a[k + 1] - a[k]) : dp[i][j])),
k = i * len1 + (j - 1) * len2,
dp[i][j] =
max(dp[i][j], (j ? (dp[i][j - 1] + a[k + 1] - a[k]) : dp[i][j]));
printf("%d\n", a[N] - a[1] - dp[sz1][sz2]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300009;
map<short, int> f[maxn];
int a[maxn], n, K, p, q, d;
int sum[maxn], i, j, k;
int main() {
scanf("%d%d", &n, &K);
d = n / K;
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
for (i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i] - a[i - 1];
f[0][0] = 0;
for (i = 1; i <= n; i++) {
for (k = 0; k <= 1; k++) {
if (i >= d)
for (map<short, int>::iterator j = f[i - d].begin();
j != f[i - d].end(); j++)
if (j->first + k <= n % K) {
if (!f[i].count(j->first + k))
f[i][j->first + k] = j->second + sum[i] - sum[i - d + 1];
else
f[i][j->first + k] =
min(f[i][j->first + k], j->second + sum[i] - sum[i - d + 1]);
}
d++;
}
if (i >= d) f[i - d].clear();
d -= 2;
}
printf("%d", f[n][n % K]);
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10000000000")
using namespace std;
vector<long long> v;
int a, b, d;
int n, k;
long long memo[5001][5001];
long long f(int first, int second) {
if (first > a || second > b) return (1LL << 60);
if (first == a && second == b) return 0;
long long &ret = memo[first][second];
if (ret != -1) return ret;
long long res = (1LL << 60);
int idx = first * (d + 1) + second * d;
int idx2 = idx + d - 1;
res = min(res, v[idx2] - v[idx] + f(first, second + 1));
idx2 = idx + d;
if (idx2 < v.size()) {
res = min(res, v[idx2] - v[idx] + f(first + 1, second));
}
return ret = res;
}
int main() {
memset((memo), (-1), sizeof(memo));
cin >> n >> k;
for (int(i) = 0; (i) < (n); (i)++) {
long long p;
cin >> p;
v.push_back(p);
}
a = n % k;
b = k - a;
d = n / k;
sort((v).begin(), (v).end());
cout << f(0, 0) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long int dp[5005][5005];
int main() {
long long int n, a[300005], k, lg, sg, x;
scanf("%lld %lld", &n, &k);
;
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
sort(a, a + n);
lg = n % k;
sg = k - n % k;
x = n / k;
for (int i = 1; i <= sg; i++)
dp[0][i] = dp[0][i - 1] + (a[i * x - 1] - a[(i - 1) * x]);
for (int i = 1; i <= lg; i++)
dp[i][0] = dp[i - 1][0] + (a[i * (x + 1) - 1] - a[(i - 1) * (x + 1)]);
for (int i = 1; i <= lg; i++)
for (int j = 1; j <= sg; j++)
dp[i][j] = min(dp[i][j - 1] + (a[i * (x + 1) + (j - 1) * x + x - 1] -
a[i * (x + 1) + (j - 1) * x]),
dp[i - 1][j] + (a[(i - 1) * (x + 1) + j * x + x] -
a[(i - 1) * (x + 1) + j * x]));
printf("%lld\n", dp[lg][sg]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6 * 1e5 + 5;
int a[N];
long long dp[5005][5005];
int main() {
std::ios::sync_with_stdio(false);
memset(dp, 120, sizeof(dp));
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
sort(a, a + n);
int x = n % k;
int y = k - x;
int z = n / k;
dp[0][0] = 0;
for (int i = 0; i <= x; ++i) {
for (int j = 0; j <= y; ++j) {
int k = z * (i + j) + i;
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(a[k + z - 1] - a[k]));
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(a[k + z] - a[k]));
}
}
cout << dp[x][y] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> v1;
long long dp[5001][5001];
long long sum1[300001];
long long f(int index, int length1, int i, int j) {
long long answer1 = sum1[index + length1];
answer1 = answer1 - sum1[index + 1];
return answer1;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
v1.push_back(-INT_MAX);
for (int(i) = (0); (i) < (n); ++(i)) {
int temp;
scanf("%d", &temp);
v1.push_back(temp);
}
sort(v1.begin(), v1.end());
for (int(i) = (2); (i) < (n + 1); ++(i)) {
sum1[i] = sum1[i - 1] + abs(v1[i] - v1[i - 1]);
}
for (int(i) = (0); (i) < (k + 1); ++(i)) {
for (int(j) = (0); (j) < (k + 1); ++(j)) {
dp[i][j] = INT_MAX;
}
}
int l1 = n / k;
int l2 = n / k + 1;
int cnt2 = n % k;
int cnt1 = (n - cnt2 * l2) / l1;
for (int(i) = (0); (i) < (cnt1 + 1); ++(i)) {
for (int(j) = (0); (j) < (cnt2 + 1); ++(j)) {
if (i == 0 && j == 0) {
dp[i][j] = 0;
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] + f(i * l1 + (j - 1) * l2, l2, i, j);
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] + f((i - 1) * l1 + j * l2, l1, i, j);
} else {
long long temp1 =
min(dp[i - 1][j] + f((i - 1) * l1 + j * l2, l1, i, j),
dp[i][j - 1] + f(i * l1 + (j - 1) * l2, l2, i, j));
dp[i][j] = min(dp[i][j], temp1);
}
}
}
printf("%lld\n", dp[cnt1][cnt2]);
return 0;
}
|
#include <bits/stdc++.h>
const int maxn = 5020;
const int mod = 1e9 + 7;
using namespace std;
int k, a, ar[maxn * 60];
long long dn[maxn][maxn];
int main() {
scanf("%d %d", &a, &k);
for (int i = 1; i <= a; i++) scanf("%d", &ar[i]);
sort(ar + 1, ar + 1 + a);
int s = (a - 1) / k, s1 = 0, s2 = 0;
for (int i = 1; i <= k; i++) {
if ((a - i) / k == s) s2++;
if ((a - i) / k == s - 1) s1++;
}
s++;
dn[s1][s2] = 0;
for (int i = 0; i <= s1; i++)
for (int j = 0; j <= s2; j++) {
if (!i && !j) continue;
dn[i][j] = 1e18;
if (i)
dn[i][j] = min(dn[i][j], (dn[i - 1][j] + ar[(s - 1) * i + s * j] -
ar[(s - 1) * (i - 1) + s * j + 1]));
;
if (j)
dn[i][j] = min(dn[i][j], (dn[i][j - 1] + ar[(s - 1) * i + s * j] -
ar[(s - 1) * i + s * (j - 1) + 1]));
;
}
cout << dn[s1][s2] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T a) {
return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
return a * a;
}
const int N = 300009;
const int logN = 22;
const int sqrN = 450;
const int MOD = 1000000007;
const int INF = 1e9 + 100;
const long long INF64 = 2e18;
const long double PI = 3.1415926535897932384626433832795;
const long double eps = 1e-9;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
long long a[N];
long long f[N];
long long dp[5009][5009];
long long get(int l, int r) { return f[r] - f[l]; }
int main() {
srand(time(NULL));
cout << setprecision(10) << fixed;
cerr << setprecision(10) << fixed;
int n, k;
cin >> n >> k;
for (int i = 0; i < (int)(n); ++i) cin >> a[i];
sort(a, a + n);
for (int i = 1; i < n; ++i) f[i] = f[i - 1] + a[i] - a[i - 1];
if (n % k == 0) {
long long res = 0;
long long d = n / k;
for (int i = 0; i < (int)(k); ++i) {
int l = i * d, r = i * d + d - 1;
res += get(l, r);
}
cout << res;
return 0;
}
map<int, int> m;
for (int i = 1; i <= k; ++i) m[((n - i) / k)]++;
vector<int> v;
vector<int> cnt;
for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
v.push_back(it->first);
cnt.push_back(it->second);
}
for (int i = 0; i < (int)(2); ++i) ++v[i];
for (int i = 0; i < (int)(k + 3); ++i)
for (int j = 0; j < (int)(k + 3); ++j) dp[i][j] = INF64;
dp[0][0] = 0;
for (int i = 0; i < (int)(k + 3); ++i)
for (int j = 0; j < (int)(k + 3); ++j) {
int cnt1 = j, cnt2 = i - j;
int l = cnt1 * v[0] + cnt2 * v[1];
int r = l + v[0] - 1;
if (r < n) dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + get(l, r));
r = l + v[1] - 1;
if (r < n) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + get(l, r));
}
long long ans = INF64;
cout << dp[cnt[0] + cnt[1]][cnt[0]];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long dp[5005][5005];
long long arr[300005];
long long dif_p[300005];
int main() {
int N, K;
cin >> N >> K;
int chains = N / K;
int r = N % K;
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
sort(arr, arr + N);
for (int i = 1; i < N; i++) {
dif_p[i] = dif_p[i - 1] + abs(arr[i] - arr[i - 1]);
}
for (int j = 0; j <= r; ++j) {
dp[0][j] = 4294967295;
}
dp[0][0] = 0;
for (int i = 1; i <= K; ++i) {
for (int j = 0; j <= r; ++j) {
if (j > i) {
dp[i][j] = 4294967295;
continue;
}
int tmp = chains * i + j;
int tmp2 = chains * (i - 1) + j;
if (j == 0) {
dp[i][j] = dp[i - 1][j] + dif_p[tmp - 1] - dif_p[tmp2];
} else {
dp[i][j] = min(dp[i - 1][j] + dif_p[tmp - 1] - dif_p[tmp2],
dp[i - 1][j - 1] + dif_p[tmp - 1] - dif_p[tmp2 - 1]);
}
}
}
cout << dp[K][r] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[300003];
unordered_map<int, long long> d[300003];
inline long long calc() {
long long res = 0;
for (int i = 0; i + k < n; ++i) res += abs(a[i] - a[i + k]);
return res;
}
inline void update(int n, int k, long long val) { d[n][k] = max(d[n][k], val); }
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
sort(a, a + n);
long long sum = 0;
for (int i = 1; i < n; ++i) sum += abs(a[i] - a[i - 1]);
int len = n / k;
int k1 = k - n % k;
int k2 = n % k;
d[0][0] = 0;
for (int i = 0; i < n; ++i) {
if (i + len > n) continue;
for (auto& key : d[i]) {
int j = key.first;
int val = key.second;
int t = ((i + 1) - j * len) / (len + 1);
if (j + 1 <= k1)
update(i + len, j + 1,
val + (i + len < n ? abs(a[i + len] - a[i + len - 1]) : 0));
if (t + 1 <= k2)
update(i + len + 1, j,
val + (i + len + 1 < n ? abs(a[i + len + 1] - a[i + len]) : 0));
}
d[i].clear();
}
cout << sum - d[n][k1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long d[5010][5010];
long long sum[300010];
int main() {
int n, k;
scanf("%d%d", &n, &k);
int a[300010];
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[0] = 0;
sum[0] = 0;
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + abs(a[i] - a[i - 1]);
int num1 = n % k;
int num2 = k - n % k;
int numL1 = n / k + 1;
int numL2 = n / k;
for (int i = 0; i <= num1; i++) {
for (int j = 0; j <= num2; j++) {
d[i][j] = 1LL << 60;
int len = numL1 * i + numL2 * j;
int len1 = numL1 * (i - 1) + numL2 * j + 1;
int len2 = numL1 * i + numL2 * (j - 1) + 1;
if (i == 0 && j == 0) d[i][j] = 0;
if (i > 0) d[i][j] = min(d[i][j], d[i - 1][j] + sum[len] - sum[len1]);
if (j > 0) d[i][j] = min(d[i][j], d[i][j - 1] + sum[len] - sum[len2]);
}
}
cout << d[num1][num2] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char i = getchar();
long long f = 1, res = 0;
while (i < '0' || i > '9') {
if (i == '-') f = -1;
i = getchar();
}
while (i >= '0' && i <= '9') {
res = res * 10 + i - '0';
i = getchar();
}
return res * f;
}
const int N = 3e5 + 50;
const int M = 5e3 + 50;
long long a[N], dp[M][M];
int main() {
int n = read(), k = read();
for (register int i = 1; i <= n; ++i) a[i] = read();
sort(a + 1, a + n + 1);
int cntx = n % k, cnty = k - cntx, numx = n / k + 1, numy = n / k;
memset(dp, 0x3f, sizeof dp);
for (register int i = 0; i <= cntx; ++i)
for (register int j = 0; j <= cnty; ++j) {
if (!i && !j)
dp[i][j] = 0;
else {
if (i != 0)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[i * numx + j * numy] -
a[i * numx - numx + j * numy + 1]);
if (j != 0)
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i * numx + j * numy] -
a[j * numy - numy + i * numx + 1]);
}
}
printf("%lld\n", dp[cntx][cnty]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void SolveA() {
long long a, b, c, l;
cin >> a >> b >> c >> l;
unsigned long long ans = 0;
if (a > b) swap(a, b);
if (b > c) swap(b, c);
if (a > b) swap(a, b);
long long num[3] = {a, b, c};
long long sum = a + b + c;
long long totalCnt = 1;
if (l > 0) {
long long cntA = 1;
long long cntB = 1;
long long cntC = 1;
long long cnt0 = 1;
for (size_t i = 1; i < l; ++i) {
long long newA = cntA;
long long newB = cntA + cntB;
long long newC = cntA + cntB + cntC;
long long new0 = cntA + cntB + cntC + cnt0;
cntA = newA;
cntB = newB;
cntC = newC;
cnt0 = new0;
}
totalCnt = cntA + cntB + cntC + cnt0;
}
for (size_t i = 0; i < 3; ++i) {
for (long long na = max<long long>(0, sum - 2 * num[i]); na <= l; ++na) {
long long toSpare = 2 * num[i] + na - sum;
if (l - na < toSpare) toSpare = l - na;
ans += ((toSpare + 1) * (toSpare + 2)) / 2;
}
}
cout << totalCnt - ans;
}
long long myabs(long long x) { return (x < 0) ? -x : x; }
void SolveB() {
size_t n, k;
cin >> n >> k;
vector<int> v(n);
for (size_t i = 0; i < n; ++i) cin >> v[i];
make_heap(v.begin(), v.end());
sort_heap(v.begin(), v.end());
if (k == 1) {
long long ans = 0;
for (size_t i = 1; i < n; ++i) ans += v[i] - v[i - 1];
cout << ans;
return;
}
size_t l = n / k;
size_t nLong = n % k;
vector<long long> diffSum(n, 0);
for (size_t i = 1; i < n; ++i)
diffSum[i] = diffSum[i - 1] + myabs(v[i] - v[i - 1]);
size_t nStrides = k;
vector<long long> F(nLong + 1, static_cast<long long>(1000000000) *
static_cast<long long>(1000000000));
vector<long long> Fnew(nLong + 1, static_cast<long long>(1000000000) *
static_cast<long long>(1000000000));
F[0] = 0;
for (size_t i = 1; i <= nStrides; ++i) {
Fnew.assign(nLong + 1, static_cast<long long>(1000000000) *
static_cast<long long>(1000000000));
for (size_t j = 0; j <= nLong; ++j) {
long long s = diffSum[(i * l + j) - 1];
if (i > 1) s -= diffSum[(i * l + j) - l];
long long newVal = F[j] + s;
Fnew[j] = min(Fnew[j], newVal);
if (j < nLong) {
long long s = diffSum[(i * l + j + 1) - 1];
if (i > 1) s -= diffSum[(i * l + j) - l];
long long newVal = F[j] + s;
Fnew[j + 1] = min(Fnew[j + 1], newVal);
}
}
swap(F, Fnew);
}
cout << F[nLong];
}
int main() {
SolveB();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
const int N = 3e5 + 7;
const int K = 5e3 + 7;
int n, k;
int a[N];
int dp[K][K];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
int top = (n + k - 1) / k;
int low = n / k;
int ctop = n % k;
int clow = k - ctop;
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) dp[i][j] = -1;
dp[0][0] = 0;
for (int i = 0; i <= ctop; i++)
for (int j = 0; j <= clow; j++)
if (i || j) {
dp[i][j] = 2e9 + 7;
int pnt = i * top + j * low;
if (j) dp[i][j] = dp[i][j - 1] + a[pnt] - a[pnt - low + 1];
if (i)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[pnt] - a[pnt - top + 1]);
}
cout << dp[ctop][clow];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
long long int ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
template <class T>
inline T bpow(T p, T e) {
long long int ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p);
p = (p * p);
}
return (T)ret;
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int toLlint(string s) {
long long int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int a[300005], n, x, y, ax, ay;
long long int sm[300005];
bool bl[5005][5005];
long long int p[5005][5005];
long long int dp(int i, int j) {
if (i > ax || j > ay) return (LLONG_MAX / 10LL);
if (i == ax && j == ay) return 0;
long long int &pr = p[i][j];
if (bl[i][j] == 1) return pr;
bl[i][j] = 1;
pr = (LLONG_MAX / 10LL);
int id;
id = i * x + j * y;
pr = min(pr, dp(i + 1, j) + sm[id + x - 1] - sm[id]);
pr = min(pr, dp(i, j + 1) + sm[id + y - 1] - sm[id]);
return pr;
}
int main() {
int t, i, j, k, l;
long long int rs = 0;
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
for (i = 1; i < n; i++) {
sm[i] = a[i] - a[i - 1] + sm[i - 1];
}
x = n / k;
ax = k;
if (n % k) {
y = x + 1;
ay = n % k;
ax = k - ay;
rs = dp(0, 0);
} else {
rs = 0;
for (i = 0; i < n; i += x) {
rs += sm[i + x - 1] - sm[i];
}
}
cout << rs << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool smin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool smax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const long long N = 3e5 + 10, maxK = 5000 + 10;
long long n, ar[N], dp[maxK][maxK], k;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, 63, sizeof dp);
cin >> n >> k;
for (long long i = 1; i <= n; ++i) cin >> ar[i];
sort(ar + 1, ar + n + 1);
long long maxBig = n % k, sizeBig = (n + k - 1) / k;
long long maxSmall = k - maxBig, sizeSmall = (n / k);
dp[0][0] = 0;
for (long long big = 0; big <= maxBig; ++big) {
for (long long small = 0; small <= maxSmall; ++small) {
if (big + small == 0) continue;
long long cur = big * sizeBig + small * sizeSmall;
if (big) {
long long prv = (big - 1) * sizeBig + small * sizeSmall + 1;
smin(dp[big][small], ar[cur] - ar[prv] + dp[big - 1][small]);
}
if (small) {
long long prv = (big * sizeBig) + (small - 1) * sizeSmall + 1;
smin(dp[big][small], ar[cur] - ar[prv] + dp[big][small - 1]);
}
}
}
cout << dp[maxBig][maxSmall] << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 100000 + 5;
const int maxk = 5005;
const long long INF = 1LL << 60;
int dp[maxk][maxk];
int a, b;
long long g[maxn][2];
vector<int> v;
long long f(int n, int x, int y) {
if (x == 0 and y == 0) return 0;
int i = n - (a * x + b * y);
if (dp[x][y] == -1) {
long long ret = INF;
if (x) ret = min(ret, g[i][0] + f(n, x - 1, y));
if (y) ret = min(ret, g[i][1] + f(n, x, y - 1));
dp[x][y] = ret;
}
return dp[x][y];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int n, k;
cin >> n >> k;
v.resize(n);
for (int i = 0; i < n; ++i) cin >> v[i];
sort(v.begin(), v.end());
int x, y;
a = 0, b = 0, x = 0, y = 0;
vector<int> mark(n);
for (int i = 0; i < n; ++i) {
if (mark[i] == 0) {
int t = i;
int cnt = 0;
while (t < n) {
cnt++;
mark[t] = 1;
t += k;
}
if (i == 0) {
a = cnt;
x++;
} else {
if (cnt == a) {
x++;
} else {
y++;
b = cnt;
}
}
}
}
long long h[maxn];
for (int i = n - 2; i >= 0; --i) {
h[i] = h[i + 1] + abs(v[i] - v[i + 1]);
}
for (int i = 0; i < n; ++i) {
g[i][0] = g[i][1] = INF;
if (i + a <= n) {
g[i][0] = h[i] - h[i + a - 1];
}
if (i + b <= n) {
g[i][1] = h[i] - h[i + b - 1];
}
}
memset(dp, -1, sizeof dp);
long long ans = f(a * x + b * y, x, y);
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps = 1e-8;
const int maxn = 4e5 + 5;
const int maxm = 4e4 + 5;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int _inf = -1e9 + 7;
inline int scan() {
int m = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') m = m * 10 + c - '0', c = getchar();
return m;
}
int N, C, D;
struct node {
long long a, b;
node() {}
node(long long a, long long b) : a(a), b(b) {}
bool operator<(const node& t) const { return a * t.b < b * t.a; }
bool operator==(const node& t) const { return a * t.b == b * t.a; }
node inv() { return node(-(this->a), -(this->b)); }
} lk[4][maxn];
int lkcnt[4];
int lkcnt_[4];
int work(int i, int j) {
if (i == 0 && j == 1) {
return lkcnt[0] - lkcnt_[0];
}
if (i == 0 && j == 2) {
return lkcnt[0] - lkcnt_[1];
}
if (i == 1 && j == 2) {
return lkcnt[1] - lkcnt_[2];
}
if (i == 3 && j == 1) return lkcnt[3] - lkcnt_[3];
return lkcnt[i];
}
int main() {
cin >> N >> C >> D;
for (int i = 0; i < N; i++) {
long long a, b;
cin >> a >> b;
a -= C;
b -= D;
if (a >= 0 && b >= 0) {
lk[0][lkcnt[0]++] = node(a, b);
if (a == 0) lkcnt_[0]++;
if (b == 0) lkcnt_[1]++;
} else if (a < 0 && b >= 0) {
lk[1][lkcnt[1]++] = node(a, b);
if (b == 0) lkcnt_[2]++;
} else if (a < 0 && b < 0) {
lk[2][lkcnt[2]++] = node(a, b);
} else {
lk[3][lkcnt[3]++] = node(a, b);
if (a == 0) lkcnt_[3]++;
}
}
sort(lk[0], lk[0] + lkcnt[0]);
sort(lk[1], lk[1] + lkcnt[1]);
sort(lk[2], lk[2] + lkcnt[2]);
sort(lk[3], lk[3] + lkcnt[3]);
long long sum = 0;
for (int i = 0; i < 4; i++) {
int i2 = (i + 2) % 4;
int i1 = (i + 1) % 4;
for (int j = 0; j < lkcnt[i]; j++) {
int l = lower_bound(lk[i2], lk[i2] + lkcnt[i2], lk[i][j].inv()) - lk[i2];
int r = upper_bound(lk[i2], lk[i2] + lkcnt[i2], lk[i][j].inv()) - lk[i2];
if (r < lkcnt[i2] && lk[i][j].inv() == lk[i2][r]) r++;
sum += 1ll * l * (lkcnt[i2] - r);
int x = lk[i][j].a;
int y = lk[i][j].b;
sum += 1ll * l * lkcnt[i1];
if (i == 0 && y == 0) {
sum -= 1ll * l * lkcnt_[2];
}
if (i == 0 && x == 0) {
sum -= 1ll * (lkcnt[2] - r) * lkcnt_[3];
}
if (i == 3 && x == 0) {
sum -= 1ll * l * lkcnt_[0];
}
if (i == 1 && y == 0) {
sum -= 1ll * (lkcnt[3] - r) * lkcnt_[1];
}
}
}
cout << sum << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void SR(int &x) { scanf("%d", &x); }
void SR(long long &x) { scanf("%lld", &x); }
void SR(double &x) { scanf("%lf", &x); }
void SR(char *s) { scanf("%s", s); }
void RI() {}
template <typename I, typename... T>
void RI(I &x, T &...tail) {
SR(x);
RI(tail...);
}
void SP(const int x) { printf("%d", x); }
void SP(const long long x) { printf("%lld", x); }
void SP(const double x) { printf("%.16lf", x); }
void SP(const char *s) { printf(s); }
void PS() { puts(""); }
template <typename I, typename... T>
void PS(I &x, T &...tail) {
putchar(' ');
SP(x);
PS(tail...);
}
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
PS(tail...);
}
const int maxn = 3.5e5;
struct Point {
Point() {}
Point(long long a, long long b) : x(a), y(b) {}
long long x, y;
long long operator*(const Point &p) const { return x * p.x + y * p.y; }
long long operator%(const Point &p) const { return x * p.y - y * p.x; }
bool operator<(const Point &p) const {
return make_tuple(y < 0, y == 0 && x < 0, p % (*this)) <
make_tuple(p.y < 0, p.y == 0 && p.x < 0, 0);
}
} ar[3 * maxn];
int n;
void read() {
long long c, d;
RI(n, c, d);
for (int i = 0; i < int(n); i++) RI(ar[i].x, ar[i].y);
for (int i = 0; i < int(n); i++) ar[i].x -= c, ar[i].y -= d;
}
int up[2 * maxn][3];
int type(int i, int j) {
int ret = (j - i < n ? 0 : 4);
if (ar[i] % ar[j] > 0)
return 1 + ret;
else if (ar[i] % ar[j] == 0) {
if (ar[i] * ar[j] > 0)
return 0 + ret;
else
return 2 + ret;
} else
return 3 + ret;
}
struct Seg {
long long dat[4 * 3 * maxn];
void add(int x, int low, int high, int a, int b, long long val) {
if (low == a && b == high) {
dat[x] += val * (high - low + 1);
return;
}
int mid = (low + high) / 2;
long long tag = dat[x] - dat[x << 1] - dat[x << 1 | 1];
if (b <= mid)
add(x << 1, low, mid, a, b, val);
else if (mid + 1 <= a)
add(x << 1 | 1, mid + 1, high, a, b, val);
else
add(x << 1, low, mid, a, mid, val),
add(x << 1 | 1, mid + 1, high, mid + 1, b, val);
dat[x] = dat[x << 1] + dat[x << 1 | 1] + tag;
}
long long ask(int x, int low, int high, int a, int b) {
if (low == a && b == high) return dat[x];
int mid = (low + high) / 2;
long long ret = 0, tag = dat[x] - dat[x << 1] - dat[x << 1 | 1];
if (b <= mid)
ret = ask(x << 1, low, mid, a, b);
else if (mid + 1 <= a)
ret = ask(x << 1 | 1, mid + 1, high, a, b);
else
ret = ask(x << 1, low, mid, a, mid) +
ask(x << 1 | 1, mid + 1, high, mid + 1, b);
return ret + tag / (high - low + 1) * (b - a + 1);
}
} seg;
void build() {
sort(ar, ar + n);
memcpy(ar + n, ar, sizeof(Point) * n);
memcpy(ar + 2 * n, ar, sizeof(Point) * n);
for (int t = 0; t < int(3); t++) {
int &me = up[0][t];
for (me = 1; type(0, me) <= t; me++)
;
}
for (int i = (1); i <= int(2 * n - 1); i++)
for (int t = 0; t < int(3); t++) {
int &me = up[i][t];
for (me = up[i - 1][t]; type(i, me) <= t; me++)
;
}
for (int i = (up[0][0]); i <= int(up[0][1] - 1); i++) {
int l = up[0][2];
int r = up[i][1];
if (l < r) seg.add(1, 0, 3 * n - 1, l, r - 1, 1);
}
}
void sol() {
for (int i = 0; i < int(n); i++)
if (type(i, i + 1) >= 2) {
PL(0);
return;
}
long long ans = 0;
for (int i = 0; i < int(n); i++) {
ans += seg.dat[1];
if (i == n - 1) break;
int a = up[i][1];
int e = up[i][2];
int b = up[i + 1][1];
int c = up[i + 1][2];
if (b < c) {
long long t = seg.ask(1, 0, 3 * n - 1, b, c - 1);
seg.add(1, 0, 3 * n - 1, b, c - 1, -t / (c - b));
}
if (e < b) {
long long d = seg.ask(1, 0, 3 * n - 1, e, b - 1);
seg.add(1, 0, 3 * n - 1, e, b - 1, -d / (b - e));
}
for (int j = (a); j <= int(b - 1); j++) {
int l = up[i + 1][2];
int r = up[j][1];
if (l < r) seg.add(1, 0, 3 * n - 1, l, r - 1, 1);
}
}
PL(ans / 3);
}
int main() {
read();
build();
sol();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
point() {}
point(int x, int y) : x(x), y(y) {}
};
int quadrant(point p) {
if (p.y >= 0 && p.x > 0) return 1;
if (p.y > 0 && p.x <= 0) return 2;
if (p.y <= 0 && p.x < 0) return 3;
return 4;
}
bool operator<(point l, point r) {
int ql = quadrant(l);
int qr = quadrant(r);
if (ql != qr) return ql < qr;
return 1ll * l.y * r.x < 1ll * r.y * l.x;
}
int n, c, d;
point p[400010];
long long rr[400010];
long long ans, cur;
int main() {
ios::sync_with_stdio(0);
cin >> n >> c >> d;
for (int i = 0; i < n; i++) {
int r, w;
cin >> r >> w;
p[i] = point(r - c, w - d);
}
sort(p, p + n);
for (int l = 0, r = 0; l < n; l++) {
if (quadrant(p[l]) > 2) r = n;
point nl = point(-p[l].x, -p[l].y);
while (r < n && p[r] < nl) r++;
rr[l] = r - 1;
}
for (int l = 0, r = 0; l < n && quadrant(p[l]) <= 2; l++) {
point nl = point(-p[l].x, -p[l].y);
while (r < n && p[r] < nl) {
cur += rr[r];
r++;
}
ans += cur - rr[l] * (rr[l] - l + 1);
int num_greater = rr[l] - (upper_bound(p, p + n, p[l]) - p - 1);
int num_opposite = upper_bound(p, p + n, nl) - lower_bound(p, p + n, nl);
ans -= 1ll * num_greater * num_opposite;
cur -= rr[l];
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5;
pair<int, int> a[MAXN], b1[MAXN], b2[MAXN];
int sumPr[MAXN], sumSuf[MAXN];
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.second == 0) return 0;
if (b.second == 0) return 1;
int cur = 0;
if (a.second < 0) cur ^= 1;
if (b.second < 0) cur ^= 1;
long long s = a.first * 1ll * b.second;
long long r = a.second * 1ll * b.first;
if (s == r) return 0;
return (s < r) ^ cur;
}
int main() {
int n, c, d;
long long ans = 0;
scanf("%d%d%d", &n, &c, &d);
for (int i = 0; (i) < (n); ++i)
scanf("%d%d", &a[i].first, &a[i].second), a[i].first -= c, a[i].second -= d;
sort(a, a + n, cmp);
int last = n;
for (int i = 0; (i) < (n); ++i)
if (a[i].second == 0) {
last = i;
break;
}
for (int i = 0; (i) < (n); ++i) sumPr[i + 1] = sumPr[i] + (a[i].second < 0);
for (int i = (last)-1; (i) >= 0; --i)
sumSuf[i] = sumSuf[i + 1] + (a[i].second < 0);
for (int i = 0; (i) < (last); ++i) {
if (a[i].second > 0) {
int l = 0, r = 0;
int k = lower_bound(a, a + last, a[i], cmp) - a;
if (k != 0) l = sumPr[k];
k = upper_bound(a, a + last, a[i], cmp) - a;
if (k != last) r = sumSuf[k];
ans += l * 1ll * r;
} else {
int l = 0, r = 0;
int k = lower_bound(a, a + last, a[i], cmp) - a;
if (k != 0) l = (k - sumPr[k]);
k = upper_bound(a, a + last, a[i], cmp) - a;
if (k != last) r = (last - k - sumSuf[k]);
ans += l * 1ll * r;
}
}
int pos1 = 0, pos2 = 0;
for (int i = 0; (i) < (last); ++i)
if (a[i].second > 0)
b1[pos1++] = a[i];
else
b2[pos2++] = a[i];
long long ans1 = 0, ans2 = 0;
for (int i = 0; (i) < (pos1); ++i) {
int l = upper_bound(b2, b2 + pos2, b1[i], cmp) - b2;
ans1 += pos2 - l;
}
for (int i = 0; (i) < (pos2); ++i) {
int l = upper_bound(b1, b1 + pos1, b2[i], cmp) - b1;
ans2 += pos1 - l;
}
for (int i = (last); (i) < (n); ++i) {
if (a[i].first > 0) ans += ans1;
if (a[i].first < 0) ans += ans2;
}
printf(
"%lld"
"\n",
ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void SR(int &x) { scanf("%d", &x); }
void SR(long long &x) { scanf("%lld", &x); }
void SR(double &x) { scanf("%lf", &x); }
void SR(char *s) { scanf("%s", s); }
void RI() {}
template <typename I, typename... T>
void RI(I &x, T &...tail) {
SR(x);
RI(tail...);
}
void SP(const int x) { printf("%d", x); }
void SP(const long long x) { printf("%lld", x); }
void SP(const double x) { printf("%.16lf", x); }
void SP(const char *s) { printf(s); }
void PS() { puts(""); }
template <typename I, typename... T>
void PS(I &x, T &...tail) {
putchar(' ');
SP(x);
PS(tail...);
}
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
PS(tail...);
}
const int maxn = 3.5e5;
struct Point {
Point() {}
Point(long long a, long long b) : x(a), y(b) {}
long long x, y;
long long operator*(const Point &p) const { return x * p.x + y * p.y; }
long long operator%(const Point &p) const { return x * p.y - y * p.x; }
bool operator<(const Point &p) const {
return make_tuple(y < 0, y == 0 && x < 0, p % (*this)) <
make_tuple(p.y < 0, p.y == 0 && p.x < 0, 0);
}
} ar[3 * maxn];
int n;
void read() {
long long c, d;
RI(n, c, d);
for (int i = 0; i < int(n); i++) RI(ar[i].x, ar[i].y);
for (int i = 0; i < int(n); i++) ar[i].x -= c, ar[i].y -= d;
}
int up[2 * maxn][3];
int type(int i, int j) {
int ret = (j - i < n ? 0 : 4);
if (ar[i] % ar[j] > 0)
return 1 + ret;
else if (ar[i] % ar[j] == 0) {
if (ar[i] * ar[j] > 0)
return 0 + ret;
else
return 2 + ret;
} else
return 3 + ret;
}
struct Seg {
long long dat[4 * 3 * maxn];
void add(int x, int low, int high, int a, int b, long long val) {
if (low == a && b == high) {
dat[x] += val * (high - low + 1);
return;
}
int mid = (low + high) / 2;
long long tag = dat[x] - dat[x << 1] - dat[x << 1 | 1];
if (b <= mid)
add(x << 1, low, mid, a, b, val);
else if (mid + 1 <= a)
add(x << 1 | 1, mid + 1, high, a, b, val);
else
add(x << 1, low, mid, a, mid, val),
add(x << 1 | 1, mid + 1, high, mid + 1, b, val);
dat[x] = dat[x << 1] + dat[x << 1 | 1] + tag;
}
long long ask(int x, int low, int high, int a, int b) {
if (low == a && b == high) return dat[x];
int mid = (low + high) / 2;
long long ret = 0, tag = dat[x] - dat[x << 1] - dat[x << 1 | 1];
if (b <= mid)
ret = ask(x << 1, low, mid, a, b);
else if (mid + 1 <= a)
ret = ask(x << 1 | 1, mid + 1, high, a, b);
else
ret = ask(x << 1, low, mid, a, mid) +
ask(x << 1 | 1, mid + 1, high, mid + 1, b);
return ret + tag / (high - low + 1) * (b - a + 1);
}
} seg;
void build() {
sort(ar, ar + n);
memcpy(ar + n, ar, sizeof(Point) * n);
memcpy(ar + 2 * n, ar, sizeof(Point) * n);
for (int t = 0; t < int(3); t++) {
int &me = up[0][t];
for (me = 1; type(0, me) <= t; me++)
;
}
for (int i = (1); i <= int(2 * n - 1); i++)
for (int t = 0; t < int(3); t++) {
int &me = up[i][t];
for (me = up[i - 1][t]; type(i, me) <= t; me++)
;
}
for (int i = (up[0][0]); i <= int(up[0][1] - 1); i++) {
int l = up[0][2];
int r = up[i][1];
if (l < r) seg.add(1, 0, 3 * n - 1, l, r - 1, 1);
if (l < r)
;
}
}
void sol() {
for (int i = 0; i < int(n); i++)
if (type(i, i + 1) >= 2) {
PL(0);
return;
}
for (int i = 0; i < int(n); i++)
;
for (int i = 0; i < int(n); i++)
;
long long ans = 0;
for (int i = 0; i < int(n); i++) {
ans += seg.dat[1];
;
if (i == n - 1) break;
int a = up[i][1];
int e = up[i][2];
int b = up[i + 1][1];
int c = up[i + 1][2];
if (b < c) {
long long t = seg.ask(1, 0, 3 * n - 1, b, c - 1);
seg.add(1, 0, 3 * n - 1, b, c - 1, -t / (c - b));
}
if (e < b) {
long long d = seg.ask(1, 0, 3 * n - 1, e, b - 1);
seg.add(1, 0, 3 * n - 1, e, b - 1, -d / (b - e));
}
for (int j = (a); j <= int(b - 1); j++) {
int l = up[i + 1][2];
int r = up[j][1];
if (l < r) seg.add(1, 0, 3 * n - 1, l, r - 1, 1);
}
}
PL(ans / 3);
}
int main() {
read();
build();
sol();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
struct node {
int x, y, c;
double f;
} q[400009];
bool cmp(node a, node b) { return a.f < b.f; }
int n, c, d, tot, k1[400009], k2[400009], g0, g1;
int main() {
n = read(), c = read(), d = read();
for (int i = 1; i <= n; i++) {
int x = read() - c, y = read() - d;
if (y != 0)
q[++tot] = (node){x, y, y > 0 ? 1 : 0, 1.0 * x / y};
else if (x > 0)
g1++;
else
g0++;
}
sort(q + 1, q + 1 + tot, cmp);
int now = 0, a = 0, b = 0;
for (int i = 1; i <= tot; i++) {
while (now < i - 1 &&
1LL * q[i].x * q[now + 1].y != 1LL * q[now + 1].x * q[i].y) {
now++;
if (q[now].c == 1)
a++;
else
b++;
}
if (q[i].c == 1)
k1[i] = b;
else
k1[i] = a;
}
now = tot + 1, a = 0, b = 0;
for (int i = tot; i >= 1; i--) {
while (i + 1 < now &&
1LL * q[i].x * q[now - 1].y != 1LL * q[now - 1].x * q[i].y) {
now--;
if (q[now].c == 1)
a++;
else
b++;
}
if (q[i].c == 1)
k2[i] = b;
else
k2[i] = a;
}
long long ans = 0;
for (int i = 1; i <= tot; i++) ans += 1LL * k1[i] * k2[i];
for (int i = 1; i <= tot; i++)
if (q[i].c == 0) ans += 1LL * k1[i] * g1;
for (int i = 1; i <= tot; i++)
if (q[i].c == 1) ans += 1LL * k1[i] * g0;
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct pt;
long long det(pt a, pt b);
struct pt {
long long x, y, w;
int section() const { return y > 0 || (y == 0 && x > 0); }
bool operator<(const pt& b) const {
int s = section();
int bs = b.section();
if (s != bs) return s < bs;
return det(*this, b) < 0;
}
} a[400005];
pt b[400005];
int n, c, d, m;
long long det(pt a, pt b) { return a.x * b.y - a.y * b.x; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
cin >> n >> c >> d;
for (int i = 0; i < n; i++) {
cin >> a[i].x >> a[i].y;
a[i].x -= c;
a[i].y -= d;
}
sort(a, a + n);
b[0] = a[0];
b[0].w = 1;
int m = 1;
for (int i = 1; i < n; i++) {
if (b[m - 1] < a[i]) {
b[m] = a[i];
b[m].w = 1;
m++;
} else {
b[m - 1].w++;
}
}
long long sol = 0;
int r = 0, w = b[0].w;
for (int l = 0; l < m; l++) {
while (1) {
int rr = (r + 1) % m;
if (det(b[l], b[rr]) <= 0 && rr != l) {
r = rr;
w += b[rr].w;
} else {
break;
}
}
sol += b[l].w *
(2 + b[l].w * b[l].w - 3 * b[l].w * (w - 1) + 3 * (w - 2ll) * w) / 6;
w -= b[l].w;
if (l == r) {
r++;
w += b[r].w;
}
}
cerr << "do ovde sol: " << sol << '\n';
r = 0;
for (int l = 0; l < m; l++) {
while (1) {
int rr = (r + 1) % m;
if (det(b[l], b[rr]) <= 0 && rr != l) {
r = rr;
} else {
break;
}
}
if (b[l].section() && !b[r].section() && det(b[l], b[r]) == 0)
sol -= b[l].w * b[r].w * (b[l].w + b[r].w - 2) / 2;
if (l == r) r++;
}
cout << n * (n - 1ll) * (n - 2) / 6 - sol << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
bool my_comp(const pair<pair<long long, long long>, long long>& l,
const pair<pair<long long, long long>, long long>& r) {
if (l.first.first * r.first.second == l.first.second * r.first.first)
return l.second < r.second;
return l.first.first * r.first.second < l.first.second * r.first.first;
}
long long n, a, b, c, d;
vector<pair<pair<long long, long long>, long long> > g;
long long prevIdx[345700], nextIdx[345700];
long long cnt[345700][2], revcnt[345700][2];
int main() {
memset(prevIdx, 0, sizeof(prevIdx));
memset(nextIdx, 0, sizeof(nextIdx));
memset(cnt, 0, sizeof(cnt));
memset(revcnt, 0, sizeof(revcnt));
cin >> n >> c >> d;
for (int i = 0; i < (int)n; i++) {
cin >> a >> b;
a -= c;
b -= d;
if (a >= 0 && b >= 0)
g.push_back(make_pair(make_pair(a, b), 0));
else if (a < 0 && b < 0)
g.push_back(make_pair(make_pair(-a, -b), 1));
else if (a >= 0)
g.push_back(make_pair(make_pair(a, -b), 2));
else
g.push_back(make_pair(make_pair(-a, b), 3));
}
sort(g.begin(), g.end(), &my_comp);
long long prevX = g[0].first.first, prevY = g[0].first.second, idx = 0,
p = -1;
for (int i = 0; i < (int)n; i++) {
long long X = g[i].first.first, Y = g[i].first.second, P = g[i].second;
if (X * Y == 0) {
prevIdx[i] = i;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
prevIdx[i] = idx;
} else {
prevIdx[i] = i;
prevX = X;
prevY = Y;
idx = i;
p = P;
}
}
}
prevX = g[n - 1].first.first, prevY = g[n - 1].first.second, idx = n + 1,
p = -1;
for (int i = n - 1; i >= 0; i--) {
long long X = g[i].first.first, Y = g[i].first.second, P = g[i].second;
if (X * Y == 0) {
nextIdx[i] = i + 2;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
nextIdx[i] = idx;
} else {
nextIdx[i] = i + 2;
prevX = X;
prevY = Y;
idx = i + 2;
p = P;
}
}
}
for (int i = 0; i < (int)g.size(); i++) {
long long p = g[i].second;
long long q = g[i].first.second;
if (q > 0) {
if (p == 0 || p == 3)
cnt[i + 1][0] = cnt[i][0] + 1, cnt[i + 1][1] = cnt[i][1];
else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1] + 1;
} else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1];
}
for (int i = g.size() - 1; i >= 0; i--) {
long long p = g[i].second;
long long q = g[i].first.first;
if (q > 0) {
if (p == 0 || p == 2)
revcnt[i + 1][0] = revcnt[i + 2][0] + 1,
revcnt[i + 1][1] = revcnt[i + 2][1];
else
revcnt[i + 1][0] = revcnt[i + 2][0],
revcnt[i + 1][1] = revcnt[i + 2][1] + 1;
} else
revcnt[i + 1][0] = revcnt[i + 2][0], revcnt[i + 1][1] = revcnt[i + 2][1];
}
long long ans = 0;
for (int i = 0; i < (int)g.size(); i++) {
long long p = g[i].second;
if (g[i].first.first * g[i].first.second == 0) continue;
long long c1 = cnt[prevIdx[i]][0], c2 = cnt[prevIdx[i]][1],
rc1 = revcnt[nextIdx[i]][0], rc2 = revcnt[nextIdx[i]][1];
if (p == 0)
ans += c2 * rc2;
else if (p == 1)
ans += c1 * rc1;
else if (p == 2)
ans += c1 * rc2;
else if (p == 3)
ans += c2 * rc1;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void SR(int &x) { scanf("%d", &x); }
void SR(long long &x) { scanf("%lld", &x); }
void SR(double &x) { scanf("%lf", &x); }
void SR(char *s) { scanf("%s", s); }
void RI() {}
template <typename I, typename... T>
void RI(I &x, T &...tail) {
SR(x);
RI(tail...);
}
void SP(const int x) { printf("%d", x); }
void SP(const long long x) { printf("%lld", x); }
void SP(const double x) { printf("%.16lf", x); }
void SP(const char *s) { printf(s); }
void PS() { puts(""); }
template <typename I, typename... T>
void PS(I &x, T &...tail) {
putchar(' ');
SP(x);
PS(tail...);
}
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
PS(tail...);
}
const int maxn = 3.5e5;
struct Point {
Point() {}
Point(long long a, long long b) : x(a), y(b) {}
long long x, y;
long long operator*(const Point &p) const { return x * p.x + y * p.y; }
long long operator%(const Point &p) const { return x * p.y - y * p.x; }
bool operator<(const Point &p) const {
return make_tuple(y < 0, y == 0 && x < 0, p % (*this)) <
make_tuple(p.y < 0, p.y == 0 && p.x < 0, 0);
}
} ar[3 * maxn];
int n;
void read() {
long long c, d;
RI(n, c, d);
for (int i = 0; i < int(n); i++) RI(ar[i].x, ar[i].y);
for (int i = 0; i < int(n); i++) ar[i].x -= c, ar[i].y -= d;
}
int type(int i, int j) {
int ret = (j - i < n ? 0 : 4);
if (ar[i] % ar[j] > 0)
return 1 + ret;
else if (ar[i] % ar[j] == 0) {
if (ar[i] * ar[j] > 0)
return 0 + 4 * (j / n - i / n);
else
return 2 + ret;
} else
return 3 + ret;
}
void build() {
sort(ar, ar + n);
memcpy(ar + n, ar, sizeof(Point) * n);
}
void sol() {
long long ans = 1LL * n * (n - 1) * (n - 2) / 6;
int l = 0, r = 0;
for (int i = 0; i < int(n); i++) {
while (type(i, l) <= 1) l++;
while (type(i, r + 1) <= 2) r++;
;
ans -= 1LL * (r - i) * (r - i - 1) / 2;
ans += 1LL * (r - l + 1) * (r - l) / 2;
}
PL(ans);
}
int main() {
read();
build();
sol();
return 0;
}
|
#include <bits/stdc++.h>
const int INF = 1e9;
const long long lINF = 1e18;
const double EPS = 1e-12;
using namespace std;
const int maxn = 5e5 + 100;
struct point {
long long x, y;
point(long long x = 0, long long y = 0) : x(x), y(y) {}
};
long long operator^(const point& a, const point& b) {
return a.x * b.y - a.y * b.x;
}
long long operator*(const point& a, const point& b) {
return a.x * b.x + a.y * b.y;
}
long long slen(const point& a) { return a.x * a.x + a.y * a.y; }
bool operator<(const point& a, const point& b) {
return (a.x != b.x) ? (a.x < b.x) : a.y < b.y;
}
bool mycomp(const point& a, const point& b) {
if ((a ^ b) != 0) {
return (a ^ b) > 0;
} else {
if (a * b < 0) {
return true;
} else {
return slen(a) < slen(b);
}
}
}
vector<point> l, r, all;
vector<point> v;
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int main() {
srand(time(0));
int n, c, d;
scanf("%d%d%d", &n, &c, &d);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
a -= c;
b -= d;
point cur(a, b);
v.push_back(cur);
if (cur * point(1, (int)2e9 + 100) < 0) {
l.push_back(cur);
} else {
r.push_back(cur);
}
}
sort((l).begin(), (l).end(), mycomp);
sort((r).begin(), (r).end(), mycomp);
for (int i = 0; i < ((int)((l).size())); i++) {
all.push_back(l[i]);
}
for (int i = 0; i < ((int)((r).size())); i++) {
all.push_back(r[i]);
}
for (int i = 0; i < n; i++) {
all.push_back(all[i]);
}
long long ans = 0;
for (int i = 0, j = 1; i < n; i++) {
j = max(j, i + 1);
while (j < i + n && mycomp(all[i], all[j])) {
j++;
}
ans += (j - i - 1) * 1ll * (j - i - 2) / 2;
}
map<pair<int, int>, int> cnt;
for (int i = 0; i < n; i++) {
point cur = all[i];
int g = gcd(abs(cur.x), abs(cur.y));
cur.x /= g;
cur.y /= g;
cnt[make_pair((int)cur.x, (int)cur.y)]++;
}
for (auto i : cnt) {
ans -= i.second * 1ll * cnt[make_pair(-i.first.first, -i.first.second)] *
1l * (cnt[make_pair(-i.first.first, -i.first.second)] - 1) / 2;
}
ans = n * 1ll * (n - 1) / 2 * 1ll * (n - 2) / 3 - ans;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool eq(pair<long long, long long> x, pair<long long, long long> y) {
long long a = x.first;
long long b = x.second;
long long c = y.first;
long long d = y.second;
return a * d == b * c;
}
bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
long long a = x.first;
long long b = x.second;
long long c = y.first;
long long d = y.second;
long long bd = b * d;
if (bd > 0) {
return a * d < b * c;
} else if (bd < 0) {
return a * d > b * c;
} else {
assert(false);
}
}
bool cmpe(pair<long long, long long> x, pair<long long, long long> y) {
return eq(x, y) || cmp(x, y);
}
long long go(vector<pair<long long, long long> > a,
vector<pair<long long, long long> > b, long long negzero) {
long long n = a.size();
long long m = b.size();
long long *f = new long long[m];
long long *g = new long long[n];
for (long long i = 0; i < m; i++) f[i] = 0;
for (long long i = 0; i < n; i++) g[i] = 0;
long long i = m - 1;
long long k = n - 1;
long long p = 0;
while (i >= 0 && k >= 0) {
while (i >= 0 && k >= 0 && cmp(b[i], a[k])) {
p++;
k--;
}
while (i >= 0 && k >= 0 && cmpe(a[k], b[i])) {
f[i] = p;
i--;
}
}
while (i >= 0) {
f[i] = p;
i--;
}
long long q = 0;
i = n - 1;
k = m - 1;
while (i >= 0 && k >= 0) {
while (i >= 0 && k >= 0 && cmp(a[i], b[k])) {
q += f[k];
k--;
}
while (i >= 0 && k >= 0 && cmpe(b[k], a[i])) {
g[i] = q;
i--;
}
}
while (i >= 0) {
g[i] = q;
i--;
}
long long ans = 0;
for (long long i = 0; i < n; i++) {
ans += g[i];
}
long long s = 0;
for (long long i = 0; i < m; i++) {
s += f[i];
}
ans += s * negzero;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
long long n, c, d;
cin >> n;
cin >> c;
cin >> d;
vector<pair<long long, long long> > a, b;
long long poszero = 0;
long long negzero = 0;
for (long long i = 0; i < n; i++) {
long long r, w;
cin >> r;
cin >> w;
r -= c;
w -= d;
if (r == 0 && w == 0) continue;
if (w == 0) {
if (r > 0) poszero++;
if (r < 0) negzero++;
continue;
}
if (w > 0) {
a.push_back(make_pair(r, w));
} else {
b.push_back(make_pair(r, w));
}
}
sort(a.begin(), a.end(), cmp);
sort(b.begin(), b.end(), cmp);
long long ans1 = go(a, b, negzero);
long long ans2 = go(b, a, poszero);
long long ans = ans1 + ans2;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 345678 + 5;
struct Point {
long long x, y;
Point() {}
Point(long long _x, long long _y) : x(_x), y(_y) {}
Point operator-(const Point &t) const { return Point(x - t.x, y - t.y); }
long long operator*(const Point &t) const { return x * t.y - y * t.x; }
long long operator^(const Point &t) const { return x * t.x + y * t.y; }
bool operator<(const Point &t) const {
bool up[2] = {0, 0};
if (y > 0 || (y == 0 && x > 0)) up[0] = 1;
if (t.y > 0 || (t.y == 0 && t.x > 0)) up[1] = 1;
if (up[0] ^ up[1])
return up[0];
else
return (*this) * t > 0;
}
} p[MAXN * 3];
int main() {
int n;
long long c, d;
scanf("%d%I64d%I64d", &n, &c, &d);
for (int i = 0; i < n; i++) {
scanf("%I64d%I64d", &p[i].x, &p[i].y);
p[i].x -= c, p[i].y -= d;
}
sort(p, p + n);
for (int i = n; i < 3 * n; i++) p[i] = p[i - n];
long long res = 0, now = 0;
int p0 = 0, p1 = 0, p2 = 0, p3 = 0, p4 = 0;
for (int i = 0; i < n; i = p4) {
while (p4 < i + n && (p4 <= i || (p[p4] * p[i] == 0 && (p[p4] ^ p[i]) > 0)))
p4++;
p0 = p1;
while (p1 < i + n &&
(p1 <= i ||
(p[p1] * p[i] < 0 || (p[p1] * p[i] == 0 && (p[p1] ^ p[i]) > 0))))
p1++;
now -= 1LL * (p0 - i) * (p1 - p2);
while (p2 < i + n && (p2 <= i || p[p2] * p[i] <= 0)) p2++;
now -= 1LL * max(0, p0 - i - (p4 - i)) * (p2 - p1);
for (int j = p0; j < p1; j++) {
while (p3 < j + n && (p3 <= j || p[p3] * p[j] < 0)) p3++;
now += 1LL * max(0, p3 - p2);
}
res += (p4 - i) * now;
}
return 0 * printf("%I64d", res / 3);
}
|
#include <bits/stdc++.h>
using namespace std;
long long rdtsc() {
long long tmp;
asm("rdtsc" : "=A"(tmp));
return tmp;
}
inline int myrand() { return abs((rand() << 15) ^ rand()); }
inline int rnd(int x) { return myrand() % x; }
const int INF = (int)1.01e9;
const long double EPS = 1e-9;
void precalc() {}
struct Point {
int x, y;
Point() {}
Point(int _x, int _y) : x(_x), y(_y) {}
inline Point operator-(const Point &p) const {
return Point(x - p.x, y - p.y);
}
inline long long operator^(const Point &p) const {
return (long long)x * p.y - (long long)y * p.x;
}
int type() const { return (y > 0 || (y == 0 && x > 0)) ? 1 : 2; }
long long sabs() const { return (long long)x * x + (long long)y * y; }
};
struct angleComp {
inline bool operator()(const Point &p1, const Point &p2) {
int t = p1.type() - p2.type();
if (t) {
return t < 0;
}
long long tmp = (p1 ^ p2);
if (tmp) {
return tmp > 0ll;
}
return p1.sabs() < p2.sabs();
}
};
const int maxn = (int)1e6 + 10;
Point ps[maxn];
int n;
bool read() {
int c, d;
if (scanf("%d%d%d", &n, &c, &d) < 3) {
return 0;
}
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
ps[i] = Point(x - c, y - d);
}
return 1;
}
long long check() {
long long res = 0;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
for (int k = i; k < n; ++k) {
if ((ps[i] ^ ps[j]) > 0ll && (ps[j] ^ ps[k]) > 0ll &&
(ps[k] ^ ps[i]) > 0ll) {
++res;
}
}
}
}
return res;
}
void solve() {
sort(ps, ps + n, angleComp());
for (int i = 0; i < n; ++i) {
ps[i + n] = ps[i];
}
long long res = (long long)n * (n - 1) * (n - 2) / 6;
int a = 0, b = 0, c = 0;
for (int i = 0, j = 0; i < n; ++i) {
if (a > 1) {
--a;
} else {
b += c;
c = 0;
a = 0;
for (int k = i;
k < j && ps[k].type() == ps[i].type() && (ps[k] ^ ps[i]) == 0ll;
++k) {
--b;
++a;
}
assert(b >= 0);
}
while (j < n && ps[i].type() == ps[j].type() && (ps[i] ^ ps[j]) == 0ll) {
++a;
++j;
}
while (j < 2 * n && (ps[i] ^ ps[j]) > 0ll) {
++b;
++j;
}
while (j < 2 * n && ps[i].type() != ps[j].type() &&
(ps[i] ^ ps[j]) == 0ll) {
++c;
++j;
}
--a;
assert(a >= 0);
res -= (long long)a * (a - 1) / 2;
res -= (long long)a * b;
res -= (long long)b * c;
res -= (long long)c * a;
res -= (long long)b * (b - 1) / 2;
++a;
}
printf(
"%lld"
"\n",
res);
if (n < 100) {
assert(check() == res);
}
}
int main() {
srand(rdtsc());
precalc();
while (1) {
if (!read()) {
break;
}
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, c, d, R[400000], W[400000], r[400000], w[400000];
long long S[400000], SS[400000], T[400000];
long double ang[400000];
vector<pair<long double, pair<int, int>>> v;
bool checkpoz(int x, int y) { return 1LL * R[x] * W[y] < 1LL * R[y] * W[x]; }
int main() {
cin >> N >> c >> d;
for (int i = 0; i < N; ++i) {
cin >> r[i] >> w[i];
r[i] -= c;
w[i] -= d;
v.push_back(make_pair(atan2(w[i], r[i]), make_pair(1, i)));
v.push_back(make_pair(atan2(-w[i], -r[i]), make_pair(0, i)));
}
sort(v.begin(), v.end());
int cnt = 0;
int curr = 0;
for (int i = 0; i < v.size(); ++i) {
if (v[i].second.first) {
++cnt;
} else {
S[curr] = cnt;
ang[curr] = v[i].first;
R[curr] = r[v[i].second.second];
W[curr] = w[v[i].second.second];
++curr;
}
}
for (int i = 0; i < v.size(); ++i) {
v[i].second.first = 1 - v[i].second.first;
}
sort(v.begin(), v.end());
cnt = 0;
curr = 0;
for (int i = 0; i < v.size(); ++i) {
if (!v[i].second.first) {
++cnt;
} else {
SS[curr] = cnt;
++curr;
}
}
T[0] = S[0];
for (int i = 1; i < N; ++i) {
T[i] = T[i - 1] + S[i];
}
long long ret = 0;
for (int i = 0; i < N; ++i) {
int st = i + 1;
int dr = i + N - 1;
int j = i;
while (st <= dr) {
int mij = (st + dr) / 2;
if (checkpoz(mij % N, i)) {
j = mij;
st = mij + 1;
} else {
dr = mij - 1;
}
}
st = i;
dr = N - 1;
int k = i;
while (st <= dr) {
int mij = (st + dr) / 2;
if (ang[mij] == ang[i]) {
k = mij;
st = mij + 1;
} else {
dr = mij - 1;
}
}
if (j <= k) continue;
ret += T[j % N] - T[k] - 1LL * (j - k) * SS[k];
if (j >= N) {
ret += 1LL * N * (j - N + 1) + T[N - 1];
}
}
cout << ret / 3 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double pi = acos((long double)-1);
const long double eps = 1e-18;
long long n, c, d;
int main() {
vector<long double> Ans;
scanf("%lld%lld%lld", &n, &c, &d);
for (int i = 0; i < n; i++) {
long long x, y;
scanf("%lld%lld", &x, &y);
x -= c;
y -= d;
long double cur = atan2(y, x);
Ans.push_back(cur);
Ans.push_back(cur + 2 * pi);
}
sort(Ans.begin(), Ans.end());
long long ans = (n * (n - 1) * (n - 2)) / 6;
for (long long i = 0, low = 0, high = 0; i < n; i++) {
while (Ans[low] < Ans[i] + pi - eps) low++;
while (Ans[high + 1] < Ans[i] + pi + eps) high++;
ans -= ((high - i) * (high - i - 1)) / 2;
ans += ((high - low) * (high - low + 1)) / 2;
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
inline void amax(T &a, const T &b) {
if (a < b) a = b;
}
const complex<long long> I(0, 1);
const long long EPS = 0;
int sign(double x) {
if (x < -EPS) return -1;
if (EPS < x) return 1;
return 0;
}
long long cross(const complex<long long> &x, const complex<long long> &y) {
return imag(conj(x) * y);
}
struct CMP_ARG {
bool operator()(const complex<long long> &x,
const complex<long long> &y) const {
return sign(cross(x, y)) == 1;
}
} cmp_arg;
int N;
long long C_sub, D_sub;
long long R[400011], W[400011];
vector<complex<long long> > U, D;
long long pos, neg;
int main() {
scanf("%d%lld%lld", &N, &C_sub, &D_sub);
for (int i = 0, i_len = (N); i < i_len; ++i) {
scanf("%lld%lld", R + i, W + i);
R[i] -= C_sub;
W[i] -= D_sub;
}
{
double x = 0;
for (int i = 0, i_len = (N); i < i_len; ++i)
x += arg(complex<double>(R[i], W[i]));
fprintf(stderr, "%f\n", x);
}
for (int i = 0, i_len = (N); i < i_len; ++i) {
if (W[i] > 0)
U.push_back(complex<long long>(R[i], W[i]));
else if (W[i] < 0)
D.push_back(complex<long long>(R[i], W[i]));
else if (R[i] > 0)
pos++;
else
neg++;
}
sort(U.begin(), U.end(), cmp_arg);
sort(D.begin(), D.end(), cmp_arg);
long long ans = 0;
for (long long i = 0, lo = 0, hi = 0; i < (int)U.size(); i++) {
while (hi < (int)D.size() && cross(U[i], D[hi]) >= 0) hi++;
while (lo < (int)D.size() && cross(U[i], D[lo]) > 0) lo++;
ans += lo * ((long long)D.size() - hi);
ans += neg * ((long long)D.size() - hi);
ans += lo * pos;
}
for (long long i = 0, lo = 0, hi = 0; i < (int)D.size(); i++) {
while (hi < (int)U.size() && cross(D[i], U[hi]) >= 0) hi++;
while (lo < (int)U.size() && cross(D[i], U[lo]) > 0) lo++;
ans += lo * ((long long)U.size() - hi);
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
struct DATA {
long long r, w;
long double rw;
long long u;
DATA() {}
DATA(long long R, long long W) {
r = R, w = W, rw = w == 0 ? r : (long double)r / (long double)w;
}
DATA(long double Rw) { rw = Rw; }
bool operator<(const DATA &p) const { return rw < p.rw; }
bool operator>(const DATA &p) const { return rw > p.rw; }
};
int n;
long long C, D;
DATA a[400000];
vector<DATA> pl, mi, ze;
int main() {
int i, j, k;
cin >> n >> C >> D;
for (i = 0; i < n; i++) {
scanf("%lld%lld", &a[i].r, &a[i].w), a[i].r -= C, a[i].w -= D;
a[i] = DATA(a[i].r, a[i].w);
if (!a[i].w)
ze.push_back(a[i]);
else if (a[i].w > 0)
pl.push_back(a[i]);
else
mi.push_back(a[i]);
}
sort((ze).begin(), (ze).end()), sort((pl).begin(), (pl).end()),
sort((mi).begin(), (mi).end());
long long dab = 0;
for (auto e : mi) {
long long x =
lower_bound((pl).begin(), (pl).end(), DATA(e.rw)) - pl.begin();
long long y =
pl.size() -
(upper_bound((pl).begin(), (pl).end(), DATA(e.rw)) - pl.begin());
dab += x * y;
}
for (auto e : pl) {
long long x =
lower_bound((mi).begin(), (mi).end(), DATA(e.rw)) - mi.begin();
long long y =
mi.size() -
(upper_bound((mi).begin(), (mi).end(), DATA(e.rw)) - mi.begin());
dab += x * y;
}
long long xmi = 0, xpl = 0;
for (auto &e : mi)
xmi += lower_bound((pl).begin(), (pl).end(), DATA(e.rw)) - pl.begin();
for (auto &e : pl)
xpl += lower_bound((mi).begin(), (mi).end(), DATA(e.rw)) - mi.begin();
for (auto e : ze) {
if (e.r > 0)
dab += xmi;
else
dab += xpl;
}
cout << dab;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 350000;
struct P {
int x, y;
P() {}
P(int _x, int _y) {
x = _x;
y = _y;
}
bool be() const { return x ? x > 0 : y > 0; }
P operator+(const P &a) const { return P(x + a.x, y + a.y); }
P operator-(const P &a) const { return P(x - a.x, y - a.y); }
long long operator*(const P &a) const {
return 1ll * x * a.y - 1ll * y * a.x;
}
bool operator==(const P &a) const {
return 1ll * x * a.y == 1ll * y * a.x && be() == a.be();
}
} p[N * 2];
long long ans, SSS[N * 2];
int n, PX, PY;
int S[N * 2], SS[N * 2];
bool cmp(P x, P y) { return x.be() ^ y.be() ? x.be() : x * y > 0; }
int main() {
scanf("%d%d%d", &n, &PX, &PY);
for (int i = (int)(1); i <= (int)(n); i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].x -= PX;
p[i].y -= PY;
}
sort(p + 1, p + n + 1, cmp);
int top = 1;
S[1] = 1;
for (int i = (int)(2); i <= (int)(n); i++) {
if (!(p[i] == p[i - 1])) p[++top] = p[i];
++S[top];
}
n = top;
for (int i = (int)(1); i <= (int)(n); i++) {
p[i + n] = p[i];
S[i + n] = S[i];
}
for (int i = (int)(1); i <= (int)(n * 2); i++) {
SS[i] = SS[i - 1] + S[i];
SSS[i] = SSS[i - 1] + 1ll * S[i] * S[i];
}
for (int i = (int)(1); i <= (int)(n); i++)
ans += 1ll * S[i] * (1ll * SS[i - 1] * SS[i - 1] - SSS[i - 1]) / 2;
int p1 = 1;
for (int i = (int)(1); i <= (int)(n); i++) {
p1 = max(p1, i);
for (; p1 + 1 != i + n && p[i] * p[p1 + 1] >= 0; ++p1)
;
ans -= 1ll * S[i] *
(1ll * (SS[p1] - SS[i]) * (SS[p1] - SS[i]) - (SSS[p1] - SSS[i])) / 2;
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, c, d;
const int maxn = 3456789;
const long double PI = acos((long double)(-1.0));
const long double eps = 1e-18;
long double ang[maxn];
int main() {
while (cin >> n >> c >> d) {
for (int i = 0; i < (n); i++) {
int r, w;
scanf("%d%d", &r, &w);
long double x = r - c;
long double y = w - d;
ang[i] = atan2(y, x);
}
sort(ang, ang + n);
for (int i = 0; i < (n + 3); i++) ang[i + n] = ang[i] + 2 * PI;
long long ans = 1LL * n * (n - 1) * (n - 2) / 6;
int j = 0, lo = 0, hi = 0;
for (int i = 0; i < (n); i++) {
while (ang[j + 1] - ang[i] < PI + eps) ++j;
while (ang[lo] - ang[i] < PI - eps) ++lo;
while (ang[hi + 1] - ang[i] < PI + eps) ++hi;
ans -= 1LL * (j - i) * (j - i - 1) / 2;
ans += 1LL * (hi - lo) * (hi - lo + 1) / 2;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = atan2((long double)0, (long double)-1);
const long double EPS = 1e-18;
vector<long double> fi;
long long N, b, c;
inline long double ang(pair<long long, long long> p) {
return atan2(p.second, p.first);
}
int main() {
cin >> N >> b >> c;
for (int i = 0; i < N; ++i) {
long long x, y;
cin >> x >> y;
fi.push_back(ang({x - b, y - c}));
}
sort(fi.begin(), fi.end());
for (int i = 0; i < N; ++i) fi.push_back(fi[i] + 2 * PI);
long long total = N * (N - 1) * (N - 2) / 6;
long long lo = 0, hi = 0;
for (int i = 0; i < N; ++i) {
while (fi[i] + PI - EPS > fi[lo + 1]) lo++;
while (fi[i] + PI + EPS > fi[hi]) hi++;
long long last = hi - 1;
long long n_eq = (hi - 1) - (lo + 1) + 1;
long long cnt = last - i;
total -= cnt * (cnt - 1) / 2;
total += n_eq * (n_eq - 1) / 2;
}
cout << total << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-18;
long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1ll;
long long _tmp = mypow(_a, _x / 2, _mod);
_tmp = (_tmp * _tmp) % _mod;
if (_x & 1) _tmp = (_tmp * _a) % _mod;
return _tmp;
}
bool equal(long double _x, long double _y) {
return _x > _y - eps && _x < _y + eps;
}
int __ = 1, cs;
const long double pi = atan2((long double)0, (long double)-1);
long double a[3456789];
void build() {}
long long n, c, d;
void init() {
n = getint();
c = getint();
d = getint();
for (int i = 0; i < n; i++) {
long double ri = getint() - c;
long double wi = getint() - d;
a[i] = atan2(wi, ri);
}
sort(a, a + n);
for (int i = 0; i < n + n; i++) a[i + n] = a[i] + 2.0 * pi;
}
void solve() {
long long ans = n * (n - 1) * (n - 2) / 6;
long long l = 0, r = 0;
for (long long i = 0; i < n; i++) {
while (a[l] - a[i] < pi - eps) l++;
while (a[r + 1] - a[i] < pi + eps) r++;
ans -= (r - i) * (r - i - 1) / 2;
ans += (r - l) * (r - l + 1) / 2;
}
cout << ans << endl;
}
int main() {
build();
while (__--) {
init();
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = atan2((long double)0, (long double)-1), eps = 1e-18;
long double ang[800000];
int main() {
long long n, c, d, r, w, hi, lo, ans, i;
while (~scanf("%lld%lld%lld", &n, &c, &d)) {
for (i = 0; i < n; i++) {
scanf("%lld%lld", &r, &w);
ang[i] = atan2(w - d, r - c);
}
sort(ang, ang + n);
for (i = 0; i < n + 3; i++) {
ang[n + i] = ang[i] + 2 * PI;
}
ans = n * (n - 1) * (n - 2) / 6;
lo = hi = 0;
for (i = 0; i < n; i++) {
while (ang[lo] - ang[i] < PI - eps) lo++;
while (ang[hi + 1] - ang[i] < PI + eps) hi++;
ans -= (hi - i) * (hi - i - 1) / 2;
ans += (hi - lo) * (hi - lo + 1) / 2;
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct comp {
bool operator()(const std::pair<long long, long long>& a,
const std::pair<long long, long long>& b) const {
return a.first * b.second < b.first * a.second;
}
};
int main() {
int n, cc, dd;
scanf("%d %d %d", &n, &cc, &dd);
int c = cc;
int d = dd;
std::vector<std::pair<long long, long long> > pos, neg;
std::vector<long long> zer;
for (int i = 0; i < n; i++) {
int r, w;
scanf("%d %d", &r, &w);
r -= c;
w -= d;
if (w > 0) pos.push_back(make_pair((long long)r, (long long)w));
if (w == 0) zer.push_back((long long)r);
if (w < 0) neg.push_back(make_pair((long long)r, (long long)-w));
}
sort((pos).begin(), (pos).end(), comp());
sort((neg).begin(), (neg).end(), comp());
sort((zer).begin(), (zer).end());
long long ans = 0;
long long pon = 0;
long long nop = 0;
for (auto x : pos) {
x.first = -x.first;
auto it2 = upper_bound((neg).begin(), (neg).end(), x, comp());
auto it1 = lower_bound((neg).begin(), (neg).end(), x, comp());
long long ov = neg.end() - it2;
long long un = it1 - neg.begin();
long long mi = it2 - it1;
pon += un;
nop += ov;
ans += ov * un;
}
for (auto x : neg) {
x.first = -x.first;
auto it2 = upper_bound((pos).begin(), (pos).end(), x, comp());
auto it1 = lower_bound((pos).begin(), (pos).end(), x, comp());
long long ov = pos.end() - it2;
long long un = it1 - pos.begin();
long long mi = it2 - it1;
ans += ov * un;
}
for (auto u : zer) {
if (u > 0)
ans += pon;
else
ans += nop;
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long double EPS = 1e-18;
const long double PI = 2 * acos(0.00);
long long n, c, d;
long double angle[N];
long double getangle(long double x, long double y) {
if (x == 0) {
if (y > 0) return PI / 2;
return 3 * PI / 2;
}
if (y == 0) {
if (x > 0) return 0;
return PI;
}
long double res = atan(y / x);
if (x < 0) return res + PI;
if (y < 0) return res + 2 * PI;
return res;
}
int main() {
scanf("%I64d%I64d%I64d", &n, &c, &d);
for (int i = 1; i <= n; ++i) {
long long x, y;
scanf("%I64d%I64d", &x, &y);
angle[i] = getangle(x - c, y - d);
}
sort(angle + 1, angle + n + 1);
for (int i = n + 1; i <= 2 * n; ++i) angle[i] = angle[i - n] + 2 * PI;
long long tot = 0;
for (int i = 1; i <= n; ++i) {
int l = i, r = i + n, f1 = -1, f2 = -1;
while (l <= r) {
int m = (l + r) >> 1;
if (angle[m] - angle[i] < PI - EPS) {
f1 = m;
l = m + 1;
} else
r = m - 1;
}
l = i, r = i + n;
while (l <= r) {
int m = (l + r) >> 1;
if (angle[m] - angle[i] < PI + EPS)
l = m + 1;
else {
f2 = m;
r = m - 1;
}
}
long long u = f1 - i;
long long v = n - (f2 - i);
long long w = f2 - f1 - 1;
tot += (long long)u * (u - 1) / 2 + (long long)v * (v - 1) / 2 +
(long long)w * (u + v);
}
printf("%I64d", 1LL * n * (n - 1) * (n - 2) / 6 - tot / 2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos((long double)-1), eps = 1e-18;
long double ang[800000];
int main() {
long long n, c, d, r, w, hi, lo, ans, i;
while (~scanf("%lld%lld%lld", &n, &c, &d)) {
for (i = 0; i < n; i++) {
scanf("%lld%lld", &r, &w);
ang[i] = atan2(w - d, r - c);
}
sort(ang, ang + n);
for (i = 0; i < n + 3; i++) {
ang[n + i] = ang[i] + 2 * PI;
}
ans = n * (n - 1) * (n - 2) / 6;
lo = hi = 0;
for (i = 0; i < n; i++) {
while (ang[lo] - ang[i] < PI - eps) lo++;
while (ang[hi + 1] - ang[i] < PI + eps) hi++;
ans -= (hi - i) * (hi - i - 1) / 2;
ans += (hi - lo) * (hi - lo + 1) / 2;
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
struct node {
double x;
int flag;
} q[350000];
bool cmp(node a, node b) { return a.x > b.x; }
int cnt;
long long cntzero1, cntzero2;
int r[350000], w[350000];
int fir[350000], las[350000];
long long fsum1[350000], fsum2[350000], lsum1[350000], lsum2[350000], ans;
int main() {
int n, c, d;
n = read(), c = read(), d = read();
for (int i = 1; i <= n; i++) {
r[i] = read(), w[i] = read();
r[i] -= c, w[i] -= d;
if (w[i] == 0) {
r[i] > 0 ? cntzero1++ : cntzero2++;
} else {
q[++cnt].x = r[i] * 1.0 / w[i];
w[i] > 0 ? q[cnt].flag = 1 : q[cnt].flag = -1;
}
}
sort(q + 1, q + cnt + 1, cmp);
int j;
for (int i = 1; i <= cnt; i = j + 1) {
j = i;
while (j + 1 <= cnt && q[j + 1].x == q[i].x) j++;
for (int k = i; k <= j; k++) {
fir[k] = i, las[k] = j;
}
}
for (int i = 1; i <= cnt; i++) {
fsum1[i] = fsum1[i - 1], fsum2[i] = fsum2[i - 1];
q[i].flag > 0 ? fsum1[i]++ : fsum2[i]++;
}
for (int i = cnt; i >= 1; i--) {
lsum1[i] = lsum1[i + 1], lsum2[i] = lsum2[i + 1];
q[i].flag > 0 ? lsum1[i]++ : lsum2[i]++;
}
for (int i = 1; i <= cnt; i++) {
(q[i].flag > 0) ? ans += fsum2[fir[i] - 1] * lsum2[las[i] + 1]
: ans += fsum1[fir[i] - 1] * lsum1[las[i] + 1];
}
long long num1 = 0, num2 = 0;
for (int i = 1; i <= cnt; i++) {
if (q[i].flag > 0)
num1 += fsum2[fir[i] - 1];
else
num2 += fsum1[fir[i] - 1];
}
ans += cntzero1 * num1 + cntzero2 * num2;
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
point() {}
point(int x, int y) : x(x), y(y) {}
};
int quadrant(point p) {
if (p.y >= 0 && p.x > 0) return 1;
if (p.y > 0 && p.x <= 0) return 2;
if (p.y <= 0 && p.x < 0) return 3;
return 4;
}
bool operator<(point l, point r) {
int ql = quadrant(l);
int qr = quadrant(r);
if (ql != qr) return ql < qr;
return 1ll * l.y * r.x < 1ll * r.y * l.x;
}
int n, c, d;
point p[400010];
long long rr[400010];
long long ans, cur;
int main() {
ios::sync_with_stdio(0);
cin >> n >> c >> d;
for (int i = 0; i < n; i++) {
int r, w;
cin >> r >> w;
p[i] = point(r - c, w - d);
}
sort(p, p + n);
for (int l = 0, r = 0; l < n; l++) {
if (quadrant(p[l]) > 2) r = n;
point nl = point(-p[l].x, -p[l].y);
while (r < n && p[r] < nl) r++;
rr[l] = r - 1;
}
for (int l = 0, r = 0; l < n && quadrant(p[l]) <= 2; l++) {
point nl = point(-p[l].x, -p[l].y);
while (r < n && p[r] < nl) {
cur += rr[r];
r++;
}
ans += cur - rr[l] * (rr[l] - l + 1);
int num_greater = rr[l] - (upper_bound(p, p + n, p[l]) - p - 1);
int num_opposite = upper_bound(p, p + n, nl) - lower_bound(p, p + n, nl);
ans -= 1ll * num_greater * num_opposite;
cur -= rr[l];
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, t...);
}
const int maxn = 4e5 + 5;
const long double pi = acos((long double)-1);
const long double esp = 1e-18;
pair<int, int> a[maxn];
int n;
long double v[maxn * 2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("TEST"
".INP",
"r"))
freopen(
"TEST"
".INP",
"r", stdin),
freopen(
"TEST"
".OUT",
"w", stdout);
cin >> n;
cin >> a[n].first >> a[n].second;
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
a[i].first -= a[n].first;
a[i].second -= a[n].second;
long double now = atan2((long double)a[i].second, (long double)a[i].first);
(now);
v[i] = now;
}
long long res = 1ll * n * (n - 1) * (n - 2) / 6;
sort(v, v + n);
for (int i = 0; i < n; ++i) v[i + n] = v[i] + 2 * pi;
int j = 0, k = 0;
v[2 * n] = 1000;
for (int i = 0; i < n; ++i) {
(v[i]);
while (v[j] < v[i] + pi - esp) ++j;
while (v[k + 1] < v[i] + pi + esp) ++k;
(i, j, k);
((long long)(k - j) * (k - j - 1) / 2,
(long long)(k - i - 1) * (k - i - 2) / 2);
res += (long long)(k - j + 1) * (k - j) / 2;
res -= (long long)(k - i) * (k - i - 1) / 2;
}
cout << res;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T, class U>
inline void Max(T &a, U b) {
if (a < b) a = b;
}
template <class T, class U>
inline void Min(T &a, U b) {
if (a > b) a = b;
}
inline void add(int &a, int b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
}
pair<int, int> p[700000];
int sign(pair<int, int> a) {
if (a.first <= 0 && a.second <= 0) return 1;
if (a.first > 0 && a.second < 0) return 2;
if (a.first > 0 && a.second >= 0) return 3;
return 4;
}
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
int c = sign(a), d = sign(b);
if (c != d) return c < d;
long long t = 1LL * a.first * b.second - 1LL * a.second * b.first;
if (t != 0) return t > 0;
return 1LL * a.first * a.first + 1LL * a.second * a.second <
1LL * b.first * b.first + 1LL * b.second * b.second;
}
inline long long cal(long long n) { return 1LL * n * (n - 1) / 2; }
inline long long cal3(int n) { return 1LL * n * (n - 1) / 2 * (n - 2) / 3; }
int det(pair<int, int> a, pair<int, int> b) {
long long t = 1LL * a.first * b.second - 1LL * a.second * b.first;
return t > 0 ? 1 : (t < 0 ? -1 : 0);
}
long long s[700000], cnt[700000];
int main() {
int T = 1, i, j, ca = 0, k, n, m;
int c, d;
scanf("%d%d%d", &n, &c, &d);
for (i = 0; i < n; i++) {
scanf("%d%d", &p[i].first, &p[i].second);
p[i].first -= c, p[i].second -= d;
}
sort(p, p + n, cmp);
long long ans = cal3(n);
for (i = 0, m = 0; i < n; i++) {
j = i + 1;
k = sign(p[i]);
while (j < n && sign(p[j]) == k && det(p[i], p[j]) == 0) j++;
p[m] = p[i], cnt[m] = j - i;
if (j - i >= 3) ans -= cal3(j - i);
i = j - 1;
m++;
}
for (i = 0; i < m; i++) s[i + 1] = s[i] + cnt[i], cnt[i + m] = cnt[i];
for (i = m; i < m + m; i++) s[i + 1] = s[i] + cnt[i];
for (i = 0, j = 0; i < m; i++) {
while (j < i + m && det(p[i], p[j >= m ? j - m : j]) >= 0) j++;
long long first = s[j] - s[i + 1];
if (first <= 0) continue;
ans -= cal(first) * cnt[i] + first * cal(cnt[i]);
k = j - 1 >= m ? j - 1 - m : j - 1;
if (j - 1 > i && sign(p[i]) > 2 && det(p[i], p[k]) == 0) {
ans += cnt[i] * cal(cnt[k]) + cal(cnt[i]) * cnt[k];
}
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int nm = 350000;
struct bg {
long long r, w;
bool operator<(bg that) const {
if (w * that.w > 0) {
return r * that.w < that.r * w;
}
if (w * that.w < 0) {
return r * that.w > that.r * w;
}
if (w == 0) {
if (that.w == 0) {
if (r > 0) {
return 0;
}
if (that.r < 0) return 0;
return 1;
}
if (r > 0) return 0;
return 1;
}
if (that.w == 0) {
if (that.r > 0) return 1;
return 0;
}
return 0;
}
bg() {}
bg(long long _r, long long _w) {
r = _r;
w = _w;
}
};
int n;
bg a[nm];
long long c, d;
vector<bg> be, to, khong;
int main() {
scanf("%d%I64d%I64d", &n, &c, &d);
for (int i = 1; i <= n; ++i) {
scanf("%I64d%I64d", &a[i].r, &a[i].w);
a[i].r -= c;
a[i].w -= d;
if (a[i].w < 0)
be.push_back(a[i]);
else if (a[i].w > 0)
to.push_back(a[i]);
else
khong.push_back(a[i]);
}
sort(be.begin(), be.end());
sort(to.begin(), to.end());
sort(khong.begin(), khong.end());
long long res = 0;
int j = -1, k = 0;
int nto = to.size(), nbe = be.size(), nkhong = khong.size();
for (int i = 0; i < nbe; ++i) {
while (j + 1 < nto && to[j + 1] < be[i]) j++;
while (k < nto && !(be[i] < to[k])) k++;
res += (long long)(j + 1) * (long long)(nto - k);
}
j = -1;
k = 0;
for (int i = 0; i < nto; ++i) {
while (j + 1 < nbe && be[j + 1] < to[i]) j++;
while (k < nbe && !(to[i] < be[k])) k++;
res += (long long)(j + 1) * (long long)(nbe - k);
}
j = -1;
k = 0;
for (int i = 0; i < nbe; ++i) {
while (j + 1 < nkhong && khong[j + 1] < be[i]) j++;
while (k < nto && !(be[i] < to[k])) k++;
res += (long long)(j + 1) * (long long)(nto - k);
}
j = -1;
k = 0;
for (int i = 0; i < nbe; ++i) {
while (j + 1 < nto && to[j + 1] < be[i]) j++;
while (k < nkhong && !(be[i] < khong[k])) k++;
res += (long long)(j + 1) * (long long)(nkhong - k);
}
printf("%I64d\n", res);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int read() {
char c = getchar();
int k = 0;
for (; c < 48 || c > 57; c = getchar())
;
for (; c > 47 && c < 58; c = getchar()) k = (k << 3) + (k << 1) + c - 48;
return k;
}
int n, nx, ny, i, l, r, st;
long long now, ans;
struct arr {
long double k;
int x, y;
} a[N];
bool operator<(arr A, arr B) { return A.k < B.k; }
long long cross(int xa, int ya, int xb, int yb) {
return (long long)xa * yb - (long long)xb * ya;
}
bool chk(int x, int y) { return cross(a[x].x, a[x].y, a[y].x, a[y].y) > 0; }
int find1(int x) {
int tl = l, tr = r, m;
while (tl < tr) {
m = tl + tr >> 1;
if (chk(m, x))
tr = m;
else
tl = m + 1;
}
return r - tl;
}
int find2(int x) {
int l = st - 1, r = n, m;
while (l < r) {
m = l + r + 1 >> 1;
if (chk(x, m))
l = m;
else
r = m - 1;
}
return l - st + 1;
}
int main() {
n = read();
nx = read();
ny = read();
for (i = 1; i <= n; i++) {
int x = read() - nx, y = read() - ny;
a[i] = (arr){atan2(y, x), x, y};
}
sort(a + 1, a + n + 1);
l = 1;
r = 1;
for (i = 1; i <= n; i++) {
for (; st <= n && !chk(st, i); st++) now -= find1(st);
if (st > n) break;
for (; r <= i ||
!cross(a[i].x, a[i].y, a[r].x, a[r].y) && fabs(a[i].k - a[r].k) < 1;
r++)
now += find2(r);
for (; chk(i, r); r++) now += find2(r);
for (; l < r && !chk(i, l); l++) now -= find2(l);
ans += now;
}
printf("%lld", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 444444;
struct vec {
long long x, y;
} stu[maxn];
bool operator<(vec a, vec b) {
if (a.y * b.y < 0)
return a.x * b.y > b.x * a.y;
else
return a.x * b.y < b.x * a.y;
}
bool operator==(vec a, vec b) { return (!(a < b) && !(b < a)); }
vector<vec> V;
int M;
long long sum[2][maxn];
long long zero[2];
int n;
long long lSum(int p, int i) { return sum[p][i]; }
long long rSum(int p, int i) { return sum[p][M] - sum[p][i - 1]; }
void solve() {
for (int i = 2; i <= M; i++) {
sum[0][i] += sum[0][i - 1];
sum[1][i] += sum[1][i - 1];
}
long long ans = 0;
for (int i = 1; i <= M; i++) {
for (int p = 0; p < 2; p++) {
int q = p ^ 1;
long long now = sum[p][i] - sum[p][i - 1];
ans += now * lSum(q, i - 1) * rSum(q, i + 1);
ans += now * rSum(q, i + 1) * zero[p];
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long C, D;
cin >> n >> C >> D;
V.clear();
for (int i = 1; i <= n; i++) {
cin >> stu[i].x >> stu[i].y;
stu[i].x -= C;
stu[i].y -= D;
if (stu[i].y == 0) {
if (stu[i].x < 0)
zero[0]++;
else
zero[1]++;
} else {
V.push_back(stu[i]);
}
}
sort(V.begin(), V.end());
V.resize(unique(V.begin(), V.end()) - V.begin());
M = V.size();
for (int i = 1; i <= n; i++) {
if (stu[i].y == 0) continue;
int id = lower_bound(V.begin(), V.end(), stu[i]) - V.begin() + 1;
if (stu[i].y < 0)
sum[0][id]++;
else
sum[1][id]++;
}
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long operator*(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.first * b.second - a.second * b.first;
}
bool operator^(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return (a > make_pair(0LL, 0LL) && b > make_pair(0LL, 0LL)) ||
(a < make_pair(0LL, 0LL) && b < make_pair(0LL, 0LL));
}
bool operator|(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return (a > make_pair(0LL, 0LL) && b < make_pair(0LL, 0LL));
}
void solve() {
long long n;
long long c, d;
cin >> n >> c >> d;
vector<pair<long long, long long> > p((size_t)n);
for (size_t i = 0; i < n; ++i) {
cin >> p[i].first >> p[i].second;
p[i].first -= c;
p[i].second -= d;
}
sort(p.begin(), p.end(),
[](pair<long long, long long> a, pair<long long, long long> b) {
if (!(a ^ b)) return a | b;
if (a * b == 0) return (a < b);
return a * b > 0;
});
for (int i = 0; i < n; ++i) {
p.push_back(p[i]);
}
long long res = n * (n - 1) * (n - 2) / 6;
c = 0;
for (int i = 0; i < n; i++) {
long long len[3] = {0, 0, 0};
int j = i + 1;
while (j < n && (p[i] ^ p[j]) && (p[i] * p[j]) == 0) j++;
if (c < j) {
c = j;
}
while (c < i + n && (p[i] * p[c]) >= 0) {
if ((p[i] * p[c]) == 0) len[2]++;
c++;
}
len[0] = c - j;
len[1] = j - i;
if (p[i] < make_pair(0LL, 0LL)) {
res += len[1] * (len[1] - 1) / 2 * len[2];
res += len[2] * (len[2] - 1) / 2 * len[1];
}
res -= len[0] * (len[0] - 1) / 2 * len[1];
res -= len[1] * (len[1] - 1) / 2 * len[0];
res -= len[1] * (len[1] - 1) * (len[1] - 2) / 6;
i = j - 1;
}
cout << res;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
long long gcd1(long long x, long long y) {
long long z = y;
while (x % y != 0) {
z = x % y;
x = y;
y = z;
}
return z;
}
struct Point {
long long x, y;
pair<long long, long long> vec;
Point() {}
Point(long long x, long long y) : x(x), y(y) {}
long double arg() const { return atan2(y, x); }
long long cross(Point b) const { return x * b.y - b.x * y; }
int getZone() const {
assert(x != 0 || y != 0);
if (x <= 0 && y < 0) return 1;
if (y < 0 || (y == 0 && x > 0)) return 2;
if ((y > 0 && x > 0) || (x == 0 && y > 0)) return 3;
return 4;
}
bool sameVec(const Point& o) {
return (cross(o) == 0 && x * o.x >= 0 && y * o.y >= 0);
}
pair<long long, long long> getvec() const { return vec; }
void setvec() {
if (x == 0)
vec = make_pair(0, y < 0 ? -1 : 1);
else if (y == 0)
vec = {x < 0 ? -1 : 1, 0};
else {
long long gg = gcd1(abs(x), abs(y));
long long X = x / gg, Y = y / gg;
vec = {X, Y};
}
}
bool operator<(const Point& o) const {
int a = getZone(), b = o.getZone();
if (a != b) return a < b;
long long tmp = cross(o);
if (tmp != 0) return tmp > 0;
return x == o.x ? y < o.y : x < o.x;
}
void readin() { scanf("%lld%lld", &x, &y); }
void pp() { printf("%lld %lld\n", x, y); }
} p[345687];
map<pair<long long, long long>, int> mp;
long long c2(long long x) {
if (x < 2) return 0;
return x * (x - 1) / 2;
}
void fmain() {
scanf("%d%d%d", &n, &m, &k);
for (int(i) = 0; (i) < (int)(n); (i)++) {
p[i].readin();
p[i].x -= m;
p[i].y -= k;
p[i].setvec();
}
if (debug) return;
sort(p, p + n);
long long ans = (long long)n * (n - 1) * (n - 2) / 6;
for (int(i) = 0; (i) < (int)(n); (i)++) mp[p[i].getvec()]++;
for (int i = 0, j = 1; i < n; i++) {
for (; j < n + i && p[i].cross(p[j % n]) >= 0; j++) {
if (p[i].getvec() == p[j % n].getvec() && j >= n && j % n <= i) break;
}
auto key = p[i].getvec();
int cc = j - i - 1;
ans -= c2(cc);
int cc1 = mp[{-key.first, -key.second}];
assert(cc >= cc1);
ans += c2(cc1);
}
printf("%lld\n", ans);
}
int main() {
fmain();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, C, D;
pair<int, int> A[400000];
long long Z, ZO, ZOZ, O, OZ, OZO;
bool cmp(pair<int, int> a, pair<int, int> b) {
if ((b.second < 0) ^ (a.second < 0))
return 1LL * a.first * b.second > 1LL * b.first * a.second;
return 1LL * a.first * b.second < 1LL * b.first * a.second;
}
bool eq(pair<int, int> a, pair<int, int> b) {
return 1LL * a.first * b.second == 1LL * b.first * a.second;
}
bool better(int i, int j) {
return 1LL * A[i].first * A[j].second > 1LL * A[i].second * A[j].first;
}
int main() {
scanf("%d%d%d", &N, &C, &D);
for (int i = 0; i < N; i++)
scanf("%d%d", &A[i].first, &A[i].second), A[i].first -= C, A[i].second -= D;
sort(A, A + N, cmp);
for (int i = 0, j; i < N; i = j) {
long long NZ = Z, NZO = ZO, NZOZ = ZOZ, NO = O, NOZ = OZ, NOZO = OZO;
for (j = i; j < N && eq(A[i], A[j]); j++) {
if (A[j].second < 0) {
NOZO += OZ;
NZO += Z;
NO++;
} else {
NZOZ += ZO;
NOZ += O;
NZ++;
}
}
Z = NZ, ZO = NZO, ZOZ = NZOZ, O = NO, OZ = NOZ, OZO = NOZO;
}
int L = 0, R = 0, D = 0;
for (int i = 0; i < N; i++)
if (A[i].second == 0) {
if (A[i].first < 0)
L++;
else
R++;
}
for (int i = 0; i < N; i++)
if (A[i].second < 0) D++;
printf("%lld\n", OZO + ZOZ - 1LL * L * R * D);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 512345, lgN = 20, rootN = 1123;
const double eps = 1e-18;
int r[N], w[N];
vector<double> positive, negative;
double p[N];
int main() {
int n, c, d;
cin >> n >> c >> d;
for (int i = 1; i <= n; ++i) {
cin >> r[i] >> w[i];
r[i] -= c, w[i] -= d;
p[i] = (((double)r[i]) / (double)w[i]);
}
for (int i = 1; i <= n; ++i) {
double x = (((double)r[i]) / (double)w[i]);
if (w[i] > 0) {
positive.push_back(x);
} else if (w[i] < 0) {
negative.push_back(x);
}
}
sort(positive.begin(), positive.end());
sort(negative.begin(), negative.end());
long long first = 0, second = 0;
for (int i = 0; i < positive.size(); ++i) {
first += (lower_bound(negative.begin(), negative.end(), positive[i]) -
negative.begin());
}
for (int i = 0; i < negative.size(); ++i) {
second += (lower_bound(positive.begin(), positive.end(), negative[i]) -
positive.begin());
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
if (w[i] > 0) {
ans += ((long long)(negative.end() - upper_bound(negative.begin(),
negative.end(), p[i]))) *
(lower_bound(negative.begin(), negative.end(), p[i]) -
negative.begin());
} else if (w[i] < 0) {
ans += ((long long)(positive.end() - upper_bound(positive.begin(),
positive.end(), p[i]))) *
(lower_bound(positive.begin(), positive.end(), p[i]) -
positive.begin());
} else if (w[i] == 0) {
if (r[i] < 0)
ans += first;
else
ans += second;
}
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 512345, lgN = 20, rootN = 1123;
const double eps = 1e-18;
int r[N], w[N];
vector<double> positive, negative;
double p[N];
int main() {
int n, c, d;
cin >> n >> c >> d;
for (int i = 1; i <= n; ++i) {
cin >> r[i] >> w[i];
r[i] -= c, w[i] -= d;
p[i] = (((double)r[i]) / (double)w[i]);
}
for (int i = 1; i <= n; ++i) {
if (w[i] > 0) {
positive.push_back(p[i]);
} else if (w[i] < 0) {
negative.push_back(p[i]);
}
}
sort(positive.begin(), positive.end());
sort(negative.begin(), negative.end());
long long first = 0, second = 0;
for (int i = 0; i < positive.size(); ++i) {
first += (lower_bound(negative.begin(), negative.end(), positive[i]) -
negative.begin());
}
for (int i = 0; i < negative.size(); ++i) {
second += (lower_bound(positive.begin(), positive.end(), negative[i]) -
positive.begin());
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
if (w[i] > 0) {
ans += ((long long)(negative.end() - upper_bound(negative.begin(),
negative.end(), p[i]))) *
(lower_bound(negative.begin(), negative.end(), p[i]) -
negative.begin());
} else if (w[i] < 0) {
ans += ((long long)(positive.end() - upper_bound(positive.begin(),
positive.end(), p[i]))) *
(lower_bound(positive.begin(), positive.end(), p[i]) -
positive.begin());
} else if (w[i] == 0) {
if (r[i] < 0)
ans += first;
else
ans += second;
}
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 345678 + 2;
int n;
struct Vector {
ll x, y;
Vector(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
Vector operator-(const Vector &a) const {
Vector ans;
ans.x = x - a.x;
ans.y = y - a.y;
return ans;
}
ll operator*(const Vector &a) { return x * a.y - y * a.x; }
bool operator<(const Vector &a) const {
bool ck1 = x < 0 || (x == 0 && y < 0),
ck2 = a.x < 0 || (a.x == 0 && a.y < 0);
if (ck1 != ck2) return ck1 < ck2;
return (x * a.y - y * a.x) > 0;
}
} a[N];
ll c, d;
ll ans(0);
int l[N * 4], p[N];
ll tmp[N * 4];
void Read() {
cin >> n >> c >> d;
for (int i = 0; i < n; ++i) {
cin >> a[i].x >> a[i].y;
a[i].x -= c;
a[i].y -= d;
}
sort(a, a + n);
}
struct com {
bool operator()(const int &x, const int &y) {
return l[x] - tmp[x] > l[y] - tmp[y];
}
};
void Solve() {
ll ans(0);
ll cur(0);
int j = 0;
for (int i = 0; i < n; ++i) {
int j = i;
while (j < n && a[i] * a[j] == 0) {
p[j] = i;
++j;
}
i = j - 1;
}
for (int i = 0; i < n; ++i) {
while (j < n + p[i] && a[i] * a[j % n] >= 0) {
if (i != j && a[i] * a[j % n] == 0)
++tmp[i];
else
tmp[i] = 0;
++j;
}
l[i] = j;
if (i < n - 1 && a[i] * a[i + 1] == 0)
tmp[i + 1] = tmp[i] - (tmp[i] == j - i - 1);
}
for (int i = n; i < 4 * n; ++i) l[i] = l[i - n] + n, tmp[i] = tmp[i - n];
priority_queue<int, vector<int>, com> s;
s.push(0);
j = 1;
ll TMP(0), TMPL(0);
cur = l[0] - tmp[0];
for (int i = 0; i < n; ++i) {
while (s.size() && l[s.top()] - tmp[s.top()] <= l[i]) {
cur -= l[s.top()] - tmp[s.top()];
s.pop();
}
while (j < l[i]) {
if (l[j] - tmp[j] > l[i]) {
if (a[i] * a[j % n] == 0) {
TMP += l[j] - tmp[j];
++TMPL;
}
cur += l[j] - tmp[j];
s.push(j);
}
++j;
}
ans += cur - 1ll * l[i] * s.size() - (TMP - TMPL * l[i]);
if (i == n - 1 || a[i] * a[i + 1] != 0) TMP = TMPL = 0;
}
cout << ans / 3;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(""
".INP",
"r")) {
freopen(
""
".INP",
"r", stdin);
freopen(
""
".OUT",
"w", stdout);
}
Read();
Solve();
}
|
#include <bits/stdc++.h>
using namespace std;
const long double pi = acos((long double)-1);
int main() {
int64_t n, c, d;
cin >> n >> c >> d;
vector<long double> ang;
for (int64_t i = 0; i < n; ++i) {
int64_t r, w;
cin >> r >> w;
r -= c;
w -= d;
swap(r, w);
ang.push_back(atan2(r, w));
ang.push_back(2 * pi + atan2(r, w));
}
sort(ang.begin(), ang.end());
int64_t ans = (n * (n - 1) * (n - 2)) / 6;
int64_t a1 = 0, a2 = 0;
for (int64_t i = 0; i < n; ++i) {
while (ang[a1] - ang[i] < pi - 1e-18) {
a1++;
}
while (ang[a2 + 1] - ang[i] < pi + 1e-18) a2++;
ans -= (a2 - i) * (a2 - i - 1) / 2;
ans += (a2 - a1) * (a2 - a1 + 1) / 2;
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, c, d;
vector<long double> pos;
vector<long double> pos_zero;
vector<long double> neg_zero;
vector<long double> neg;
vector<long long int> dppos;
vector<long long int> dpneg;
int main() {
scanf("%d%d%d", &n, &c, &d);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
x -= c;
y -= d;
long double ex = x;
long double ey = y;
if (y > 0)
pos.push_back(ex / ey);
else if (y < 0)
neg.push_back(ex / ey);
else if (x > 0)
pos_zero.push_back(100000007);
else if (x < 0)
neg_zero.push_back(-100000007);
}
sort(pos.begin(), pos.end());
sort(neg.begin(), neg.end());
dppos.resize(pos.size());
dpneg.resize(neg.size());
int last;
last = 0;
for (int i = 0; i < dppos.size(); i++) {
while (last < neg.size() && neg[last] < pos[i]) last++;
dppos[i] = last;
if (i) dppos[i] += dppos[i - 1];
}
last = 0;
for (int i = 0; i < dpneg.size(); i++) {
while (last < pos.size() && pos[last] < neg[i]) last++;
dpneg[i] = last;
if (i) dpneg[i] += dpneg[i - 1];
}
long long int cevap = 0;
for (int i = 0; i < dppos.size(); i++) {
vector<long double>::iterator it =
lower_bound(neg.begin(), neg.end(), pos[i]);
if (it != neg.begin()) {
it--;
int yer = it - neg.begin();
if (yer >= 0 && yer < dpneg.size()) cevap += dpneg[yer];
}
}
for (int i = 0; i < dpneg.size(); i++) {
vector<long double>::iterator it =
lower_bound(pos.begin(), pos.end(), neg[i]);
if (it != pos.begin()) {
it--;
int yer = it - pos.begin();
if (yer >= 0 && yer < dppos.size()) cevap += dppos[yer];
}
}
if (pos_zero.size() > 0 && dpneg.size() > 0)
cevap += dpneg.back() * pos_zero.size();
if (neg_zero.size() > 0 && dppos.size() > 0)
cevap += dppos.back() * neg_zero.size();
cout << cevap << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 350000;
struct P {
int x, y;
P() {}
P(int _x, int _y) {
x = _x;
y = _y;
}
bool be() const { return x ? x > 0 : y > 0; }
P operator+(const P &a) const { return P(x + a.x, y + a.y); }
P operator-(const P &a) const { return P(x - a.x, y - a.y); }
long long operator*(const P &a) const {
return 1ll * x * a.y - 1ll * y * a.x;
}
bool operator==(const P &a) const {
return 1ll * x * a.y == 1ll * y * a.x && be() == a.be();
}
} p[N * 2];
long long ans, SSS[N * 2];
int n, PX, PY;
int S[N * 2], SS[N * 2];
bool cmp(P x, P y) { return x.be() ^ y.be() ? x.be() : x * y > 0; }
int main() {
scanf("%d%d%d", &n, &PX, &PY);
for (int i = (int)(1); i <= (int)(n); i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].x -= PX;
p[i].y -= PY;
}
sort(p + 1, p + n + 1, cmp);
int top = 1;
S[1] = 1;
for (int i = (int)(2); i <= (int)(n); i++) {
if (!(p[i] == p[i - 1])) p[++top] = p[i];
++S[top];
}
n = top;
for (int i = (int)(1); i <= (int)(n); i++) {
p[i + n] = p[i];
S[i + n] = S[i];
}
for (int i = (int)(1); i <= (int)(n * 2); i++) {
SS[i] = SS[i - 1] + S[i];
SSS[i] = SSS[i - 1] + 1ll * S[i] * S[i];
}
for (int i = (int)(1); i <= (int)(n); i++)
ans += 1ll * S[i] * (1ll * SS[i - 1] * SS[i - 1] - SSS[i - 1]) / 2;
int p1 = 1;
for (int i = (int)(1); i <= (int)(n); i++) {
p1 = max(p1, i);
for (; p1 + 1 != i + n && p[i] * p[p1 + 1] >= 0; ++p1)
;
ans -= 1ll * S[i] *
(1ll * (SS[p1] - SS[i]) * (SS[p1] - SS[i]) - (SSS[p1] - SSS[i])) / 2;
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = 2 * acos(0);
const int maxn = 1000010;
int n, c, d;
long double angle[maxn];
long double getangle(long double x, long double y) {
if (x == 0) {
if (y > 0) return PI / 2;
return 3 * PI / 2;
}
if (y == 0) {
if (x > 0) return 0;
return PI;
}
long double res = atan(y / x);
if (x < 0) return res + PI;
if (y < 0) return res + 2 * PI;
return res;
}
void solve() {
scanf("%d%d%d", &n, &c, &d);
for (int i = (0); i < (n); i++) {
long long x, y;
scanf("%I64d%I64d", &x, &y);
angle[i] = getangle(x - c, y - d);
}
sort(angle, angle + n);
for (int i = (0); i < (n); i++) angle[i + n] = angle[i] + 2 * PI;
long long tot = 0;
for (int i = (0); i < (n); i++) {
int l = i, r = i + n;
while (l < r) {
int m = (l + r + 1) >> 1;
if (angle[m] - angle[i] < PI - 1e-18)
l = m;
else
r = m - 1;
}
int p = l;
l = i;
r = i + n;
while (l < r) {
int m = (l + r) >> 1;
if (angle[m] - angle[i] < PI + 1e-18)
l = m + 1;
else
r = m;
}
int q = l;
long long u = p - i;
long long v = n - (q - i);
long long w = q - p - 1;
tot += u * (u - 1) / 2 + v * (v - 1) / 2 + w * (u + v);
}
printf("%I64d", 1LL * n * (n - 1) * (n - 2) / 6 - tot / 2);
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double ang[1000000];
long long s[1000000], g[1000000];
int main() {
long double pi = atan2((long double)(0.0), (long double)(-1.0));
long double eps = 1e-18;
long long c, d, x, y;
long long i, j, k, n, m, ans = 0;
cin >> n >> c >> d;
for (i = 1; i <= n; i++) {
scanf("%I64d%I64d", &x, &y);
ang[i] = (long double)atan2((long double)(y - d), (long double)(x - c));
if (ang[i] < 0) ang[i] += (long double)(2.0) * pi;
}
sort(ang + 1, ang + 1 + n);
for (i = 1; i <= n; i++) ang[n + i] = ang[i] + (long double)(2.0) * pi;
j = 0;
for (i = 1; i <= n; i++) {
while (ang[j + 1] - ang[i] < pi - eps) j++;
s[i] = j;
}
for (i = 1; i <= n; i++) s[i + n] = s[i] + n;
for (i = 2; i <= n * 2; i++) s[i] += s[i - 1];
j = 0;
for (i = 1; i <= n; i++) {
while (ang[j + 1] - ang[i] < pi + eps) j++;
g[i] = j;
}
long long l = 1, r = 0;
for (i = 1; i <= n; i++) {
while (ang[l] - ang[i] < eps) l++;
while (ang[r + 1] - ang[i] < pi - eps) r++;
if (ang[l] - ang[i] > eps) ans += (s[r] - s[l - 1]) - g[i] * (r - l + 1);
}
cout << ans / 3;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
bool my_comp(const pair<pair<long long, long long>, int>& l,
const pair<pair<long long, long long>, int>& r) {
if (l.first.first * r.first.second == l.first.second * r.first.first)
return l.second < r.second;
return l.first.first * r.first.second < l.first.second * r.first.first;
}
int n, a, b, c, d;
vector<pair<pair<long long, long long>, int> > g;
int prevIdx[345700], nextIdx[345700];
long long cnt[345700][2], revcnt[345700][2];
int main() {
ios::sync_with_stdio(false);
cin >> n >> c >> d;
for (int i = 0; i < (int)n; i++) {
cin >> a >> b;
a -= c;
b -= d;
if (a >= 0 && b >= 0)
g.push_back(make_pair(make_pair(a, b), 0));
else if (a < 0 && b < 0)
g.push_back(make_pair(make_pair(-a, -b), 1));
else if (a >= 0)
g.push_back(make_pair(make_pair(a, -b), 2));
else
g.push_back(make_pair(make_pair(-a, b), 3));
}
sort(g.begin(), g.end(), &my_comp);
long long prevX = g[0].first.first, prevY = g[0].first.second, idx = 0;
int p = -1;
for (int i = 0; i < (int)n; i++) {
long long X = g[i].first.first, Y = g[i].first.second;
int P = g[i].second;
if (X * Y == 0) {
prevIdx[i] = i;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
prevIdx[i] = idx;
} else {
prevIdx[i] = i;
prevX = X;
prevY = Y;
idx = i;
p = P;
}
}
}
prevX = g[n - 1].first.first, prevY = g[n - 1].first.second, idx = n + 1,
p = -1;
for (int i = n - 1; i >= 0; i--) {
long long X = g[i].first.first, Y = g[i].first.second;
int P = g[i].second;
if (X * Y == 0) {
nextIdx[i] = i + 2;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
nextIdx[i] = idx;
} else {
nextIdx[i] = i + 2;
prevX = X;
prevY = Y;
idx = i + 2;
p = P;
}
}
}
for (int i = 0; i < (int)g.size(); i++) {
int p = g[i].second;
long long q = g[i].first.second;
if (q > 0) {
if (p == 0 || p == 3)
cnt[i + 1][0] = cnt[i][0] + 1, cnt[i + 1][1] = cnt[i][1];
else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1] + 1;
} else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1];
}
for (int i = g.size() - 1; i >= 0; i--) {
int p = g[i].second;
long long q = g[i].first.first;
if (q > 0) {
if (p == 0 || p == 2)
revcnt[i + 1][0] = revcnt[i + 2][0] + 1,
revcnt[i + 1][1] = revcnt[i + 2][1];
else
revcnt[i + 1][0] = revcnt[i + 2][0],
revcnt[i + 1][1] = revcnt[i + 2][1] + 1;
} else
revcnt[i + 1][0] = revcnt[i + 2][0], revcnt[i + 1][1] = revcnt[i + 2][1];
}
long long ans = 0;
for (int i = 0; i < (int)g.size(); i++) {
int p = g[i].second;
if (g[i].first.first * g[i].first.second == 0) continue;
long long c1 = cnt[prevIdx[i]][0], c2 = cnt[prevIdx[i]][1],
rc1 = revcnt[nextIdx[i]][0], rc2 = revcnt[nextIdx[i]][1];
if (p == 0)
ans += c2 * rc2;
else if (p == 1)
ans += c1 * rc1;
else if (p == 2)
ans += c1 * rc2;
else if (p == 3)
ans += c2 * rc1;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
inline void amax(T &a, const T &b) {
if (a < b) a = b;
}
const complex<long long> I(0, 1);
const long long EPS = 0;
int sign(double x) {
if (x < -EPS) return -1;
if (EPS < x) return 1;
return 0;
}
namespace std {
bool operator<(const complex<long long> &x, const complex<long long> &y) {
return real(x) != real(y) ? real(x) < real(y) : imag(x) < imag(y);
}
} // namespace std
long long cross(const complex<long long> &x, const complex<long long> &y) {
return imag(conj(x) * y);
}
struct CMP_ARG {
bool operator()(const complex<long long> &x,
const complex<long long> &y) const {
return sign(cross(x, y)) == 1;
}
} cmp_arg;
bool cmp_arg_2(const complex<long long> &x, const complex<long long> &y) {
return arg(complex<double>(x.real(), x.imag())) <
arg(complex<double>(y.real(), y.imag()));
}
int N;
long long C_sub, D_sub;
long long R[400011], W[400011];
vector<complex<long long> > U, D;
long long pos, neg;
int main() {
scanf("%d%lld%lld", &N, &C_sub, &D_sub);
for (int i = 0, i_len = (N); i < i_len; ++i) {
scanf("%lld%lld", R + i, W + i);
R[i] -= C_sub;
W[i] -= D_sub;
assert(abs(R[i]) + abs(W[i]) > 0);
}
for (int i = 0, i_len = (N); i < i_len; ++i) {
if (W[i] > 0)
U.push_back(complex<long long>(R[i], W[i]));
else if (W[i] < 0)
D.push_back(complex<long long>(R[i], W[i]));
else if (R[i] > 0)
pos++;
else
neg++;
}
sort(U.begin(), U.end(), cmp_arg);
sort(D.begin(), D.end(), cmp_arg);
long long ans = 0;
for (long long i = 0, lo = 0, hi = 0; i < (int)U.size(); i++) {
while (hi < (int)D.size() && cross(U[i], D[hi]) >= 0) hi++;
while (lo < (int)D.size() && cross(U[i], D[lo]) > 0) lo++;
ans += lo * ((long long)D.size() - hi);
ans += neg * ((long long)D.size() - hi);
ans += lo * pos;
}
for (long long i = 0, lo = 0, hi = 0; i < (int)D.size(); i++) {
while (hi < (int)U.size() && cross(D[i], U[hi]) >= 0) hi++;
while (lo < (int)U.size() && cross(D[i], U[lo]) > 0) lo++;
ans += lo * ((long long)U.size() - hi);
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433832795l;
template <typename T>
inline auto sqr(T x) -> decltype(x * x) {
return x * x;
}
template <typename T1, typename T2>
inline bool umx(T1& a, T2 b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
inline bool umn(T1& a, T2 b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
pair<long long, long long> operator+(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return make_pair(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return make_pair(a.first - b.first, a.second - b.second);
}
long long operator*(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.first * b.first + a.second * b.second;
}
long long operator^(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.first * b.second - a.second * b.first;
}
const int N = 400100;
struct Input {
int n;
pair<long long, long long> a[N];
pair<long long, long long> p;
void read(pair<long long, long long>& a) {
int u, v;
scanf("%d%d", &u, &v);
a = make_pair(u, v);
}
bool read() {
if (scanf("%d", &n) != 1) return 0;
read(p);
for (int i = int(0); i < int(n); ++i) {
read(a[i]);
}
return 1;
}
void init(const Input& input) { *this = input; }
};
struct Data : Input {
long long ans;
void write() { cout << ans << endl; }
virtual void solve() {}
virtual void clear() { *this = Data(); }
};
struct Solution : Data {
bool check(pair<long long, long long> a, pair<long long, long long> b,
pair<long long, long long> c, pair<long long, long long> p) {
pair<long long, long long> pair = a - p, push_back = b - p, pc = c - p;
long long suma = abs(pair ^ push_back), sumb = abs(push_back ^ pc),
sumc = abs(pc ^ pair);
if (suma == 0 || sumb == 0 || sumc == 0) return 0;
return suma + sumb + sumc == abs((a - c) ^ (b - c));
}
bool collin(pair<long long, long long> a, pair<long long, long long> b) {
return a * b > 0 && (a ^ b) == 0;
}
void solve() {
ans = 0;
for (int i = int(0); i < int(n); ++i) {
a[i] = a[i] - p;
}
sort(a, a + n,
[&](pair<long long, long long> a, pair<long long, long long> b) {
return atan2l(a.second, a.first) < atan2l(b.second, b.first);
});
;
ans = 0;
int t1 = 0;
int tek = 0;
for (int i = 0; i < n;) {
int j = i;
for (; j < n && collin(a[i], a[j]); ++j) {
if (t1 == j) {
(t1 += 1) %= n;
tek++;
}
}
while ((a[t1] ^ a[i]) <= 0 && !(collin(a[t1], a[i]))) {
(t1 += 1) %= n;
tek++;
};
tek -= j - i;
ans += 1ll * (j - i) * (tek) * (tek - 1) / 2 +
(1ll * (j - i) * (j - i - 1)) / 2 * tek;
i = j;
}
ans = (1ll * n * (n - 1) * (n - 2)) / 6 - ans;
for (int i = 0; i < n;) {
int j = i;
for (; j < n && collin(a[i], a[j]); ++j)
;
;
ans -= (2ll * (j - i) * (j - i - 1) * (j - i - 2)) / 6;
i = j;
}
for (int i = int(0); i < int(n); ++i) {
if (a[i].second < 0 || (a[i].second == 0 && a[i].first < 0)) {
a[i] = make_pair(-a[i].first, -a[i].second);
}
}
sort(a, a + n,
[&](pair<long long, long long> a, pair<long long, long long> b) {
return atan2l(a.second, a.first) < atan2l(b.second, b.first);
});
for (int i = 0; i < n;) {
int j = i;
for (; j < n && collin(a[i], a[j]); ++j)
;
;
ans += (1ll * (j - i) * (j - i - 1) * (j - i - 2)) / 6;
i = j;
}
}
void clear() { *this = Solution(); }
};
Solution sol;
int main() {
cout.setf(ios::showpoint | ios::fixed);
cout.precision(20);
sol.read();
sol.solve();
sol.write();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 350000;
struct P {
int x, y;
P() {}
P(int _x, int _y) {
x = _x;
y = _y;
}
bool be() const { return x ? x > 0 : y > 0; }
P operator+(const P &a) const { return P(x + a.x, y + a.y); }
P operator-(const P &a) const { return P(x - a.x, y - a.y); }
long long operator*(const P &a) const {
return 1ll * x * a.y - 1ll * y * a.x;
}
bool operator==(const P &a) const {
return 1ll * x * a.y == 1ll * y * a.x && be() == a.be();
}
} p[N * 2];
long long ans, SSS[N * 2];
int n, PX, PY;
int S[N * 2], SS[N * 2];
bool cmp(P x, P y) { return x.be() ^ y.be() ? x.be() : x * y > 0; }
int main() {
scanf("%d%d%d", &n, &PX, &PY);
for (int i = (int)(1); i <= (int)(n); i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].x -= PX;
p[i].y -= PY;
}
sort(p + 1, p + n + 1, cmp);
int top = 1;
S[1] = 1;
for (int i = (int)(2); i <= (int)(n); i++) {
if (!(p[i] == p[i - 1])) p[++top] = p[i];
++S[top];
}
n = top;
for (int i = (int)(1); i <= (int)(n); i++) {
p[i + n] = p[i];
S[i + n] = S[i];
}
for (int i = (int)(1); i <= (int)(n * 2); i++) {
SS[i] = SS[i - 1] + S[i];
SSS[i] = SSS[i - 1] + 1ll * S[i] * S[i];
}
for (int i = (int)(1); i <= (int)(n); i++)
ans += 1ll * S[i] * (1ll * SS[i - 1] * SS[i - 1] - SSS[i - 1]) / 2;
int p1 = 1;
for (int i = (int)(1); i <= (int)(n); i++) {
p1 = max(p1, i);
for (; p1 + 1 != i + n && p[i] * p[p1 + 1] >= 0; ++p1)
;
ans -= 1ll * S[i] *
(1ll * (SS[p1] - SS[i]) * (SS[p1] - SS[i]) - (SSS[p1] - SSS[i])) / 2;
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
bool my_comp(const pair<pair<long long, long long>, int>& l,
const pair<pair<long long, long long>, int>& r) {
if (l.first.first * r.first.second == l.first.second * r.first.first)
return l.second < r.second;
return l.first.first * r.first.second < l.first.second * r.first.first;
}
int n, a, b, c, d;
vector<pair<pair<long long, long long>, int> > g;
int prevIdx[345700], nextIdx[345700];
long long cnt[345700][2], revcnt[345700][2];
int main() {
scanf("%d%d%d", &n, &c, &d);
for (int i = 0; i < (int)n; i++) {
scanf("%d%d", &a, &b);
a -= c;
b -= d;
if (a >= 0 && b >= 0)
g.push_back(make_pair(make_pair(a, b), 0));
else if (a < 0 && b < 0)
g.push_back(make_pair(make_pair(-a, -b), 1));
else if (a >= 0)
g.push_back(make_pair(make_pair(a, -b), 2));
else
g.push_back(make_pair(make_pair(-a, b), 3));
}
sort(g.begin(), g.end(), &my_comp);
long long prevX = g[0].first.first, prevY = g[0].first.second, idx = 0;
int p = -1;
for (int i = 0; i < (int)n; i++) {
long long X = g[i].first.first, Y = g[i].first.second;
int P = g[i].second;
if (X * Y == 0) {
prevIdx[i] = i;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
prevIdx[i] = idx;
} else {
prevIdx[i] = i;
prevX = X;
prevY = Y;
idx = i;
p = P;
}
}
}
prevX = g[n - 1].first.first, prevY = g[n - 1].first.second, idx = n + 1,
p = -1;
for (int i = n - 1; i >= 0; i--) {
long long X = g[i].first.first, Y = g[i].first.second;
int P = g[i].second;
if (X * Y == 0) {
nextIdx[i] = i + 2;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
nextIdx[i] = idx;
} else {
nextIdx[i] = i + 2;
prevX = X;
prevY = Y;
idx = i + 2;
p = P;
}
}
}
for (int i = 0; i < (int)g.size(); i++) {
int p = g[i].second;
long long q = g[i].first.second;
if (q > 0) {
if (p == 0 || p == 3)
cnt[i + 1][0] = cnt[i][0] + 1, cnt[i + 1][1] = cnt[i][1];
else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1] + 1;
} else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1];
}
for (int i = g.size() - 1; i >= 0; i--) {
int p = g[i].second;
long long q = g[i].first.first;
if (q > 0) {
if (p == 0 || p == 2)
revcnt[i + 1][0] = revcnt[i + 2][0] + 1,
revcnt[i + 1][1] = revcnt[i + 2][1];
else
revcnt[i + 1][0] = revcnt[i + 2][0],
revcnt[i + 1][1] = revcnt[i + 2][1] + 1;
} else
revcnt[i + 1][0] = revcnt[i + 2][0], revcnt[i + 1][1] = revcnt[i + 2][1];
}
long long ans = 0;
for (int i = 0; i < (int)g.size(); i++) {
int p = g[i].second;
if (g[i].first.first * g[i].first.second == 0) continue;
long long c1 = cnt[prevIdx[i]][0], c2 = cnt[prevIdx[i]][1],
rc1 = revcnt[nextIdx[i]][0], rc2 = revcnt[nextIdx[i]][1];
if (p == 0)
ans += c2 * rc2;
else if (p == 1)
ans += c1 * rc1;
else if (p == 2)
ans += c1 * rc2;
else if (p == 3)
ans += c2 * rc1;
}
printf("%I64d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, t...);
}
const int maxn = 4e5 + 5;
const long double pi = acos((long double)-1);
const long double esp = 1e-18;
pair<int, int> a[maxn];
int n;
long double v[maxn * 2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("TEST"
".INP",
"r"))
freopen(
"TEST"
".INP",
"r", stdin),
freopen(
"TEST"
".OUT",
"w", stdout);
cin >> n;
cin >> a[n].first >> a[n].second;
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
a[i].first -= a[n].first;
a[i].second -= a[n].second;
long double now = atan2((long double)a[i].second, (long double)a[i].first);
(now);
v[i] = now;
}
long long res = 1ll * n * (n - 1) * (n - 2) / 6;
sort(v, v + n);
for (int i = 0; i < n; ++i) v[i + n] = v[i] + 2 * pi;
int j = 0, k = 0;
v[2 * n] = 1000;
for (int i = 0; i < n; ++i) {
(v[i]);
while (v[j] < v[i] + pi - esp) ++j;
while (v[k] < v[i] + pi + esp) ++k;
(i, j, k);
((long long)(k - j) * (k - j - 1) / 2,
(long long)(k - i - 1) * (k - i - 2) / 2);
res += (long long)(k - j) * (k - j - 1) / 2;
res -= (long long)(k - i - 2) * (k - i - 1) / 2;
}
cout << res;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
const long double PI = 3.141592653589793238462643383279502884197;
const int MOD = 666013;
const int NMax = 350000;
int N, C, D;
int lastIndexForSearch;
long long ans = 0LL;
struct Punct;
long long diferente[NMax], diferentePartiale[NMax];
long long sqr(const long long &x);
long long Determinant(const Punct &A, const Punct &B, const Punct &C);
long long Dist2(const Punct &A, const Punct &B);
long double GetAngle(const Punct &A);
int GetCadran(const Punct &A);
long long CompareUnghiPolar(const Punct &A, const Punct &B);
struct Punct {
int x, y;
Punct() {
this->x = 0;
this->y = 0;
}
Punct(const int x, const int y) {
this->x = x;
this->y = y;
}
inline bool operator<(const Punct &other) const {
long long s = CompareUnghiPolar(*this, other);
if (s == 0) return Dist2(Punct(0, 0), *this) < Dist2(Punct(0, 0), other);
return s == -1;
}
};
Punct a[NMax];
enum InsertType {
InsertCoord,
InsertCntStrict,
InsertCntNestrict,
InsertIndexNestrict
};
enum QueryType {
QueryCoord,
QueryCntStrict,
QueryCntNestrict,
QueryIndexNestrict
};
struct HashNode {
int x, y, indexNestrict;
long long cntStrict, cntNestrict;
HashNode() {
x = y = indexNestrict = 0;
cntStrict = cntNestrict = 0LL;
}
HashNode(const int &x, const int &y, const int &indexNestrict,
const long long &cntStrict, const long long &cntNestrict) {
this->x = x;
this->y = y;
this->indexNestrict = indexNestrict;
this->cntStrict = cntStrict;
this->cntNestrict = cntNestrict;
}
};
vector<HashNode> H[MOD];
struct CntEvidence {
int x, y;
long long cnt;
CntEvidence() {
x = y = 0;
cnt = 0LL;
}
CntEvidence(const int &x, const int &y, const long long &cnt) {
this->x = x;
this->y = y;
this->cnt = cnt;
}
inline bool operator<(const CntEvidence &other) const {
long long s =
CompareUnghiPolar(Punct(this->x, this->y), Punct(other.x, other.y));
if (s == 0)
return Dist2(Punct(0, 0), Punct(this->x, this->y)) <
Dist2(Punct(0, 0), Punct(other.x, other.y));
return s == -1;
}
};
CntEvidence maiMiciStrictVector[NMax];
CntEvidence maiMiciStrictVectorSumePartiale[NMax];
CntEvidence maiMiciSauEgaleVector[NMax];
CntEvidence maiMiciSauEgaleVectorSumePartiale[NMax];
int NmaiMiciStrictVector;
int NmaiMiciSauEgaleVector;
int gcd(int x, int y) {
int r;
while (y) {
r = x % y;
x = y;
y = r;
}
return x;
}
void Insert(const int &x, const int &y, InsertType type, long long param = 0) {
int codh = (abs(x) + abs(y)) % MOD;
if (type == InsertCoord) {
for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end();
++it) {
if (it->x == x && it->y == y) return;
}
H[codh].push_back(HashNode(x, y, 0, 0, 0));
return;
} else if (type == InsertCntStrict) {
for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end();
++it) {
if (it->x == x && it->y == y) {
it->cntStrict = param;
return;
}
}
H[codh].push_back(HashNode(x, y, 0, param, 0));
return;
} else if (type == InsertCntNestrict) {
for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end();
++it) {
if (it->x == x && it->y == y) {
it->cntNestrict = param;
return;
}
}
H[codh].push_back(HashNode(x, y, 0, 0, param));
return;
} else if (type == InsertIndexNestrict) {
for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end();
++it) {
if (it->x == x && it->y == y) {
it->indexNestrict = param;
return;
}
}
H[codh].push_back(HashNode(x, y, param, 0, 0));
return;
}
}
void FindMaiMiciStrict(int x, int y) {
int st = 1, dr = lastIndexForSearch;
int ret = 0;
while (st <= dr) {
int mij = (st + dr) / 2;
if (CompareUnghiPolar(a[mij], Punct(x, y)) < 0) {
ret = mij;
st = mij + 1;
} else
dr = mij - 1;
}
Insert(x, y, InsertCntStrict, ret);
maiMiciStrictVector[++NmaiMiciStrictVector] = CntEvidence(x, y, ret);
}
void FindMaiMiciSauEgale(int x, int y) {
int st = 1, dr = lastIndexForSearch;
int ret = 0;
while (st <= dr) {
int mij = (st + dr) / 2;
if (CompareUnghiPolar(a[mij], Punct(x, y)) <= 0) {
ret = mij;
st = mij + 1;
} else
dr = mij - 1;
}
Insert(x, y, InsertCntNestrict, ret);
maiMiciSauEgaleVector[++NmaiMiciSauEgaleVector] = CntEvidence(x, y, ret);
}
long long QueryHash(int x, int y, QueryType type) {
x = -x;
y = -y;
if (y == 0) {
if (x < 0)
x = -1;
else if (x > 0)
x = 1;
} else if (x == 0) {
if (y < 0)
y = -1;
else if (y > 0)
y = 1;
} else {
int semnx = 1;
int semny = 1;
if (x < 0) {
x = -x;
semnx = -1;
}
if (y < 0) {
y = -y;
semny = -1;
}
int cmmdc = gcd(x, y);
x = x / cmmdc;
y = y / cmmdc;
x = x * semnx;
y = y * semny;
}
int codh = (abs(x) + abs(y)) % MOD;
for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end();
++it) {
if (it->x == x && it->y == y) {
if (type == QueryCntStrict) return it->cntStrict;
if (type == QueryCntNestrict) return it->cntNestrict;
if (type == QueryIndexNestrict) return it->indexNestrict;
}
}
return -1;
}
void Solve() {
int loc;
loc = 0;
for (int i = 1; i <= N; ++i) {
if (a[i].y > 0 || (a[i].y == 0 && a[i].x > 0)) {
++loc;
swap(a[i], a[loc]);
}
}
sort(a + 1, a + loc + 1);
sort(a + loc + 1, a + N + 1);
lastIndexForSearch = -1;
NmaiMiciStrictVector = 0;
NmaiMiciSauEgaleVector = 0;
int specialIndex = -1;
for (int i = 1; i <= N; ++i) {
if (GetAngle(a[i]) >= PI) {
if (a[i].y == 0) {
Insert(1, 0, InsertCoord);
FindMaiMiciStrict(1, 0);
FindMaiMiciSauEgale(1, 0);
continue;
}
if (a[i].x == 0) {
Insert(0, 1, InsertCoord);
FindMaiMiciStrict(0, 1);
FindMaiMiciSauEgale(0, 1);
continue;
}
int x, y;
x = -a[i].x;
y = -a[i].y;
int semnx = 1, semny = 1;
if (x < 0) {
x = -x;
semnx = -1;
}
if (y < 0) {
y = -y;
semny = -1;
}
int cmmdc = gcd(x, y);
x = x / cmmdc;
y = y / cmmdc;
x = x * semnx;
y = y * semny;
Insert(x, y, InsertCoord);
FindMaiMiciStrict(x, y);
FindMaiMiciSauEgale(x, y);
} else {
lastIndexForSearch = i;
}
}
bool foundOnRightOX = false;
specialIndex = NmaiMiciSauEgaleVector;
for (int i = 1; i <= N; ++i) {
if (a[i].y == 0 && a[i].x > 0) {
foundOnRightOX = true;
Insert(-1, 0, InsertCoord);
FindMaiMiciStrict(-1, 0);
FindMaiMiciSauEgale(-1, 0);
}
}
loc = 0;
for (int i = 1; i <= NmaiMiciStrictVector; ++i) {
if (maiMiciStrictVector[i].y > 0 ||
(maiMiciStrictVector[i].y == 0 && maiMiciStrictVector[i].x > 0)) {
++loc;
swap(maiMiciStrictVector[i], maiMiciStrictVector[loc]);
}
}
sort(maiMiciStrictVector + 1, maiMiciStrictVector + loc + 1);
sort(maiMiciStrictVector + loc + 1,
maiMiciStrictVector + NmaiMiciStrictVector + 1);
for (int i = 1; i <= NmaiMiciStrictVector; ++i) {
maiMiciStrictVectorSumePartiale[i].x = maiMiciStrictVector[i].x;
maiMiciStrictVectorSumePartiale[i].y = maiMiciStrictVector[i].y;
maiMiciStrictVectorSumePartiale[i].cnt =
((i > 1) ? (maiMiciStrictVectorSumePartiale[i - 1].cnt) : (0)) +
maiMiciStrictVector[i].cnt;
}
loc = 0;
for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) {
if (maiMiciSauEgaleVector[i].y > 0 ||
(maiMiciSauEgaleVector[i].y == 0 && maiMiciSauEgaleVector[i].x > 0)) {
++loc;
swap(maiMiciSauEgaleVector[i], maiMiciSauEgaleVector[loc]);
}
}
sort(maiMiciSauEgaleVector + 1, maiMiciSauEgaleVector + loc + 1);
sort(maiMiciSauEgaleVector + loc + 1,
maiMiciSauEgaleVector + NmaiMiciSauEgaleVector + 1);
for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) {
maiMiciSauEgaleVectorSumePartiale[i].x = maiMiciSauEgaleVector[i].x;
maiMiciSauEgaleVectorSumePartiale[i].y = maiMiciSauEgaleVector[i].y;
maiMiciSauEgaleVectorSumePartiale[i].cnt =
((i > 1) ? (maiMiciSauEgaleVectorSumePartiale[i - 1].cnt) : (0)) +
maiMiciSauEgaleVector[i].cnt;
}
for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) {
int index = i;
int x = maiMiciSauEgaleVectorSumePartiale[i].x;
int y = maiMiciSauEgaleVectorSumePartiale[i].y;
Insert(x, y, InsertIndexNestrict, index);
}
for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i)
diferente[i] =
max(0LL, maiMiciSauEgaleVector[i].cnt - maiMiciStrictVector[i].cnt);
for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i)
diferentePartiale[i] = diferentePartiale[i - 1] + diferente[i];
if (lastIndexForSearch != -1 && NmaiMiciSauEgaleVector != 0 &&
NmaiMiciStrictVector != 0) {
for (int i = lastIndexForSearch + 1; i <= N; ++i) {
if (a[i].y == 0 && a[i].x < 0) {
int index;
if (foundOnRightOX) {
index = specialIndex;
} else
index = NmaiMiciSauEgaleVector;
if (index <= 0) continue;
long long total = maiMiciSauEgaleVectorSumePartiale[index].cnt;
long long total2;
int jndex = QueryHash(a[i].x, a[i].y, QueryIndexNestrict);
if (jndex == -1) continue;
total2 = maiMiciSauEgaleVectorSumePartiale[jndex].cnt +
1LL * (index - jndex) * maiMiciSauEgaleVector[jndex].cnt;
long long inPlus = diferentePartiale[index];
if (foundOnRightOX) inPlus = inPlus - diferentePartiale[jndex];
ans += max(0LL, total - total2 - inPlus);
} else {
int index;
index = NmaiMiciSauEgaleVector;
if (index <= 0) continue;
long long total = maiMiciSauEgaleVectorSumePartiale[index].cnt;
long long total2;
int jndex = QueryHash(a[i].x, a[i].y, QueryIndexNestrict);
if (jndex == -1) continue;
total2 = maiMiciSauEgaleVectorSumePartiale[jndex].cnt +
1LL * (index - jndex) * maiMiciSauEgaleVector[jndex].cnt;
long long inPlus = diferentePartiale[index];
inPlus = inPlus - diferentePartiale[jndex];
ans += max(0LL, total - total2 - inPlus);
}
}
}
}
void EliminaDuplicate() {
int maiMici180, cnt;
long long total;
maiMici180 = 0;
total = 0LL;
for (int i = 1; i <= N; ++i) {
int x = a[i].x, y = a[i].y;
if (y > 0 || (y == 0 && x > 0)) ++maiMici180;
}
for (int i = 1; i <= N; ++i) {
if (a[i].y < 0) {
int x = -a[i].x;
int y = -a[i].y;
int st = 1, dr = maiMici180;
int val = 0;
while (st <= dr) {
int mij = (st + dr) / 2;
if (Determinant(Punct(0, 0), Punct(x, y), a[mij]) <= 0) {
val = mij;
st = mij + 1;
} else
dr = mij - 1;
}
total += max(0, maiMici180 - val);
}
}
cnt = 0;
for (int i = 1; i <= N; ++i)
if (a[i].y == 0 && a[i].x > 0) ++cnt;
ans = ans - 1LL * total * cnt;
}
int main() {
std::ios_base::sync_with_stdio(false);
cin >> N >> C >> D;
for (int i = 1; i <= N; ++i) {
int x, y;
cin >> x >> y;
a[i] = Punct(x - C, y - D);
}
Solve();
EliminaDuplicate();
for (int i = 0; i < MOD; ++i) H[i].clear();
for (int i = 1; i <= N; ++i) {
a[i].x = -a[i].x;
a[i].y = -a[i].y;
}
Solve();
EliminaDuplicate();
cout << ans << "\n";
return 0;
}
long long sqr(const long long &x) { return x * x; }
long long Determinant(const Punct &A, const Punct &B, const Punct &C) {
return 1LL * (A.x - C.x) * (B.y - C.y) - 1LL * (A.y - C.y) * (B.x - C.x);
}
long long Dist2(const Punct &A, const Punct &B) {
return sqr(A.x - B.x) + sqr(A.y - B.y);
}
int GetCadran(const Punct &A) {
if (A.x > 0 && A.y > 0) return 1;
if (A.x < 0 && A.y > 0) return 2;
if (A.x < 0 && A.y < 0) return 3;
return 4;
}
long double GetAngle(const Punct &A) {
long double ret = atan2(A.y, A.x);
if (A.y < 0) ret = ret + PI * 2.0;
return ret;
}
long long CompareUnghiPolar(const Punct &A, const Punct &B) {
long long ret = Determinant(Punct(0, 0), A, B);
if (ret > 0) return -1;
if (ret < 0) return 1;
if (A.y == 0 && B.y == 0) {
if ((A.x > 0 && B.x > 0) || (A.x < 0 && B.x < 0)) return 0;
if (A.x > 0 && B.x < 0) return -1;
return 1;
}
if (A.x == 0 && B.x == 0) {
if ((A.y > 0 && B.y > 0) || (A.y < 0 && B.y < 0)) return 0;
if (A.y > 0 && B.y < 0) return -1;
return 1;
}
int semnxA, semnxB, semnyA, semnyB;
semnxA = semnxB = semnyA = semnyB = 1;
if (A.x < 0) semnxA = -1;
if (B.x < 0) semnxB = -1;
if (A.y < 0) semnyA = -1;
if (B.y < 0) semnyB = -1;
if (semnxA == semnxB && semnyA == semnyB) return 0;
if (semnyA == 1) return -1;
return 1;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos((long double)-1), eps = 1e-18;
long double ang[800005];
long long ans = 0, n, c, d;
int main() {
scanf("%I64d%I64d%I64d", &n, &c, &d);
for (int i = 1, r, w; i <= n; i++) {
scanf("%d%d", &r, &w);
ang[i] = atan2l(w - d, r - c);
}
sort(ang + 1, ang + n + 1);
for (int i = 1; i <= n + 1; i++) ang[n + i] = ang[i] + 2 * PI;
ans = n * (n - 1) * (n - 2) / 6;
long long low = 1, hi = 1;
for (int i = 1; i <= n; i++) {
while (low <= n * 2 && ang[low + 1] - ang[i] < PI - eps) low++;
while (hi <= n * 2 && ang[hi + 1] - ang[i] < PI + eps) hi++;
ans -= (hi - i) * (hi - i - 1) / 2;
ans += (hi - low) * (hi - low - 1) / 2;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct PT {
int x, y;
PT(int x = 0, int y = 0) : x(x), y(y) {}
void in() { scanf("%d%d", &x, &y); }
PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
long long operator*(const PT &p) const {
return 1LL * x * p.y - 1LL * y * p.x;
}
};
bool cmp(PT a, PT b) {
if (a.y >= 0 && b.y < 0) return 1;
if (a.y < 0 && b.y >= 0) return 0;
if (a.y == 0 && b.y == 0) {
if (a.x >= 0 && b.x < 0)
return 1;
else
return 0;
}
return a * b > 0;
}
const int N = 440000;
int n;
PT A, B[N + N];
int main() {
scanf("%d", &n);
A.in();
for (int i = 0; i < n; i++) {
B[i].in();
B[i] = B[i] - A;
}
sort(B, B + n, cmp);
for (int i = n; i < n + n; i++) B[i] = B[i - n];
long long ans = 0;
int j = 0, pr = 0;
for (int i = 0; i < n; i++) {
if (B[i] * B[pr] != 0 || (B[i] * B[pr] == 0 && 1LL * B[i].x * B[j].x <= 0 &&
1LL * B[i].y * B[j].y <= 0))
pr = i;
while (B[i] * B[j] >= 0 && j < pr + n) j++;
int cnt = j - i;
ans += 1LL * (cnt - 1) * (cnt - 2) / 2;
}
j = 0;
while (j < n) {
int i = j;
while (j < n && B[i] * B[j] == 0 && 1LL * B[i].x * B[j].x >= 0 &&
1LL * B[i].y * B[j].y >= 0)
++j;
int cnt = j - i;
ans += 1LL * cnt * (cnt - 1) * (cnt - 2) / 6;
}
for (int i = 0; i < n; i++) {
if (B[i].x < 0 || (B[i].x == 0 && B[i].y < 0)) B[i] = PT(-B[i].x, -B[i].y);
}
sort(B, B + n, cmp);
int i = 0;
while (i < n) {
int j = i;
while (i < n && B[i] * B[j] == 0) ++i;
int cnt = i - j;
ans -= 1LL * cnt * (cnt - 1) * (cnt - 2) / 6;
}
cout << 1LL * n * (n - 1) * (n - 2) / 6 - ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
bool my_comp(const pair<pair<long long, long long>, int>& l,
const pair<pair<long long, long long>, int>& r) {
if (l.first.first * r.first.second == l.first.second * r.first.first)
return l.second < r.second;
return l.first.first * r.first.second < l.first.second * r.first.first;
}
int n, a, b, c, d;
vector<pair<pair<long long, long long>, int> > g;
int prevIdx[345700], nextIdx[345700];
long long cnt[345700][2], revcnt[345700][2];
int main() {
cin >> n >> c >> d;
for (int i = 0; i < (int)n; i++) {
cin >> a >> b;
a -= c;
b -= d;
if (a >= 0 && b >= 0)
g.push_back(make_pair(make_pair(a, b), 0));
else if (a < 0 && b < 0)
g.push_back(make_pair(make_pair(-a, -b), 1));
else if (a >= 0)
g.push_back(make_pair(make_pair(a, -b), 2));
else
g.push_back(make_pair(make_pair(-a, b), 3));
}
sort(g.begin(), g.end(), &my_comp);
long long prevX = g[0].first.first, prevY = g[0].first.second, idx = 0;
int p = -1;
for (int i = 0; i < (int)n; i++) {
long long X = g[i].first.first, Y = g[i].first.second;
int P = g[i].second;
if (X * Y == 0) {
prevIdx[i] = i;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
prevIdx[i] = idx;
} else {
prevIdx[i] = i;
prevX = X;
prevY = Y;
idx = i;
p = P;
}
}
}
prevX = g[n - 1].first.first, prevY = g[n - 1].first.second, idx = n + 1,
p = -1;
for (int i = n - 1; i >= 0; i--) {
long long X = g[i].first.first, Y = g[i].first.second;
int P = g[i].second;
if (X * Y == 0) {
nextIdx[i] = i + 2;
} else {
if (X * prevY == Y * prevX && p / 2 == P / 2) {
nextIdx[i] = idx;
} else {
nextIdx[i] = i + 2;
prevX = X;
prevY = Y;
idx = i + 2;
p = P;
}
}
}
for (int i = 0; i < (int)g.size(); i++) {
int p = g[i].second;
long long q = g[i].first.second;
if (q > 0) {
if (p == 0 || p == 3)
cnt[i + 1][0] = cnt[i][0] + 1, cnt[i + 1][1] = cnt[i][1];
else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1] + 1;
} else
cnt[i + 1][0] = cnt[i][0], cnt[i + 1][1] = cnt[i][1];
}
for (int i = g.size() - 1; i >= 0; i--) {
int p = g[i].second;
long long q = g[i].first.first;
if (q > 0) {
if (p == 0 || p == 2)
revcnt[i + 1][0] = revcnt[i + 2][0] + 1,
revcnt[i + 1][1] = revcnt[i + 2][1];
else
revcnt[i + 1][0] = revcnt[i + 2][0],
revcnt[i + 1][1] = revcnt[i + 2][1] + 1;
} else
revcnt[i + 1][0] = revcnt[i + 2][0], revcnt[i + 1][1] = revcnt[i + 2][1];
}
long long ans = 0;
for (int i = 0; i < (int)g.size(); i++) {
int p = g[i].second;
if (g[i].first.first * g[i].first.second == 0) continue;
long long c1 = cnt[prevIdx[i]][0], c2 = cnt[prevIdx[i]][1],
rc1 = revcnt[nextIdx[i]][0], rc2 = revcnt[nextIdx[i]][1];
if (p == 0)
ans += c2 * rc2;
else if (p == 1)
ans += c1 * rc1;
else if (p == 2)
ans += c1 * rc2;
else if (p == 3)
ans += c2 * rc1;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct PT {
long long x, y;
PT() {}
PT(long long x, long long y) : x(x), y(y) {}
PT(const PT &p) : x(p.x), y(p.y) {}
PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
PT operator*(long long c) const { return PT(x * c, y * c); }
PT operator/(long long c) const { return PT(x / c, y / c); }
bool operator<(const PT &p) const {
return make_pair(x, y) < make_pair(p.x, p.y);
}
bool operator==(const PT &p) const { return !(*this < p) && !(p < *this); }
};
long long dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
long long dist2(PT p, PT q) { return dot(p - q, p - q); }
long long cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
PT norm(PT x, long long l) { return x * sqrt(l * l / dot(x, x)); }
istream &operator>>(istream &is, PT &p) { return is >> p.x >> p.y; }
ostream &operator<<(ostream &os, const PT &p) {
return os << "(" << p.x << "," << p.y << ")";
}
PT center;
bool cmp(PT a, PT b) {
if (a.x == center.x && a.y == center.y) return false;
if (b.x == center.x && b.y == center.y) return true;
if (a.x >= center.x && b.x < center.x) return true;
if (a.x < center.x && b.x >= center.x) return false;
if (a.x - center.x == 0 && b.x - center.x == 0) {
if (a.y - center.y >= 0 || b.y - center.y >= 0) return a.y > b.y;
return b.y > a.y;
}
return cross(a - center, b - center) < 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long c, d;
cin >> N >> c >> d;
vector<PT> v(N);
vector<int> en(N << 1);
for (int i = 0; i < N; i++) cin >> v[i], v[i].x -= c, v[i].y -= d;
center = {0, 0};
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < N; i++) v.push_back(v[i]);
for (int i = 0, k = 0; i < N;) {
int j = i;
while (j < N && cross(v[i], v[j]) == 0 &&
min(dot(v[j], v[i]), dot(v[i], v[j])) > 0)
j++;
k = max(k, j);
while (k < (N << 1) && cross(v[i], v[k]) < 0) k++;
for (; i < j; i++) en[i] = k - 1, en[i + N] = k - 1 + N;
}
long long sum2 = 0;
long long sum1 = 0;
long long res = 0;
for (int i = 0, k = 0, j = 0, s = 0; i < N; i = j) {
j = i;
while (k <= en[i]) {
sum2 += en[k] + 1;
sum1++;
k++;
}
while (j < N && cross(v[i], v[j]) == 0 &&
min(dot(v[j], v[i]), dot(v[i], v[j])) > 0) {
j++;
}
while (s < j) {
sum1--;
sum2 -= en[s] + 1;
s++;
}
int ref = en[i] + 1;
while (cross(v[ref], v[i]) == 0 &&
min(dot(v[ref], v[i]), dot(v[i], v[ref])) < 0)
ref++;
while (en[s] < ref) {
sum1--;
sum2 -= en[s] + 1;
s++;
}
res += (j - i) * (sum2 - sum1 * ref);
}
assert(res % 3 == 0);
cout << res / 3 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-12;
const double pi = acos(-1);
struct Point {
long long x, y;
Point(long long x = 0, long long y = 0) : x(x), y(y) {}
Point operator+(const Point &o) const { return Point(x + o.x, y + o.y); }
Point operator-(const Point &o) const { return Point(x - o.x, y - o.y); }
Point operator*(const int k) const { return Point(x * k, y * k); }
void read() { cin >> x >> y; }
bool up() const { return y > 0 || y == 0 && x > 0; }
};
long long det(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }
long long dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
long long vlen(const Point &a) { return a.x * a.x + a.y * a.y; }
bool cmp(const Point &a, const Point &b) {
if (a.up() ^ b.up()) return b.up();
if (det(a, b) != 0) {
return det(a, b) > 0;
}
return vlen(a) < vlen(b);
}
bool in_big_range(const Point &a, const Point &b) {
if (det(a, b) != 0) {
return det(a, b) > 0;
}
if (dot(a, b) < 0) {
return true;
}
return vlen(b) > vlen(a);
}
bool in_small_range(const Point &a, const Point &b) {
if (det(a, b) != 0) {
return det(a, b) > 0;
}
if (dot(a, b) < 0) {
return false;
}
return vlen(b) > vlen(a);
}
int main() {
int n, c, d;
cin >> n >> c >> d;
vector<Point> points(n);
for (int i = 0; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u -= c;
v -= d;
points[i].x = u;
points[i].y = v;
}
sort(points.begin(), points.end(), cmp);
reverse(points.begin(), points.end());
for (int i = 0; i < n; i++) points.push_back(points[i]);
int ptr_big = n, ptr_small = n;
while (ptr_big - 1 > 0 && in_big_range(points[0], points[ptr_big - 1])) {
--ptr_big;
}
while (ptr_small - 1 > 0 &&
in_small_range(points[0], points[ptr_small - 1])) {
--ptr_small;
}
long long ans = 0;
for (int i = n; i < 2 * n; ++i) {
ptr_small = max(ptr_small, i - n + 1);
ptr_big = max(ptr_big, i - n + 1);
while (ptr_small + 1 <= i &&
!in_small_range(points[i], points[ptr_small])) {
++ptr_small;
}
while (ptr_big + 1 <= i && !in_big_range(points[i], points[ptr_big])) {
++ptr_big;
}
long long len_big = i - ptr_big;
long long len_small = i - ptr_small;
ans += len_small * (len_small - 1) / 2;
ans += len_small * (len_big - len_small);
}
ans = n * (n - 1LL) * (n - 2LL) / 6 - ans;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
struct Ratio {
int x, y;
Ratio() {}
Ratio(int x_, int y_) : x(x_), y(y_) {}
bool operator<(const Ratio &that) const {
return (long long)x * that.y < (long long)y * that.x;
}
};
ostream &operator<<(ostream &o, const Ratio &p) {
o << p.x << "/" << p.y;
return o;
}
int signum(int x) { return x < 0 ? -1 : x == 0 ? 0 : 1; }
bool check(pair<int, int> p, pair<int, int> q) {
return (long long)p.first * q.second > (long long)q.first * p.second;
}
int main() {
int n;
int c;
int d;
while (1) {
if (!~scanf("%d%d%d", &n, &c, &d)) break;
vector<pair<Ratio, int> > ratios;
vector<pair<int, int> > pairs(n);
for (int(i) = 0; (i) < (int)(n); ++(i)) {
int rr;
int ww;
scanf("%d%d", &rr, &ww);
int r = rr - c, w = ww - d;
pairs[i] = make_pair((r), (w));
if (w == 0) {
int s = signum(r);
ratios.push_back(make_pair((Ratio(1, 0)), (s)));
} else {
int s = signum(w);
ratios.push_back(make_pair((Ratio(r * s, w * s)), (s)));
}
}
sort((ratios).begin(), (ratios).end());
vector<pair<int, int> > v;
vector<Ratio> w;
for (int i = 0; i < (int)ratios.size();) {
Ratio r = ratios[i].first;
v.push_back(make_pair((0), (0)));
w.push_back(r);
for (; i < (int)ratios.size() && !(r < ratios[i].first); ++i) {
int s = ratios[i].second;
++(s == 1 ? v.back().first : v.back().second);
}
}
long long ans = 0;
long long cnt1[2] = {}, cnt2[2] = {};
for (int(i) = 0; (i) < (int)(v.size()); ++(i)) {
ans += cnt2[0] * v[i].first;
ans += cnt2[1] * v[i].second;
cnt2[0] += cnt1[0] * v[i].second;
cnt2[1] += cnt1[1] * v[i].first;
cnt1[0] += v[i].first;
cnt1[1] += v[i].second;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct stu {
long long w, r;
bool operator<(const stu& o) const {
return (w * o.w < 0) ^ (r * o.w < o.r * w);
}
bool operator!=(const stu& o) const { return w * o.r != r * o.w; }
} s[400100];
int main() {
int p0 = 0, n0 = 0, pc = 0, nc = 0;
long long pt = 0, nt = 0, ans = 0;
int n, c, d, m = 0;
scanf("%d%d%d", &n, &c, &d);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (y == d) {
if (x > c)
p0++;
else
n0++;
} else {
s[m].r = x - c;
s[m].w = y - d;
m++;
}
}
sort(s, s + m);
for (int i = 0, j = 0; i < m; i++) {
if (s[i].w > 0) {
ans += nt;
ans += 1LL * n0 * nc;
} else {
ans += pt;
ans += 1LL * p0 * pc;
}
if (i + 1 < m && s[i] != s[i + 1]) {
int tpc = 0, tnc = 0;
while (j < i + 1) {
if (s[j].w > 0)
tpc++;
else
tnc++;
j++;
}
nt += 1LL * tnc * pc;
pt += 1LL * tpc * nc;
pc += tpc;
nc += tnc;
}
}
printf("%I64d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long double pi = acos((long double)-1);
const long double eps = 1e-18;
int main() {
ios_base::sync_with_stdio(0);
ll n, c, d;
cin >> n >> c >> d;
vector<long double> ANS;
for (int i = 0; i < n; i++) {
ll x, y;
cin >> x >> y;
x -= c;
y -= d;
long double cur = atan2(y, x);
ANS.push_back(cur);
ANS.push_back(cur + 2 * pi);
}
sort(ANS.begin(), ANS.end());
ll ans = (n * (n - 1) * (n - 2)) / 6;
for (ll i = 0, lo = 0, hi = 0; i < n; i++) {
while (ANS[lo] < ANS[i] + pi - eps) lo++;
while (ANS[hi + 1] < ANS[i] + pi + eps) hi++;
ans -= ((hi - i) * (hi - i - 1)) / 2;
ans += ((hi - lo) * (hi - lo + 1)) / 2;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
inline void amax(T &a, const T &b) {
if (a < b) a = b;
}
const complex<long long> I(0, 1);
const long long EPS = 0;
int sign(double x) {
if (x < -EPS) return -1;
if (EPS < x) return 1;
return 0;
}
namespace std {
bool operator<(const complex<long long> &x, const complex<long long> &y) {
return real(x) != real(y) ? real(x) < real(y) : imag(x) < imag(y);
}
} // namespace std
long long cross(const complex<long long> &x, const complex<long long> &y) {
return imag(conj(x) * y);
}
struct CMP_ARG {
bool operator()(const complex<long long> &x,
const complex<long long> &y) const {
return sign(cross(x, y)) == 1;
}
} cmp_arg;
bool cmp_arg_2(const complex<long long> &x, const complex<long long> &y) {
return arg(complex<double>(x.real(), x.imag())) <
arg(complex<double>(y.real(), y.imag()));
}
int N;
long long C_sub, D_sub;
long long R[400011], W[400011];
vector<complex<long long> > U, D;
long long pos, neg;
int main() {
scanf("%d%lld%lld", &N, &C_sub, &D_sub);
for (int i = 0, i_len = (N); i < i_len; ++i) {
scanf("%lld%lld", R + i, W + i);
R[i] -= C_sub;
W[i] -= D_sub;
}
{
vector<double> v;
for (int i = 0, i_len = (N); i < i_len; ++i) {
v.push_back(arg(complex<double>(R[i], W[i])));
}
sort(v.begin(), v.end());
cerr << v.back() << endl;
}
for (int i = 0, i_len = (N); i < i_len; ++i) {
if (W[i] > 0)
U.push_back(complex<long long>(R[i], W[i]));
else if (W[i] < 0)
D.push_back(complex<long long>(R[i], W[i]));
else if (R[i] > 0)
pos++;
else
neg++;
}
sort(U.begin(), U.end(), cmp_arg);
sort(D.begin(), D.end(), cmp_arg);
long long ans = 0;
for (long long i = 0, lo = 0, hi = 0; i < (int)U.size(); i++) {
while (hi < (int)D.size() && cross(U[i], D[hi]) >= 0) hi++;
while (lo < (int)D.size() && cross(U[i], D[lo]) > 0) lo++;
ans += lo * ((long long)D.size() - hi);
ans += neg * ((long long)D.size() - hi);
ans += lo * pos;
}
for (long long i = 0, lo = 0, hi = 0; i < (int)D.size(); i++) {
while (hi < (int)U.size() && cross(D[i], U[hi]) >= 0) hi++;
while (lo < (int)U.size() && cross(D[i], U[lo]) > 0) lo++;
ans += lo * ((long long)U.size() - hi);
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 345678 * 2 + 10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int M = 1e9 + 7;
int dcmp(double first) {
if (fabs(first) < eps) return 0;
return first > 0 ? 1 : -1;
}
struct pp {
double ang;
double first, second;
};
double Cross(pp A, pp B) { return A.first * B.second - A.second * B.first; }
bool operator<(const pp& A, const pp& B) {
if (fabs(A.ang - B.ang) < PI / 2) return dcmp(Cross(A, B)) > 0;
return A.ang < B.ang;
}
pp xp[N], xq[N];
double a[N], b[N];
int num[N], sum[N][2];
int check(int i, int j) {
if (fabs(b[i] - a[j]) < PI / 2) return dcmp(Cross(xp[j], xq[i]));
return dcmp(b[i] - a[j]);
}
int main() {
int n;
double c, d;
scanf("%d %lf %lf", &n, &c, &d);
for (int i = 0; i < n; i++) {
double first, second;
scanf("%lf %lf", &first, &second);
first -= c;
second -= d;
xp[i].ang = atan2(second, first);
xq[i].ang = atan2(-second, -first);
xp[i].first = first;
xp[i].second = second;
xq[i].first = -first;
xq[i].second = -second;
}
sort(xp, xp + n);
sort(xq, xq + n);
for (int i = 0; i < n; i++) {
a[i] = xp[i].ang;
b[i] = xq[i].ang;
}
int n1 = 1;
num[0] = 1;
for (int i = 1; i < n; i++) {
if (fabs(a[i] - a[n1 - 1]) < PI / 2 && !dcmp(Cross(xp[i], xp[n1 - 1])))
num[n1 - 1]++;
else {
num[n1] = 1;
a[n1] = a[i];
xp[n1++] = xp[i];
}
}
for (int i = n1; i < 2 * n1; i++) {
xp[i] = xp[i - n1];
a[i] = a[i - n1] + 2 * PI;
num[i] = num[i - n1];
}
for (int i = n; i < 2 * n; i++) {
b[i] = b[i - n] + 2 * PI;
xq[i] = xq[i - n];
}
for (int i = 0, j = 0; i < 2 * n; i++) {
while (j < 2 * n1 && check(i, j) >= 0) j++;
sum[j][0]++;
}
for (int i = 0, j = 0; i < 2 * n; i++) {
while (j < 2 * n1 && check(i, j) > 0) j++;
sum[j][1]++;
}
for (int i = 1; i < 2 * n1; i++) {
sum[i][0] += sum[i - 1][0];
sum[i][1] += sum[i - 1][1];
}
long long ans = 0, tmp = 0, ds = 0;
for (int i = 0, j = 1; i < n1; i++) {
for (; j < 2 * n1 && dcmp(Cross(xp[i], xp[j])) > 0; j++) {
tmp += num[j] * (sum[j][0] - sum[i][1]);
ds += num[j];
}
ans += tmp * num[i];
if (j > i + 1) {
tmp -= num[i + 1] * (sum[i + 1][0] - sum[i][1]);
ds -= num[i + 1];
tmp += (sum[i][1] - sum[i + 1][1]) * ds;
} else {
tmp = ds = 0;
j = i + 2;
}
}
printf("%lld\n", ans / 3);
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.