Submission #542760

# Submission time Handle Problem Language Result Execution time Memory
542760 2022-03-27T22:24:10 Z Olympia Nekameleoni (COCI15_nekameleoni) C++17
42 / 140
3000 ms 142400 KB
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <limits.h>

using namespace std;
class Node {
public:
    map<int64_t,int> left;
    map<int64_t,int> right;
    int64_t bor = 0;
    int tot = 0;
    int length = 0;
    bool okay = true;
    void print () {
        cout << "TOTAL: " << tot << '\n';
        cout << "LEFT\n";
        for (auto& p: left) {
            cout << p.first << " " << p.second << '\n';
        }
        cout << "RIGHT\n";
        for (auto& p: right) {
            cout << p.first << " " << p.second << '\n';
        }
        cout << '\n';
    }
};
vector<int64_t> powr;
Node generate (int64_t x) {
    Node ans;
    ans.left[powr[x]] = 1;
    ans.left[0] = 0;
    ans.right[powr[x]] = 1;
    ans.right[0] = 0;
    ans.tot = 1;
    ans.bor = powr[x];
    ans.length = 1;
    return ans;
}
int chmin (int &x, int y) {
    if (x == 0) return (x = y);
    if (y == 0) return x;
    return (x = min(x, y));
}
Node merge (Node x, Node y) {
    if (!x.okay) return y;
    if (!y.okay) return x;
    Node ans;
    ans.left = x.left;
    ans.right = y.right;
    for (auto& p: y.left) {
        ans.left[p.first | x.bor] = chmin(ans.left[p.first | x.bor], p.second + x.length);
    }
    for (auto& p: x.right) {
        ans.right[p.first | y.bor] = chmin(ans.right[p.first | y.bor], p.second + y.length);
    }
    ans.bor = x.bor | y.bor;
    ans.tot = INT_MAX;
    ans.length = x.length + y.length;
    if (x.bor == ans.bor) {
        ans.tot = min(ans.tot, x.tot);
    }
    if (y.bor == ans.bor) {
        ans.tot = min(ans.tot, y.tot);
    }
    vector<pair<int,int64_t>> xr; for (auto& p: x.right) xr.push_back({p.second, p.first});
    vector<pair<int,int64_t>> yr; for (auto& p: y.left) yr.push_back({p.second, p.first});
    sort(xr.begin(), xr.end());
    for (auto& p: xr) {
        for (auto& q: yr) {
            if ((p.second | q.second) == ans.bor) {
                ans.tot = min(ans.tot, p.first + q.first);
                break;
            }
        }
    }
    ans.left[0] = ans.right[0] = 0;
    return ans;
}
template<class T>
class SegmentTree {
public:

    SegmentTree (int N) {
        N = (1 << ((int)floor(log2(N - 1)) + 1));
        this->N = N;
        val.assign(2 * N, ID);
        ID.okay = false;
    }

    void update (int x, T y) {
        x += N - 1;
        val[x] = y;
        while (x != 0) {
            x = (x - 1)/2;
            val[x].left.clear(), val[x].right.clear();
            val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
        }
    }

    void build (int dum, vector<int>& arr, int tl, int tr) {
        if (tl == tr) {
            if (tl < arr.size()) val[dum] = generate(arr[tl]);
            else val[dum].okay = false;
            return;
        }
        build(2 * dum + 1, arr, tl, (tl + tr)/2);
        build(2 * dum + 2, arr, (tl + tr)/2 + 1, tr);
        val[dum] = merge(val[2 * dum + 1], val[2 * dum + 2]);
        //val[dum].print();
    }

    void build (vector<int> arr) {
        build(0,  arr, 0, N - 1);
    }

    T query (int ind, const int l, const int r, int tl, int tr) {
        if (tl >= l && tr <= r) {
            return val[ind];
        }
        if (tr < l || tl > r) {
            return ID;
        }
        return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
    }

    T query (int l, int r) {
        return query(0, l, r, 0, N - 1);
    }
    vector<T> val;
private:
    T ID;
    int N;
};
int main() {
    //freopen("balancing.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, K, M;
    cin >> N >> K >> M;
    SegmentTree<Node> st(N);
    vector<int> arr(N);
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
    powr = {1};
    for (int i = 0; i < 51; i++) {
        powr.push_back(powr.back() * 2);
    }
    st.build(arr);
    while (M--) {
        int t;
        cin >> t;
        if (t == 2) {
            //cout << st.val[0].bor << " " << powr[K + 1] - 2 << '\n';
            if (st.val[0].bor == powr[K + 1] - 2) {
                cout << st.val[0].tot << '\n';
            } else {
                cout << "-1\n";
            }
        } else {
            int p, v; cin >> p >> v; p--;
            st.update(p, generate(v));
        }
    }
}

Compilation message

nekameleoni.cpp: In instantiation of 'void SegmentTree<T>::build(int, std::vector<int>&, int, int) [with T = Node]':
nekameleoni.cpp:121:14:   required from 'void SegmentTree<T>::build(std::vector<int>) [with T = Node]'
nekameleoni.cpp:157:17:   required from here
nekameleoni.cpp:110:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |             if (tl < arr.size()) val[dum] = generate(arr[tl]);
      |                 ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 93 ms 4392 KB Output is correct
2 Correct 68 ms 3792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 273 ms 6736 KB Output is correct
2 Correct 295 ms 6356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 528 ms 7836 KB Output is correct
2 Correct 588 ms 7644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3081 ms 30812 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3104 ms 76236 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3094 ms 61788 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3076 ms 88480 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3088 ms 83232 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3097 ms 142400 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3092 ms 142380 KB Time limit exceeded
2 Halted 0 ms 0 KB -