Task: | Xor sum |
Sender: | paulschulte |
Submission time: | 2024-09-23 16:21:12 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.40 s | details |
Code
// ~/.vim/cpp_template.cpp #include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <string> #define REP(i,a,b) for (int i = a; i < b; i++) #define ll long long // Type Aliases for 1D and 2D vectors with initialization #define vi(n, val) vector<int>(n, val) // 1D vector of ints with size n, initialized to val #define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val #define ll long long #define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints (n x m), initialized to val #define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val using namespace std; template <typename T> void pV(const std::vector<T>& vec, const std::string& label = "Vector") { std::cout << label << ": [ "; for (const auto& elem : vec) { std::cout << elem << " "; } std::cout << "]" << std::endl; } using namespace std; void dfs(int s, vector<bool> *visited, vector<int> (*adj)[]) { if ((*visited)[s]) return; (*visited)[s] = true; // process node s for (auto u: (*adj)[s]) { dfs(u, visited, adj); } } /* vector<int> adj[N]; vector<bool> visited(N, false); int u, v; for(int i = 0; i < M;i++){ cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } */ int qxor(int a, int b, ll n, vector<int>* tree) { a += n; b += n; int s = 0; while (a <= b) { //cout << a << ' ' << b << endl; if (a%2 == 1) s = (*tree)[a++] ^ s; if (b%2 == 0) s = (*tree)[b--] ^ s; a /= 2; b /= 2; } return s; } void change(int k, int u, ll n, vector<int>* tree) { k += n; (*tree)[k] = u; for (k /= 2; k >= 1; k /= 2) { (*tree)[k] = min((*tree)[2*k], (*tree)[2*k+1]); } } int main() { ios::sync_with_stdio(0); cin.tie(0); // Your code starts here int n, q; cin >> n >> q; ll n_2 = pow(2, ceil(log(n)/log(2))); vector<int> v = vi(2*n_2+1, 1e9); for(ll i = n_2; i < n_2+n; i++){ cin >> v[i]; } for(ll i = n_2-1; i > 0; i--){ v[i] = v[2*i] ^ v[2*i+1]; } REP(i, 0, q){ int a, b; cin >> a >> b; ++a;++b; if(a == b){ cout << v[n_2-2+a] << endl; continue; } cout << qxor(a, b, n_2-2, &v) << endl; } return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
8 36 7 6 4 6 2 9 4 8 1 1 1 2 1 3 ... |
correct output |
---|
7 1 5 3 1 ... |
user output |
---|
7 1 5 3 1 ... |
Test 2
Verdict: ACCEPTED
input |
---|
200000 200000 921726510 307633388 992247073 ... |
correct output |
---|
834756431 130379787 403037296 308618218 784778243 ... |
user output |
---|
834756431 130379787 403037296 308618218 784778243 ... Truncated |