Submission #57089

# Submission time Handle Problem Language Result Execution time Memory
57089 2018-07-13T23:46:25 Z Benq Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
2687 ms 92052 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
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<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 200001;

template<class T, int SZ> struct Seg {
    T seg[2*SZ], MN = -MOD;
    
    Seg() {
        F0R(i,2*SZ) seg[i] = -MOD;
    }
    
    T comb(T a, T b) { return max(a,b); } // easily change this to min or max
    
    void upd(int p, T value) {  // set value at position p
        for (seg[p += SZ] = value; p > 1; p >>= 1)
            seg[p>>1] = comb(seg[(p|1)^1],seg[p|1]); // non-commutative operations
    }
    
    void build() {
        F0Rd(i,SZ) seg[i] = comb(seg[2*i],seg[2*i+1]);
    }
    
    T query(int l, int r) {  // sum on interval [l, r]
        T res1 = MN, res2 = MN; r++;
        for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) {
            if (l&1) res1 = comb(res1,seg[l++]);
            if (r&1) res2 = comb(seg[--r],res2);
        }
        return comb(res1,res2);
    }
};

Seg<int,1<<20> S;
int N,K;
vpi p;
map<int,int> m;
vi rm, t;

void input() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> K; t.resize(K); p.resize(N);
    F0R(i,N) {
        cin >> p[i].f >> p[i].s;
        m[p[i].f] = m[p[i].s] = 0;
    }
    F0R(i,K) {
        cin >> t[i];
        m[t[i]] = 0;
    }
    for (auto& a: m) {
        a.s = sz(rm);
        rm.pb(a.f);
    }
    F0R(i,N) {
        p[i].f = m[p[i].f];
        p[i].s = m[p[i].s];
    }
    F0R(i,K) {
        t[i] = m[t[i]];
        S.upd(t[i],i);
    }
}

int main() {
    input();
    Tree<int> tmp;
    vpi T; F0R(i,K) T.pb({t[i],i}); sort(all(T));
    sort(all(p),[](pi a, pi b) { return max(a.f,a.s) > max(b.f,b.s); });
    
    ll ans = 0;
    for (auto a: p) {
        while (sz(T) && T.back().f >= max(a.f,a.s)) tmp.insert(T.back().s), T.pop_back();
        int x = S.query(min(a.f,a.s),max(a.f,a.s)-1);
        int flip = sz(tmp)-tmp.order_of_key(x);
        a.f = rm[a.f], a.s = rm[a.s];
        if (x == -MOD) {
            if (flip&1) ans += a.s;
            else ans += a.f;
        } else {
            if (flip&1) ans += min(a.f,a.s);
            else ans += max(a.f,a.s);
        }
        // cout << a.f << " " << a.s << " " << x << " " << flip << " " << ans << "\n";
    }
    cout << ans;
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8700 KB Output is correct
2 Correct 10 ms 8824 KB Output is correct
3 Correct 11 ms 9096 KB Output is correct
4 Correct 11 ms 9176 KB Output is correct
5 Correct 10 ms 9220 KB Output is correct
6 Correct 12 ms 9380 KB Output is correct
7 Correct 15 ms 9380 KB Output is correct
8 Correct 10 ms 9380 KB Output is correct
9 Correct 10 ms 9380 KB Output is correct
10 Correct 10 ms 9380 KB Output is correct
11 Correct 10 ms 9380 KB Output is correct
12 Correct 11 ms 9544 KB Output is correct
13 Correct 14 ms 9608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8700 KB Output is correct
2 Correct 10 ms 8824 KB Output is correct
3 Correct 11 ms 9096 KB Output is correct
4 Correct 11 ms 9176 KB Output is correct
5 Correct 10 ms 9220 KB Output is correct
6 Correct 12 ms 9380 KB Output is correct
7 Correct 15 ms 9380 KB Output is correct
8 Correct 10 ms 9380 KB Output is correct
9 Correct 10 ms 9380 KB Output is correct
10 Correct 10 ms 9380 KB Output is correct
11 Correct 10 ms 9380 KB Output is correct
12 Correct 11 ms 9544 KB Output is correct
13 Correct 14 ms 9608 KB Output is correct
14 Correct 45 ms 11760 KB Output is correct
15 Correct 96 ms 14568 KB Output is correct
16 Correct 170 ms 17704 KB Output is correct
17 Correct 213 ms 21044 KB Output is correct
18 Correct 271 ms 22104 KB Output is correct
19 Correct 211 ms 22688 KB Output is correct
20 Correct 215 ms 24488 KB Output is correct
21 Correct 215 ms 24488 KB Output is correct
22 Correct 133 ms 24488 KB Output is correct
23 Correct 123 ms 24488 KB Output is correct
24 Correct 110 ms 24488 KB Output is correct
25 Correct 137 ms 25672 KB Output is correct
26 Correct 138 ms 27024 KB Output is correct
27 Correct 155 ms 28712 KB Output is correct
28 Correct 150 ms 29732 KB Output is correct
29 Correct 187 ms 32000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8700 KB Output is correct
2 Correct 10 ms 8824 KB Output is correct
3 Correct 11 ms 9096 KB Output is correct
4 Correct 11 ms 9176 KB Output is correct
5 Correct 10 ms 9220 KB Output is correct
6 Correct 12 ms 9380 KB Output is correct
7 Correct 15 ms 9380 KB Output is correct
8 Correct 10 ms 9380 KB Output is correct
9 Correct 10 ms 9380 KB Output is correct
10 Correct 10 ms 9380 KB Output is correct
11 Correct 10 ms 9380 KB Output is correct
12 Correct 11 ms 9544 KB Output is correct
13 Correct 14 ms 9608 KB Output is correct
14 Correct 45 ms 11760 KB Output is correct
15 Correct 96 ms 14568 KB Output is correct
16 Correct 170 ms 17704 KB Output is correct
17 Correct 213 ms 21044 KB Output is correct
18 Correct 271 ms 22104 KB Output is correct
19 Correct 211 ms 22688 KB Output is correct
20 Correct 215 ms 24488 KB Output is correct
21 Correct 215 ms 24488 KB Output is correct
22 Correct 133 ms 24488 KB Output is correct
23 Correct 123 ms 24488 KB Output is correct
24 Correct 110 ms 24488 KB Output is correct
25 Correct 137 ms 25672 KB Output is correct
26 Correct 138 ms 27024 KB Output is correct
27 Correct 155 ms 28712 KB Output is correct
28 Correct 150 ms 29732 KB Output is correct
29 Correct 187 ms 32000 KB Output is correct
30 Correct 946 ms 49032 KB Output is correct
31 Correct 1225 ms 56608 KB Output is correct
32 Correct 1640 ms 66020 KB Output is correct
33 Correct 2528 ms 82880 KB Output is correct
34 Correct 633 ms 82880 KB Output is correct
35 Correct 2626 ms 90572 KB Output is correct
36 Correct 2554 ms 92052 KB Output is correct
37 Correct 2687 ms 92052 KB Output is correct
38 Correct 2517 ms 92052 KB Output is correct
39 Correct 2485 ms 92052 KB Output is correct
40 Correct 2247 ms 92052 KB Output is correct
41 Correct 2579 ms 92052 KB Output is correct
42 Correct 2669 ms 92052 KB Output is correct
43 Correct 1176 ms 92052 KB Output is correct
44 Correct 1202 ms 92052 KB Output is correct
45 Correct 1194 ms 92052 KB Output is correct
46 Correct 1224 ms 92052 KB Output is correct
47 Correct 1215 ms 92052 KB Output is correct
48 Correct 1685 ms 92052 KB Output is correct
49 Correct 1610 ms 92052 KB Output is correct