Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[問題追加] Count Points in Triangle #1075

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions geo/count_points_in_triangle/checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// https://github.com/MikeMirzayanov/testlib/blob/master/checkers/wcmp.cpp

// The MIT License (MIT)

// Copyright (c) 2015 Mike Mirzayanov

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "testlib.h"

using namespace std;

int main(int argc, char * argv[])
{
setName("compare sequences of tokens");
registerTestlibCmd(argc, argv);

int n = 0;
string j, p;

while (!ans.seekEof() && !ouf.seekEof())
{
n++;

ans.readWordTo(j);
ouf.readWordTo(p);

if (j != p)
quitf(_wa, "%d%s words differ - expected: '%s', found: '%s'", n, englishEnding(n).c_str(), compress(j).c_str(), compress(p).c_str());
}

if (ans.seekEof() && ouf.seekEof())
{
if (n == 1)
quitf(_ok, "\"%s\"", compress(j).c_str());
else
quitf(_ok, "%d tokens", n);
}
else
{
if (ans.seekEof())
quitf(_wa, "Participant output contains extra tokens");
else
quitf(_wa, "Unexpected EOF in the participants output");
}
}
35 changes: 35 additions & 0 deletions geo/count_points_in_triangle/gen/dense.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <cstdio>
#include <vector>

#include "../params.h"
#include "random.h"
using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
int m = M_MAX;
int q = Q_MAX;
printf("%d\n", n);
for (int i=0; i<n; i++) {
long long a = gen.uniform(-12, 12);
long long b = gen.uniform(-12, 12);
printf("%lld %lld\n", a, b);
}
printf("%d\n", m);
for (int i=0; i<m; i++) {
long long a = gen.uniform(-9, 9);
long long b = gen.uniform(-9, 9);
printf("%lld %lld\n", a, b);
}
printf("%d\n", q);
for (int i=0; i<q; i++) {
int a = gen.uniform(0, n-1);
int b = gen.uniform(0, n-1);
int c = gen.uniform(0, n-1);
printf("%d %d %d\n", a, b, c);
}
return 0;
}
17 changes: 17 additions & 0 deletions geo/count_points_in_triangle/gen/example_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
5
-1 -1
-1 2
2 -1
2 2
5 5
5
0 0
0 1
1 0
1 1
1 1
4
0 1 2
3 3 3
0 3 4
1 4 2
35 changes: 35 additions & 0 deletions geo/count_points_in_triangle/gen/max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <cstdio>
#include <vector>

#include "../params.h"
#include "random.h"
using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
int m = M_MAX;
int q = Q_MAX;
printf("%d\n", n);
for (int i=0; i<n; i++) {
long long a = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
long long b = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
printf("%lld %lld\n", a, b);
}
printf("%d\n", m);
for (int i=0; i<m; i++) {
long long a = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
long long b = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
printf("%lld %lld\n", a, b);
}
printf("%d\n", q);
for (int i=0; i<q; i++) {
int a = gen.uniform(0, n-1);
int b = gen.uniform(0, n-1);
int c = gen.uniform(0, n-1);
printf("%d %d %d\n", a, b, c);
}
return 0;
}
42 changes: 42 additions & 0 deletions geo/count_points_in_triangle/gen/middle_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/*

An easier solution ( O( N + M Q ) time ) can pass this testcase.

*/

#include <cstdio>
#include <vector>

#include "../params.h"
#include "random.h"
using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
int m = M_MAX;
int q = min<long long>(Q_MAX, 4000'0000 / m);
printf("%d\n", n);
for (int i=0; i<n; i++) {
long long a = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
long long b = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
printf("%lld %lld\n", a, b);
}
printf("%d\n", m);
for (int i=0; i<m; i++) {
long long a = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
long long b = gen.uniform(-X_AND_Y_ABS_MAX, X_AND_Y_ABS_MAX);
printf("%lld %lld\n", a, b);
}
printf("%d\n", q);
for (int i=0; i<q; i++) {
int a = gen.uniform(0, n-1);
int b = gen.uniform(0, n-1);
int c = gen.uniform(0, n-1);
printf("%d %d %d\n", a, b, c);
}
return 0;
}
22 changes: 22 additions & 0 deletions geo/count_points_in_triangle/hash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"dense_00.in": "036f55e2799522e6368dc78fbef79e2822b4bb70ecc8541702f1fe5cb712a09f",
"dense_00.out": "464ed740e3d66104017ab38cb686f86b9958a1bbc8a8a0fd68496b1047c2075b",
"dense_01.in": "922fc5c5ca06f32d650f7164ba045641fd036b4b0c4725b767c6d4849dff3ef5",
"dense_01.out": "a7e3f7722d143d68eb9642fa64b030e1d46a7726b03dea96735298aea5c1211b",
"example_00.in": "1c1694bc76fbc885dc663f31749118c55f44de16e2c55694e6b716f9f72d409a",
"example_00.out": "49f044318460c78482da423fe4ce239bace57b06a491ad9b3e3a4d0714677955",
"max_random_00.in": "d2a0b5d592650f276a858f20eacc2046aa7ae6393dbe72e61a95305771c2adcc",
"max_random_00.out": "0dc84b413514d605a519a8d1ec04294d6f0625885a0c04fca6d1d2f1eada4eb5",
"max_random_01.in": "247830f28271610bc44722d51ed7fcf68ce5d3cdeae8058db8fa0652db6d10c0",
"max_random_01.out": "4031253445c3116e20f9e268ece2bcb8853cb51dcf8b689afd68076403d3d3ca",
"middle_random_00.in": "ec976e9a05c375faac0df26e48f28e126051196b76a069e9a6a0e41333d4c2d9",
"middle_random_00.out": "f88de1179fdfeafad2f55ee99d9f4e8abc586a3b3516884e77b5014051981a81",
"middle_random_01.in": "840dc205732fc0d926b9b4ea68964c6072351ae10e0708d65d97f8355e276b21",
"middle_random_01.out": "126b89028d49cb542bf6afa7c9b3344373176b700cbc30311078209fe6733509",
"middle_random_02.in": "254c810f18356b5205a6e620dcf2a605a7c8a319be1131122d5501df1317aff2",
"middle_random_02.out": "4411360df1cb335f27ff23481d2e70a7889034bc94cdfde512394590914db6de",
"middle_random_03.in": "b1a7a4d9106d7fd0ed0787941203ce0eb689367e058517a0067db82274bc4778",
"middle_random_03.out": "1a3fdb542543d1d159d4640fd3b2c03af7239ddd26d3e3b6473ca08f408283c7",
"middle_random_04.in": "b3914b4927f1f6ebcefe7336b322806a18f2381d795d59caf8eb5554d97c0993",
"middle_random_04.out": "edd0d6403ec3ba37c3796b689c2a7c0044a9587545428446535e5acfd2f5c7a7"
}
29 changes: 29 additions & 0 deletions geo/count_points_in_triangle/info.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
title = 'Count Points in Triangles'
timelimit = 5.0
forum = "https://github.com/yosupo06/library-checker-problems/issues/908"

