Submission #299511

# Submission time Handle Problem Language Result Execution time Memory
299511 2020-09-15T05:41:55 Z E869120 Mixture (BOI20_mixture) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

__int128 gcd(__int128 a, __int128 b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

// ------------------------------ Fraction Class ----------------------------
struct Fraction {
	__int128 r1, r2;
};

Fraction operator+(const Fraction &a1, const Fraction &a2) {
	__int128 v1 = a1.r1 * a2.r2 + a1.r2 * a2.r1;
	__int128 v2 = a1.r2 * a2.r2;
	__int128 vv = abs(gcd(v1, v2));
	return Fraction{v1 / vv, v2 / vv};
}

Fraction operator-(const Fraction &a1, const Fraction &a2) {
	__int128 v1 = a1.r1 * a2.r2 - a1.r2 * a2.r1;
	__int128 v2 = a1.r2 * a2.r2;
	__int128 vv = abs(gcd(v1, v2));
	return Fraction{v1 / vv, v2 / vv};
}

Fraction operator*(const Fraction &a1, const Fraction &a2) {
	__int128 v1 = a1.r1 * a2.r1;
	__int128 v2 = a1.r2 * a2.r2;
	__int128 vv = abs(gcd(v1, v2));
	return Fraction{v1 / vv, v2 / vv};
}

bool operator<(const Fraction &a1, const Fraction &a2) {
	__int128 v1 = a1.r1 * a2.r2 - a1.r2 * a2.r1;
	if (v1 < 0) return true;
	return false;
}

bool operator>(const Fraction &a1, const Fraction &a2) {
	__int128 v1 = a1.r1 * a2.r2 - a1.r2 * a2.r1;
	if (v1 > 0) return true;
	return false;
}

bool operator==(const Fraction &a1, const Fraction &a2) {
	__int128 v1 = a1.r1 * a2.r2 - a1.r2 * a2.r1;
	if (v1 == 0) return true;
	return false;
}

Fraction seikika(Fraction a1) {
	__int128 vv = abs(gcd(a1.r1, a1.r2));
	return Fraction{a1.r1 / vv, a1.r2 / vv};
}

// --------------------------------- Point Class --------------------------
struct Point {
	Fraction px, py;
};

bool operator==(const Point &a1, const Point &a2) {
	if (a1.px == a2.px && a1.py == a2.py) return true;
	return false;
}

bool operator<(const Point &a1, const Point &a2) {
	if (a1.px < a2.px) return true;
	if (a1.px > a2.px) return false;
	if (a1.py < a2.py) return true;
	return false;
}

Point operator+(const Point &a1, const Point &a2) {
	return Point{a1.px + a2.px, a1.py + a2.py};
}

Point operator-(const Point &a1, const Point &a2) {
	return Point{a1.px - a2.px, a1.py - a2.py};
}

Fraction dot(Point a1, Point a2) {
	return (a1.px * a2.px) + (a1.py * a2.py);
}

Fraction crs(Point a1, Point a2) {
	Fraction t1 = (a1.px * a2.py);
	Fraction t2 = (a1.py * a2.px);
	return t1 - t2;
}

// ----------------------------------- Variables ---------------------------
Fraction ZERO = Fraction{0LL, 1LL};
long long SX, SY, SZ;
long long Q;
long long D1[1 << 18], D2[1 << 18], D3[1 << 18];
long long DD[1 << 18];
long long idx[1 << 18];
char C[1 << 18];

// Geometry Part
int N;
Point BASE;
Point H[1 << 18];
vector<pair<Point, int>> G;

// Maeshori
bool used[5009];
bool r1[5009];
bool r2[5009][5009];

bool solve1() {
	vector<int> vec;
	for (int i = 0; i < N; i++) { if (used[i] == true) vec.push_back(i); }
	for (int i = 0; i < (int)vec.size(); i++) {
		if (r1[vec[i]] == true) return true;
	}
	return false;
}

bool solve2() {
	vector<int> vec;
	for (int i = 0; i < N; i++) { if (used[i] == true) vec.push_back(i); }
	for (int i = 0; i < (int)vec.size(); i++) {
		for (int j = i + 1; j < (int)vec.size(); j++) {
			if (r2[vec[i]][vec[j]] == true) return true;
		}
	}
	return false;
}

bool solve3() {
	vector<int> vec;
	for (int i = 0; i < N; i++) { if (used[i] == true) vec.push_back(i); }
	if (vec.size() <= 2) return false;
	
	vector<Point> G1;
	G1.push_back(H[vec[0]]);
	G1.push_back(H[vec[1]]);
	for (int i = 2; i < (int)vec.size(); i++) {
		while (G1.size() >= 2) {
			Point H1 = G1[G1.size() - 2];
			Point H2 = G1[G1.size() - 1];
			if (crs(H1 - H[vec[i]], H2 - H[vec[i]]) < ZERO) break;
			else G1.pop_back();
		}
		G1.push_back(H[vec[i]]);
	}
	
	vector<Point> G2;
	G2.push_back(H[vec[0]]);
	G2.push_back(H[vec[1]]);
	for (int i = 2; i < (int)vec.size(); i++) {
		while (G2.size() >= 2) {
			Point H1 = G2[G2.size() - 2];
			Point H2 = G2[G2.size() - 1];
			if (crs(H1 - H[vec[i]], H2 - H[vec[i]]) > ZERO) break;
			else G2.pop_back();
		}
		G2.push_back(H[vec[i]]);
	}
	
	vector<Point> I = G1;
	for (int i = (int)G2.size() - 2; i >= 1; i--) I.push_back(G2[i]);
	
	int cnt1 = 0, cnt2 = 0, cnt3 = 0;
	for (int i = 0; i < (int)I.size(); i++) {
		int n1 = i;
		int n2 = (i + 1) % I.size();
		Fraction I2 = crs(I[n1] - BASE, I[n2] - BASE);
		if (I2 > ZERO) cnt1 += 1;
		if (I2 < ZERO) cnt2 += 1;
		if (I2 == ZERO) cnt3 += 1;
	}
	if ((cnt1 == 0 || cnt2 == 0) && cnt3 == 0) return true;
	return false;
}

int main() {
	// Step #1. Input
	cin >> SX >> SY >> SZ;
	cin >> Q;
	for (int i = 1; i <= Q; i++) {
		cin >> C[i];
		if (C[i] == 'A') {
			cin >> D1[i] >> D2[i] >> D3[i];
			Fraction V1 = seikika(Fraction{D1[i], D1[i] + D2[i] + D3[i]});
			Fraction V2 = seikika(Fraction{D2[i], D1[i] + D2[i] + D3[i]});
			G.push_back(make_pair(Point{V1, V2}, N));
			N += 1;
		}
		if (C[i] == 'R') {
			cin >> D1[i];
			D1[i] -= 1;
		}
	}
	sort(G.begin(), G.end());
	
	// Step #2. Index
	int cnts = 0;
	for (int i = 0; i < N; i++) idx[G[i].second] = i;
	for (int i = 1; i <= Q; i++) {
		if (C[i] == 'A') { DD[i] = idx[cnts]; cnts += 1; }
		if (C[i] == 'R') { D1[i] = idx[D1[i]]; }
	}
	for (int i = 0; i < N; i++) H[i] = G[i].first;
	
	// Step #3. Maeshori
	BASE.px = seikika(Fraction{SX, SX + SY + SZ});
	BASE.py = seikika(Fraction{SY, SX + SY + SZ});
	for (int i = 0; i < N; i++) {
		if (BASE == H[i]) { r1[i] = true; }
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			Point v1 = H[i] - BASE;
			Point v2 = H[j] - BASE;
			if (crs(v1, v2) == ZERO && dot(v1, v2) < ZERO) { r2[i][j] = true; }
		}
	}
	
	// Step #4. Answer Query
	for (int i = 1; i <= Q; i++) {
		if (C[i] == 'A') used[DD[i]] = true;
		if (C[i] == 'R') used[D1[i]] = false;
		bool f1 = solve1();
		bool f2 = solve2();
		bool f3 = solve3();
		if (f1 == true) cout << "1" << endl;
		else if (f2 == true) cout << "2" << endl;
		else if (f3 == true) cout << "3" << endl;
		else cout << "0" << endl;
	}
	return 0;
}

