Submission #1095308

# Submission time Handle Problem Language Result Execution time Memory
1095308 2024-10-01T20:41:30 Z Triseedot Gift Exchange (JOI24_ho_t4) C++17
50 / 100
87 ms 30004 KB
// In honor of El Psy Congroo

//#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;

// variables
using ld = long double;
using ll = long long;
using ull = unsigned long long;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// bitwise operations
#define cnt_bit(n) __builtin_popcountll(n)
#define low_bit(n) ((n) & (-(n)))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_bit(n, i) ((n) | (1ll << (i)))
#define reset_bit(n, i) ((n) & ~(1ll << (i)))
#define flip_bit(n, i) ((n) ^ (1ll << (i)))
// math
#define sqr(n) ((n) * (n))
int log2_floor(ull n) {
    return n ? __builtin_clzll(1) - __builtin_clzll(n) : -1;
}
// vector
#define len(x) (int) x.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for(auto& el : v) { 
        is >> el;
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < len(v); i++) {
        if (i) os << ' ';
        os << v[i];
    }
    return os;
}
template<class... Args>
auto create(size_t n, Args&&... args) {
    if constexpr(sizeof...(args) == 1) {
        return vector(n, args...);
    }
    else {
        return vector(n, create(args...));
    }
}
template<typename T>
void remove_dups(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
// random
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

//////solution start//////

struct FenwickTree {
    vector<ll> tree;
    int n;

    void increase(int i, int val) {
        for (i = i + 1; i <= n; i += low_bit(i)) {
            tree[i] += val;
        }
    }

    void build(const vector<int>& a) {
        n = len(a);
        tree.assign(n + 1, 0);
        for (int i = 0; i < n; i++) {
            increase(i, a[i]);
        }
    }

    ll get(int r) {
        ll res = 0;
        for (int i = r; i > 0; i -= low_bit(i)) {
            res += tree[i];
        }
        return res;
    }

    ll get(int l, int r) {
        return get(r) - get(l);
    }

    int lower_bound(ll val) {
        int l = 0;
        for (int i = log2_floor(n) + 1; i >= 0; i--) {
            int r = l + (1 << i);
            if (r <= n && val > tree[r]) {
                val -= tree[r];
                l = r;
            }
        }
        return l;
    }
};

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
        a[i]--, b[i]--;
    }
    int m;
    cin >> m;

    vector<optional<int>> l(n), r(n);
    set<pair<int, int>> curr;
    for (int i = 0; i < n; i++) {
        while (true) {
            auto it = curr.lower_bound({b[i], -1});
            if (it == curr.end() || it->first > a[i]) break;
            int j = it->second;
            if (!r[j].has_value() || r[j] > i) r[j] = i;
            if (!l[i].has_value() || l[i] < j) l[i] = j;
            curr.erase(it);
        }
        curr.insert({a[i], i});
    }
    curr.clear();
    for (int i = n - 1; i >= 0; i--) {
        while (true) {
            auto it = curr.lower_bound({b[i], -1});
            if (it == curr.end() || it->first > a[i]) break;
            int j = it->second;
            if (!r[i].has_value() || r[i] > j) r[i] = j;
            if (!l[j].has_value() || l[j] < i) l[j] = i;
            curr.erase(it);
        }
        curr.insert({a[i], i});
    }

    struct query {
        int l, r, i;
    };
    vector<bool> ans(n);
    vector<vector<query>> q(n);
    for (int i = 0; i < m; i++) {
        query qi;
        cin >> qi.l >> qi.r;
        qi.l--, qi.r--;
        qi.i = i;
        q[qi.r].push_back(qi);
    }

    FenwickTree tree;
    tree.build(vector(n + 1, 0));
    auto add = [&] (int l, int r) {
        tree.increase(l, 1);
        tree.increase(r + 1, -1);
    };
    auto ers = [&] (int l, int r) {
        tree.increase(l, -1);
        tree.increase(r + 1, 1);
    };
    auto get = [&] (int i) {
        return tree.get(i + 1);
    };
    vector<vector<pair<int, int>>> to_ers(n);
    for (int i = 0; i < n; i++) {
        for (auto [l_, r_] : to_ers[i]) {
            ers(l_, r_);
        }
        int l_, r_ = i;
        if (!l[i].has_value()) l_ = 0;
        else l_ = *l[i] + 1;
        add(l_, r_);
        if (r[i].has_value()) to_ers[*r[i]].emplace_back(l_, r_);
        for (auto qi : q[i]) {
            if (get(qi.l) == 0) ans[qi.i] = true;
            else ans[qi.i] = false;
        }
    }

    for (int i = 0; i < m; i++) {
        if (ans[i]) cout << "Yes\n";
        else cout << "No\n";
    }
}

