Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <set>
- #include <array>
- #include <stack>
- #include <queue>
- #include <random>
- #include <numeric>
- #include <functional>
- #include <chrono>
- #include <utility>
- #include <iomanip>
- #include <assert.h>
- using namespace std;
- void dbg_out() { cerr << endl; }
- template<typename Head, typename... Tail>
- void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
- #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
- #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
- #define rng_seed(x) mt19937 rng(x)
- #define all(x) (x).begin(), (x).end()
- #define sz(x) (int) (x).size()
- // #define int long long
- const int MXN = 1e5 + 5, INF = 1e9 + 5;
- int N, Q;
- int A[3 * MXN], tree_pos[3 * MXN], tree_cnt[3 * MXN][33];
- int l_free, r_free;
- void modify_cnt(int pos, int val, int delta) {
- int bits_new = __builtin_popcount(val);
- int bits_cur = __builtin_popcount(A[pos]);
- for (int i = pos; i <= 3 * N; i += i & -i) {
- tree_cnt[i][bits_new]++;
- tree_cnt[i][bits_cur] -= (A[pos] > -1);
- tree_pos[i] += delta;
- }
- }
- void shift(int idx) {
- modify_cnt(idx, 0, -1);
- if (A[idx] & 1) {
- modify_cnt(r_free++, A[idx], 1);
- A[r_free - 1] = A[idx];
- } else {
- modify_cnt(l_free--, A[idx], 1);
- A[l_free + 1] = A[idx];
- }
- A[idx] = -1;
- }
- int get_kth(int k) {
- int pos = 0, jump = 1;
- while (jump * 2 <= 3 * N)
- jump *= 2;
- while (jump) {
- if (pos + jump <= 3 * N && tree_pos[pos + jump] < k) {
- k -= tree_pos[pos + jump];
- pos += jump;
- }
- jump /= 2;
- }
- return pos + 1;
- }
- int query(int pos, int bits) {
- int res = 0;
- for (int i = pos; i > 0; i -= i & -i)
- res += tree_cnt[i][bits];
- return res;
- }
- int query(int l, int r, int bits) {
- return query(r, bits) - query(l - 1, bits);
- }
- void solve() {
- cin >> N >> Q;
- for (int i = 0; i <= 3 * N; i++) {
- A[i] = -1;
- tree_pos[i] = 0;
- for (int j = 0; j <= 32; j++)
- tree_cnt[i][j] = 0;
- }
- l_free = N, r_free = 2 * N + 1;
- for (int i = N + 1; i <= 2 * N; i++) {
- int val;
- cin >> val;
- modify_cnt(i, val, 1);
- A[i] = val;
- }
- while (Q--) {
- int type;
- cin >> type;
- if (type == 1) {
- int X;
- cin >> X;
- int pos = get_kth(X);
- shift(pos);
- } else if (type == 2) {
- int X, P;
- cin >> X >> P;
- int pos = get_kth(X);
- modify_cnt(pos, P, 0);
- A[pos] = P;
- } else {
- int L, R, K;
- cin >> L >> R >> K;
- L = get_kth(L), R = get_kth(R);
- cout << query(L, R, K) << "\n";
- }
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int TC = 1;
- cin >> TC;
- while (TC--) solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement