Submission #1040814

# Submission time Handle Problem Language Result Execution time Memory
1040814 2024-08-01T09:54:49 Z minhquaanz Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
286 ms 54800 KB
/*Code by TMQ*/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define task "name"
#define ii pair<int, int >
#define fi first
#define se second
#define pb push_back
#define em emplace
#define all(x) (x).begin(), (x).end()
#define sall(x) sort(all(x))
#define rall(x) reverse(all(x))
#define reset(a, x) (memset(a, x, sizeof(a)));
#define testcase int tc; cin >> tc; while(tc--) solve();
ll inline gcd(ll a, ll b) { return !b ? abs(a) : gcd(b, a % b); }
ll inline lcm(ll a, ll b) { return (a / gcd(a, b) * b); }
///============================================================================
#define BIT(i,mask) ((mask >> (i-1)) & 1)
#define DAOBIT(i,mask) ((mask ^ (1<<i-1))) 
#define OFFBIT(i,mask) ((mask & ~(1 << (i - 1))))
#define ONBIT(i,mask) ((mask | (1 << (i - 1)))) 
///============================================================================
const ll  mod = 1000000007;
const ll mod1 = 998244353;
const ll inf = 1000000000000000000 + 10;
const ll nmax = 200002;
using namespace std;

int s[nmax][2];

int a[nmax];

vector<int> t[nmax * 4];

void build(int k, int l, int r) {
    if (l == r) {
        t[k] = { a[l] };
        return;
    }
    int i1 = 0, i2 = 0, id1 = k << 1, id2 = k << 1 | 1, s1, s2, m = (l + r) >> 1;
    build(id1, l, m);
    build(id2, m + 1, r);

    s1 = t[id1].size();
    s2 = t[id2].size();

    while (i1 != s1 && i2 != s2)
        if (t[id1][i1] > t[id2][i2])
            t[k].pb(t[id2][i2++]);
        else
            t[k].pb(t[id1][i1++]);
    while (i1 != s1)
        t[k].pb(t[id1][i1++]);
    while (i2 != s2)
        t[k].pb(t[id2][i2++]);
}

pair<int, int> query(int k, int l, int r, int f, int s) {
    ///cout << '\n' << '\n';
    auto it = lower_bound(all(t[k]), min(f, s));
    if (it == t[k].end())
    {
        ////cerr << t[k].end() - it << '\n';
        return { 0, 0 };
    }

    if (*it >= max(f, s))
    {
        ///cerr << t[k].end() - it << '\n';
        return { 0, t[k].end() - it };
    }

    if (l == r)
        return { 1, 0 };
    int m = (l + r) >> 1;
    auto r2 = query(k << 1 | 1, m + 1, r, f, s);
    if (r2.fi) return r2;
    auto r1 = query(k << 1, l, m, f, s);
    return { r1.fi, r1.se ^ r2.se };
}

ll res;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> s[i][0] >> s[i][1];
    for (int i = 1; i <= k; i++)
        cin >> a[i];

    build(1, 1, k);
    for (int i = 1; i <= n; i++) {
        auto w = query(1, 1, k, s[i][0], s[i][1]);
        ///cerr << w.fi << ' ' << w.se << '\n';
        ///cout << '\n';
        if (w.fi && s[i][0] < s[i][1])
            swap(s[i][0], s[i][1]);
        res += s[i][w.se & 1];
    }
    cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 3 ms 21084 KB Output is correct
9 Correct 3 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 3 ms 21084 KB Output is correct
9 Correct 3 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
14 Correct 11 ms 22804 KB Output is correct
15 Correct 22 ms 24504 KB Output is correct
16 Correct 31 ms 25372 KB Output is correct
17 Correct 43 ms 27968 KB Output is correct
18 Correct 41 ms 27988 KB Output is correct
19 Correct 40 ms 27996 KB Output is correct
20 Correct 45 ms 27956 KB Output is correct
21 Correct 34 ms 28116 KB Output is correct
22 Correct 25 ms 27484 KB Output is correct
23 Correct 25 ms 27404 KB Output is correct
24 Correct 25 ms 27484 KB Output is correct
25 Correct 25 ms 27484 KB Output is correct
26 Correct 26 ms 27736 KB Output is correct
27 Correct 33 ms 27996 KB Output is correct
28 Correct 26 ms 27916 KB Output is correct
29 Correct 40 ms 27996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21084 KB Output is correct
2 Correct 3 ms 21084 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 3 ms 21084 KB Output is correct
9 Correct 3 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21084 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
14 Correct 11 ms 22804 KB Output is correct
15 Correct 22 ms 24504 KB Output is correct
16 Correct 31 ms 25372 KB Output is correct
17 Correct 43 ms 27968 KB Output is correct
18 Correct 41 ms 27988 KB Output is correct
19 Correct 40 ms 27996 KB Output is correct
20 Correct 45 ms 27956 KB Output is correct
21 Correct 34 ms 28116 KB Output is correct
22 Correct 25 ms 27484 KB Output is correct
23 Correct 25 ms 27404 KB Output is correct
24 Correct 25 ms 27484 KB Output is correct
25 Correct 25 ms 27484 KB Output is correct
26 Correct 26 ms 27736 KB Output is correct
27 Correct 33 ms 27996 KB Output is correct
28 Correct 26 ms 27916 KB Output is correct
29 Correct 40 ms 27996 KB Output is correct
30 Correct 77 ms 50632 KB Output is correct
31 Correct 122 ms 51472 KB Output is correct
32 Correct 174 ms 52428 KB Output is correct
33 Correct 267 ms 54728 KB Output is correct
34 Correct 70 ms 50380 KB Output is correct
35 Correct 275 ms 54720 KB Output is correct
36 Correct 273 ms 54784 KB Output is correct
37 Correct 266 ms 54800 KB Output is correct
38 Correct 251 ms 54728 KB Output is correct
39 Correct 281 ms 54728 KB Output is correct
40 Correct 194 ms 54476 KB Output is correct
41 Correct 264 ms 54728 KB Output is correct
42 Correct 286 ms 54732 KB Output is correct
43 Correct 124 ms 53964 KB Output is correct
44 Correct 120 ms 53964 KB Output is correct
45 Correct 120 ms 53960 KB Output is correct
46 Correct 123 ms 52936 KB Output is correct
47 Correct 136 ms 52936 KB Output is correct
48 Correct 146 ms 54732 KB Output is correct
49 Correct 129 ms 54720 KB Output is correct