Submission #918193

#TimeUsernameProblemLanguageResultExecution timeMemory
918193devkudawlaLightning Rod (NOI18_lightningrod)C++17
66 / 100
2072 ms262144 KiB
// AUTHOR->DEV KUDAWLA //---------------------------------------------------- #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // find_by_order(it return an iterator input is a value), order_of_key(input is index) typedef tree<long long, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #define ll int #define vl vector<long long> #define nline cout << "\n" #define n_digit(n) (int)log10(n) + 1 #define msb(n) (int)(log2(n)) + 1 // it is 1 based #define pll pair<ll, ll> #define all(x) x.begin(), x.end() #define ternary(a, b, c) ((a) ? (b) : (c)) #define yesno(a) a ? cout << "YES" : cout << "NO" #define sroot(a) sqrt((long double)a) #define Max(a, b) max((ll)a, (ll)b) #define Min(a, b) min((ll)a, (ll)b) //---------------------------------------------------- template <class T1, class T2> ostream &operator<<(std::ostream &os, pair<T1, T2> &st) { cout << "{ " << st.first << " " << st.second << " }"; return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { int n = v.size(); for (int i = 0; i < n; i++) is >> v[i]; return is; } template <class T> istream &operator>>(istream &is, vector<vector<T>> &v) { int n = v.size(); int m = v[0].size(); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) is >> v[i][j]; return is; } template <class T> ostream &operator<<(std::ostream &os, vector<T> &v) { int n = v.size(); for (int i = 0; i < n; i++) os << v[i] << ((i == n - 1) ? "\n" : " "); return os; } template <class T> ostream &operator<<(std::ostream &os, vector<vector<T>> &v) { int n = v.size(); int m = v[0].size(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) os << v[i][j] << " "; os << "\n"; } return os; } template <class T> ostream &operator<<(std::ostream &os, set<T> &st) { cout << "---------------------------------\n"; for (auto i : st) cout << i << " "; nline; cout << "---------------------------------\n"; return os; } template <class T> ostream &operator<<(std::ostream &os, multiset<T> &st) { cout << "---------------------------------\n"; for (auto i : st) cout << i << " "; nline; cout << "---------------------------------\n"; return os; } template <class T1, class T2> ostream &operator<<(std::ostream &os, map<T1, T2> &st) { cout << "-------------------------------\n"; auto x = st.begin(); while (x != st.end()) { cout << x->first; cout << " -> "; cout << x->second; nline; x++; } cout << "-------------------------------\n"; return os; } template <class T> vector<T> add(vector<T> v1, vector<T> v2) { vector<T> v3 = v1; for (ll i = 0; i < v2.size(); i++) v3.push_back(v2[i]); return v3; } template <int D, typename T> struct Vector : public vector<Vector<D - 1, T>> { static_assert(D >= 1, "Vector dimension must be greater than zero!"); template <typename U, typename... Args> Vector(U n = U(), Args... args) : vector<Vector<D - 1, T>>(n, Vector<D - 1, T>(args...)) {} }; template <typename T> struct Vector<1, T> : public vector<T> { template <typename... Args> Vector(Args... args) : vector<T>(args...) {} }; inline ll power2(ll n) { ll answer = 0; if (n != 0) answer = msb(((ll)n) ^ ((ll)(n - 1))) - 1; return answer; } inline ll indexOf(ordered_multiset &st, ll value) { return st.order_of_key(value); } inline ll valueAt(ordered_multiset &st, ll index) { return *st.find_by_order(index); } inline ll indexOf(ordered_set &st, ll value) { return st.order_of_key(value); } inline ll valueAt(ordered_set &st, ll index) { return *st.find_by_order(index); } template <class T> void Distinct(T &v, bool sorting = true) { if (sorting) sort(begin(v), end(v)); v.resize(unique(begin(v), end(v)) - begin(v)); } //---------------------------------------------------- const ll N1 = 1000000007; const ll N2 = 998244353; //---------------------------------------------------- // MODULAR ARITHMETIC inline ll expo(ll a, ll b, ll mod = INT_MAX) { ll res = 1; while (b > 0) { if (b & 1) res = ((__int128_t)res * a) % mod; a = ((__int128_t)a * a) % mod; b = b >> 1; } return res; } inline ll mminvprime(ll a, ll b) { return expo(a, b - 2, b); } // FOR PRIME inline ll mod_add(ll a, ll b, ll m = N1) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } inline ll mod_mul(ll a, ll b, ll m = N1) { a = a % m; b = b % m; return (((__int128_t)(a * b) % m) + m) % m; } inline ll mod_sub(ll a, ll b, ll m = N1) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } inline ll mod_div(ll a, ll b, ll m = N1) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } // only for prime m ll ncr(ll n, ll r, bool mod_version = false, ll mod = N1) { ll answer = 0; if (n >= r) { r = Min(r, n - r); if (mod_version == true) { ll a = 1; for (ll i = n; i >= n - r + 1; i--) a = mod_mul(a, i, mod); ll b = 1; for (ll i = 1; i <= r; i++) b = mod_mul(b, i, mod); b = mminvprime(b, mod); a = mod_mul(a, b, mod); answer = a; } else { ll a = 1; ll b = 1; for (ll i = n; i >= n - r + 1; i--) { a *= i; b *= (n - i + 1); ll g = __gcd(a, b); a /= g, b /= g; } answer = a / b; } } return answer; } ll factorial(ll n, bool mod_version = false, ll mod = N1) { ll answer = 1; if (mod_version == true) { for (int i = 2; i <= n; i++) answer = mod_mul(answer, i, mod); } else { for (int i = 2; i <= n; i++) answer *= i; } return answer; } bool is_prime(ll a) { if (a == 1) return false; for (ll i = 2; i * i <= a; i++) { if (a % i == 0) return false; } return true; } //---------------------------------------------------- map<ll, ll> prime_factors(ll n, bool debug = false) { map<ll, ll> answer; ll a = n; for (ll i = 2; i * i <= a; i++) while (a % i == 0) answer[i]++, a /= i; if (a > 1) answer[a]++; if (debug) { for (auto i : answer) cout << i.first << " -> " << i.second << "\n"; } return answer; } //---------------------------------------------------- const int n_sieve = (20000008); // O(Nlog(log(N))) // vector<bool> prime_sieve(n_sieve + 1, true); void initialise_sieve(vector<bool> &prime_sieve) { prime_sieve[0] = false; prime_sieve[1] = false; for (ll i = 2; i * i < n_sieve; i++) if (prime_sieve[i] == true) for (ll j = 2; j * i < n_sieve; j++) prime_sieve[j * i] = false; } //---------------------------------------------------- // #define LOCAL_COMPILER #ifdef LOCAL_COMPILER #define dbg(x) \ cerr << "\n" \ << #x << " -> \n"; \ cout << x << "\n"; #endif #ifndef LOCAL_COMPILER #define dbg(x) #endif //---------------------------------------------------- // CODE STARTS HERE const int BUF_SZ = 1 << 15; inline namespace Input { char buf[BUF_SZ]; int pos; int len; char next_char() { if (pos == len) { pos = 0; len = (int)fread(buf, 1, BUF_SZ, stdin); if (!len) { return EOF; } } return buf[pos++]; } int read_int() { int x; char ch; int sgn = 1; while (!isdigit(ch = next_char())) { if (ch == '-') { sgn *= -1; } } x = ch - '0'; while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); } return x * sgn; } } // namespace Input inline namespace Output { char buf[BUF_SZ]; int pos; void flush_out() { fwrite(buf, 1, pos, stdout); pos = 0; } void write_char(char c) { if (pos == BUF_SZ) { flush_out(); } buf[pos++] = c; } void write_int(int x) { static char num_buf[100]; if (x < 0) { write_char('-'); x *= -1; } int len = 0; for (; x >= 10; x /= 10) { num_buf[len++] = (char)('0' + (x % 10)); } write_char((char)('0' + x)); while (len) { write_char(num_buf[--len]); } write_char('\n'); } // auto-flush output when program exits void init_output() { assert(atexit(flush_out) == 0); } } // namespace Output const int sz = 10000010; const int sz1 = 10000010; long long Fenwick[sz] = {0}; long long Fenwick1[sz1] = {0}; unordered_map<ll, ll> D1, D2; // 1 BASED INDEXING // ALWAYS DECLARE FENWICK TREE GLOBALLY struct fenwicktree { int n; vector<ll> v; fenwicktree(vector<ll> A) { v.resize(A.size() + 1, 0); n = A.size(); for (int i = 0; i < A.size(); i++) Add(i + 1, A[i]); } long long Sum(int i) { long long answer = 0; for (; i > 0; i -= (i & (-i))) answer += Fenwick[i]; return answer; } void Add(int i, ll x) { v[i] += x; for (; i <= n; i += (i & (-i))) Fenwick[i] += x; } void Replace(int i, ll x) { ll y = v[i]; v[i] = x; for (; i <= n; i += (i & (-i))) Fenwick[i] += x - y; } long long RangeSum(int l, int r) { return Sum(r) - Sum(l - 1); } }; // 1 BASED INDEXING // ALWAYS DECLARE FENWICK TREE GLOBALLY struct fenwicktree1 { int n; vector<ll> v; fenwicktree1(vector<ll> A) { v.resize(A.size() + 1, 0); n = A.size(); for (int i = 0; i < A.size(); i++) Add(i + 1, A[i]); } long long Sum(int i) { long long answer = 0; for (; i > 0; i -= (i & (-i))) answer += Fenwick1[i]; return answer; } void Add(int i, ll x) { v[i] += x; for (; i <= n; i += (i & (-i))) Fenwick1[i] += x; } void Replace(int i, ll x) { ll y = v[i]; v[i] = x; for (; i <= n; i += (i & (-i))) Fenwick1[i] += x - y; } long long RangeSum(int l, int r) { return Sum(r) - Sum(l - 1); } }; //---------------------------------------------------- void solve(bool testCases = true) { ll T = 1; //->TEST CASES if (testCases) T = read_int(); while (T--) { ll n; n = read_int(); vector<pll> v(n); for (ll i = 0; i < n; i++) { ll a, b; a = read_int(); b = read_int(); v[i] = {b, a}; } sort(all(v)); reverse(all(v)); int c1, c2; { vl a; for (ll i = 0; i < n; i++) a.push_back(v[i].second - v[i].first); Distinct(a); for (ll i = 0; i < a.size(); i++) D1[a[i]] = i + 1; c1 = a.size(); a.clear(); for (ll i = 0; i < n; i++) a.push_back(v[i].second + v[i].first); Distinct(a); c2 = a.size(); for (ll i = 0; i < a.size(); i++) D2[a[i]] = i + 1; } fenwicktree a(vector<ll>(c1 + 5, 0)); fenwicktree1 b(vector<ll>(c2 + 5, 0)); ll answer = 0; for (ll i = 0; i < n; i++) { ll d1 = v[i].second - v[i].first; ll d2 = v[i].second + v[i].first; ll p1 = a.RangeSum(1, D1[d1]); ll p2 = b.RangeSum(D2[d2], c2); if (p1 + p2 - answer > 0) continue; answer++; a.Add(D1[d1], 1); b.Add(D2[d2], 1); } write_int(answer); nline; } //-------------------------------------------- // CODE ENDS HERE } //---------------------------------------------------- int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); init_output(); //------------------------------------------------ // initialise_sieve(prime_sieve); //------------------------------------------------ #ifdef LOCAL_COMPILER std::cout << std::fixed << std::setprecision(25); std::cerr << std::fixed << std::setprecision(10); auto start = std::chrono::high_resolution_clock::now(); #endif solve(false); #ifdef LOCAL_COMPILER auto stop = std::chrono::high_resolution_clock::now(); long double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count(); std::cerr << "Time taken : " << duration / 1e9 << "s" << std::endl; #endif //------------------------------------------------ return 0; } //----------------------------------------------------

Compilation message (stderr)

lightningrod.cpp: In constructor 'fenwicktree::fenwicktree(std::vector<int>)':
lightningrod.cpp:405:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  405 |         for (int i = 0; i < A.size(); i++)
      |                         ~~^~~~~~~~~~
lightningrod.cpp: In constructor 'fenwicktree1::fenwicktree1(std::vector<int>)':
lightningrod.cpp:444:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  444 |         for (int i = 0; i < A.size(); i++)
      |                         ~~^~~~~~~~~~
lightningrod.cpp: In function 'void solve(bool)':
lightningrod.cpp:498:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  498 |             for (ll i = 0; i < a.size(); i++)
      |                            ~~^~~~~~~~~~
lightningrod.cpp:506:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  506 |             for (ll i = 0; i < a.size(); i++)
      |                            ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...