Compilation message

Mixture.cpp: In function 'Fraction operator+(const Fraction&, const Fraction&)':
Mixture.cpp:17:31: error: call of overloaded 'abs(__int128)' is ambiguous
   17 |  __int128 vv = abs(gcd(v1, v2));
      |                               ^
In file included from /usr/include/c++/9/bits/std_abs.h:38,
                 from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/stdlib.h:774:12: note: candidate: 'int abs(int)'
  774 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/c++/9/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
Mixture.cpp: In function 'Fraction operator-(const Fraction&, const Fraction&)':
Mixture.cpp:24:31: error: call of overloaded 'abs(__int128)' is ambiguous
   24 |  __int128 vv = abs(gcd(v1, v2));
      |                               ^
In file included from /usr/include/c++/9/bits/std_abs.h:38,
                 from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/stdlib.h:774:12: note: candidate: 'int abs(int)'
  774 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/c++/9/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
Mixture.cpp: In function 'Fraction operator*(const Fraction&, const Fraction&)':
Mixture.cpp:31:31: error: call of overloaded 'abs(__int128)' is ambiguous
   31 |  __int128 vv = abs(gcd(v1, v2));
      |                               ^
In file included from /usr/include/c++/9/bits/std_abs.h:38,
                 from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/stdlib.h:774:12: note: candidate: 'int abs(int)'
  774 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/c++/9/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
Mixture.cpp: In function 'Fraction seikika(Fraction)':
Mixture.cpp:54:37: error: call of overloaded 'abs(__int128)' is ambiguous
   54 |  __int128 vv = abs(gcd(a1.r1, a1.r2));
      |                                     ^
In file included from /usr/include/c++/9/bits/std_abs.h:38,
                 from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/stdlib.h:774:12: note: candidate: 'int abs(int)'
  774 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/9/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:41,
                 from Mixture.cpp:1:
/usr/include/c++/9/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/9/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~