[[tests]]
name = "example.in"
number = 1
[[tests]]
name = "middle_random.cpp"
number = 5
[[tests]]
name = "max_random.cpp"
number = 2
[[tests]]
name = "dense.cpp"
number = 2

[[solutions]]
name = "naive.cpp"
allow_tle = true
[[solutions]]
name = "fast.cpp"


[params]
N_MAX = 500
M_MAX = 500
Q_MAX = 1_000_000
X_AND_Y_ABS_MAX = 1_000_000_000
107 changes: 107 additions & 0 deletions geo/count_points_in_triangle/sol/correct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*

O( N^2 M + Q ) time solution

too slow at N,M = 1000

*/

#include "fastio.hpp"
#include <vector>
#include <algorithm>
#include <cassert>

template<class Int = long long, class Int2 = long long>
struct VecI2 {
Int x, y;
VecI2() : x(0), y(0) {}
VecI2(Int _x, Int _y) : x(_x), y(_y) {}
VecI2 operator+(VecI2 r) const { return VecI2(x+r.x, y+r.y); }
VecI2 operator-(VecI2 r) const { return VecI2(x-r.x, y-r.y); }
VecI2 operator-() const { return VecI2(-x, -y); }
Int2 operator*(VecI2 r) const { return Int2(x) * Int2(r.x) + Int2(y) * Int2(r.y); }
Int2 operator^(VecI2 r) const { return Int2(x) * Int2(r.y) - Int2(y) * Int2(r.x); }
static bool compareYX(VecI2 a, VecI2 b){ return a.y < b.y || (!(b.y < a.y) && a.x < b.x); }
static bool compareXY(VecI2 a, VecI2 b){ return a.x < b.x || (!(b.x < a.x) && a.y < b.y); }
};


using i64 = long long;
using namespace std;
using Vec = VecI2<i64>;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

int main(){
using nachia::cin;
using nachia::cout;
int N; cin >> N;
vector<Vec> A(N);
rep(i,N) cin >> A[i].x >> A[i].y;
int M; cin >> M;
vector<Vec> B(M);
rep(i,M) cin >> B[i].x >> B[i].y;

auto pointL = vector<int>(N); // bx < Ax
auto pointM = vector<int>(N); // bx = Ax
rep(i,N) rep(j,M) if(A[i].y == B[j].y){
if(B[j].x < A[i].x) pointL[i]++;
if(B[j].x == A[i].x) pointM[i]++;
}

auto edgeL = vector<vector<int>>(N, vector<int>(N)); // bx < lerp(Ax, Bx)
auto edgeM = vector<vector<int>>(N, vector<int>(N)); // bx = lerp(Ax, Bx)
rep(a,N) rep(b,N) if(A[a].y < A[b].y){
rep(i,M) if(A[a].y < B[i].y && B[i].y < A[b].y){
auto det = (A[a] - A[b]) ^ (B[i] - A[b]);
if(det < 0) edgeL[a][b]++;
if(det == 0) edgeM[a][b]++;
}
}

int Q; cin >> Q;
rep(qi, Q){
int a,b,c; cin >> a >> b >> c;
if(Vec::compareYX(A[b], A[a])) swap(a, b);
if(Vec::compareYX(A[c], A[b])) swap(b, c);
if(Vec::compareYX(A[b], A[a])) swap(a, b);
// c ^y
// b |
// a +---> x

// c
// nega |
// v posi
// a
auto det = (A[a] - A[c]) ^ (A[b] - A[c]);
int ans = 0;
if(det != 0){
if(A[a].y == A[b].y){ // A[a].x < A[b].x
// c
// a b
ans = edgeL[b][c] - (edgeL[a][c] + edgeM[a][c]);
} else if(A[b].y == A[c].y){ // A[b].x < A[c].x
// b c
// a
ans = edgeL[a][c] - (edgeL[a][b] + edgeM[a][b]);
} else if(det < 0){
// c
// b
// a
ans += edgeL[a][c];
ans -= edgeL[b][c] + edgeM[b][c];
ans -= edgeL[a][b] + edgeM[a][b];
ans -= pointL[b] + pointM[b];
} else {
// c
// b
// a
ans += edgeL[a][b];
ans += edgeL[b][c];
ans += pointL[b];
ans -= edgeL[a][c] + edgeM[a][c];
}
}
cout << ans << '\n';
}
return 0;
}
Loading