Submission #250280

#TimeUsernameProblemLanguageResultExecution timeMemory
250280mode149256Fortune Telling 2 (JOI14_fortune_telling2)C++14
100 / 100
851 ms136312 KiB
/*input 5 1 9 1 8 8 3 7 4 6 4 2 3 */ #include <bits/stdc++.h> using namespace std; namespace my_template { typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<vl> vll; typedef vector<pi> vpi; typedef vector<vpi> vpii; typedef vector<pl> vpl; typedef vector<cd> vcd; typedef vector<pd> vpd; typedef vector<bool> vb; typedef vector<vb> vbb; typedef std::string str; typedef std::vector<str> vs; #define x first #define y second #define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n" const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L; template<typename T> pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); } template<typename T> pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); } template<typename T> T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); } template<typename T> T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); } template<typename T> void print(vector<T> vec, string name = "") { cout << name; for (auto u : vec) cout << u << ' '; cout << '\n'; } } using namespace my_template; const int MOD = 1000000007; const ll INF = std::numeric_limits<ll>::max(); const int MX = 100101; struct FENWICK { vi sk; int N; FENWICK(int n) : N(n) { sk.resize(N + 1, 0); } void add(int i, int val) { for (; i <= N; i += (i) & (-i)) sk[i] += val; } int get(int i) { int ret = 0; for (; i > 0; i -= (i) & (-i)) ret += sk[i]; return ret; } }; struct node { int l, r; int did; node *left = nullptr; node *right = nullptr; node(int a, int b): l(a), r(b), did(-1) { } ~node() { if (left) { delete left; left = nullptr; } if (right) { delete right; right = nullptr; } } inline void add_left() { if (!left) left = new node(l, (l + r) / 2); } inline void add_right() { if (!right) right = new node((l + r) / 2 + 1, r); } void add(int pos, int val) { if (l == r) { did = max(did, val); return; } if (pos <= (l + r) / 2) { add_left(); left->add(pos, val); } else { add_right(); right->add(pos, val); } did = max((left ? left->did : -1), (right ? right->did : -1)); } int get(int a, int b) { if (r < a or b < l) return -1; else if (a <= l and r <= b) return did; else if (r <= (l + r) / 2) { if (right) { delete right; right = nullptr; } return (left ? left->get(a, b) : -1); } else if ((l + r) / 2 + 1 <= l) { return (right ? right->get(a, b) : -1); } else { return max((left ? left->get(a, b) : -1), (right ? right->get(a, b) : -1)); } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, K; cin >> N >> K; vpi sk(N); for (int i = 0; i < N; ++i) { cin >> sk[i].x >> sk[i].y; } node inds(1, (int)1e9); vpi suInd(K); for (int i = 0; i < K; ++i) { int a; cin >> a; inds.add(a, i); suInd[i] = {a, i}; } sort(suInd.rbegin(), suInd.rend()); sort(sk.begin(), sk.end(), [](const pi & a, const pi & b) { return max(a.x, a.y) > max(b.x, b.y); }); FENWICK fen(K); ll ats = 0; int j = 0; for (int i = 0; i < N; ++i) { int a = max(sk[i].x, sk[i].y); int b = min(sk[i].x, sk[i].y); int k = inds.get(b, a - 1); while (j < (int)suInd.size() and suInd[j].x >= a) { fen.add(suInd[j].y + 1, 1); j++; } int kiek; if (k == -1) { kiek = fen.get(K); if (kiek & 1) ats += (ll)sk[i].y; else ats += (ll)sk[i].x; } else { kiek = fen.get(K) - fen.get(k + 1); if (kiek & 1) ats += (ll)b; else ats += (ll)a; } // printf("i = %d, a = %d, b = %d, k = %d, kiek = %d\n", i, a, b, k, kiek); } printf("%lld\n", ats); } /* Look for: * special cases (n=1?) * overflow (ll vs int?) * the exact constraints (multiple sets are too slow for n=10^6 :( ) * array bounds */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...