//////solution end//////
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int tt = 1;
    //cin >> tt;
    for (int i = 1; i <= tt; i++) {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 36 ms 11976 KB Output is correct
5 Correct 38 ms 11980 KB Output is correct
6 Correct 36 ms 11976 KB Output is correct
7 Correct 45 ms 12072 KB Output is correct
8 Correct 35 ms 11976 KB Output is correct
9 Correct 35 ms 11980 KB Output is correct
10 Correct 35 ms 11880 KB Output is correct
11 Correct 36 ms 11980 KB Output is correct
12 Correct 35 ms 11880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 36 ms 11976 KB Output is correct
38 Correct 38 ms 11980 KB Output is correct
39 Correct 36 ms 11976 KB Output is correct
40 Correct 45 ms 12072 KB Output is correct
41 Correct 35 ms 11976 KB Output is correct
42 Correct 35 ms 11980 KB Output is correct
43 Correct 35 ms 11880 KB Output is correct
44 Correct 36 ms 11980 KB Output is correct
45 Correct 35 ms 11880 KB Output is correct
46 Correct 27 ms 9004 KB Output is correct
47 Correct 36 ms 11980 KB Output is correct
48 Correct 26 ms 8756 KB Output is correct
49 Correct 42 ms 11976 KB Output is correct
50 Correct 52 ms 10764 KB Output is correct
51 Correct 52 ms 10952 KB Output is correct
52 Correct 37 ms 8240 KB Output is correct
53 Correct 50 ms 10952 KB Output is correct
54 Correct 44 ms 11468 KB Output is correct
55 Correct 43 ms 11468 KB Output is correct
56 Correct 87 ms 14176 KB Output is correct
57 Correct 31 ms 12492 KB Output is correct
58 Correct 50 ms 12108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Runtime error 64 ms 30004 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Runtime error 64 ms 30004 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 36 ms 11976 KB Output is correct
38 Correct 38 ms 11980 KB Output is correct
39 Correct 36 ms 11976 KB Output is correct
40 Correct 45 ms 12072 KB Output is correct
41 Correct 35 ms 11976 KB Output is correct
42 Correct 35 ms 11980 KB Output is correct
43 Correct 35 ms 11880 KB Output is correct
44 Correct 36 ms 11980 KB Output is correct
45 Correct 35 ms 11880 KB Output is correct
46 Correct 27 ms 9004 KB Output is correct
47 Correct 36 ms 11980 KB Output is correct
48 Correct 26 ms 8756 KB Output is correct
49 Correct 42 ms 11976 KB Output is correct
50 Correct 52 ms 10764 KB Output is correct
51 Correct 52 ms 10952 KB Output is correct
52 Correct 37 ms 8240 KB Output is correct
53 Correct 50 ms 10952 KB Output is correct
54 Correct 44 ms 11468 KB Output is correct
55 Correct 43 ms 11468 KB Output is correct
56 Correct 87 ms 14176 KB Output is correct
57 Correct 31 ms 12492 KB Output is correct
58 Correct 50 ms 12108 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Runtime error 64 ms 30004 KB Execution killed with signal 11
63 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 36 ms 11976 KB Output is correct
38 Correct 38 ms 11980 KB Output is correct
39 Correct 36 ms 11976 KB Output is correct
40 Correct 45 ms 12072 KB Output is correct
41 Correct 35 ms 11976 KB Output is correct
42 Correct 35 ms 11980 KB Output is correct
43 Correct 35 ms 11880 KB Output is correct
44 Correct 36 ms 11980 KB Output is correct
45 Correct 35 ms 11880 KB Output is correct
46 Correct 27 ms 9004 KB Output is correct
47 Correct 36 ms 11980 KB Output is correct
48 Correct 26 ms 8756 KB Output is correct
49 Correct 42 ms 11976 KB Output is correct
50 Correct 52 ms 10764 KB Output is correct
51 Correct 52 ms 10952 KB Output is correct
52 Correct 37 ms 8240 KB Output is correct
53 Correct 50 ms 10952 KB Output is correct
54 Correct 44 ms 11468 KB Output is correct
55 Correct 43 ms 11468 KB Output is correct
56 Correct 87 ms 14176 KB Output is correct
57 Correct 31 ms 12492 KB Output is correct
58 Correct 50 ms 12108 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Runtime error 64 ms 30004 KB Execution killed with signal 11
63 Halted 0 ms 0 KB -