[Codeforces Round #665 (Div. 2)] F. Reverse and Swap (线段树,特殊的标记,交换儿子)
[Codeforces Round #665 (Div. 2)] F. Reverse and Swap (线段树,特殊的标记,交换儿子)
题面:
题意:
给你一个长度为\(2^n\) 的数组,以及有四种操作:
1、给定\((x,k)\),将\(a_x\)换成\(\mathit k\)
2、给定\(\mathit k\) ,翻转每一个区间:\([(i-1) \cdot2^k+1, i \cdot 2^k],i \ge 1\)
3、给定\(\mathit k\),交换区间:$ [(2i−2)⋅2k+1,(2i−1)⋅2k] \ and \ [(2i−1)⋅2k+1,2i⋅2k],(i≥1)$
4、给定\(l,r\) 求数组\(\mathit a\) 在区间 \([l,r]\)的sum和。
思路:
线段树是一个满二叉树,且如果根区间长度是\(2^k\)的话,线段树的每一个节点的区间长度都是\(\text 2\)的次幂,且自顶而下每一层幂次方减一。
我们先以数组\(\mathit a\) 建立线段树,
那么我们可以发现,
操作2就是将线段树中所有区间长度小于等于\(2^k\)的节点的左右儿子交换一下。
操作3就是将线段树中所有区间长度等于\(2^{k+1}\)的节点的左右儿子交换一下。
并且交换操作是可以抵消的,那么我们不妨在每一个节点上开个swap_laze标记,
用二进制状压第\(\mathit i\) 位是否为1代表对应长度为\(2^i\)的区间是否需要交换儿子。
既然是laze标记,我们只需要打在根节点上即可,打标记只需要将那个位异或下即可,这样可以完成抵消的操作。
当单点修改或者区间查询的适合再向下传。
注意:
我们有2种写法,
1种是开2个laze标记,分别代表reverse和swap,向下传的时候reverse需要将其状压的位置右移变化一下,
也可以只开一个swap标记,直接传给儿子即可。
具体看我代码:
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = (1 << 19);
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, q;
int a[maxn << 2];
int lson[maxn << 2];
int rson[maxn << 2];
ll val[maxn << 2];
int reverse_laze[maxn << 2];
int swap_laze[maxn << 2];
void pushup(int rt)
{
val[rt] = val[lson[rt]] + val[rson[rt]];
}
void build(int rt, int l, int r)
{
if (l == r)
{
val[rt] = a[l];
return ;
}
lson[rt] = rt << 1;
rson[rt] = rt << 1 | 1;
int mid = l + r >> 1;
build(lson[rt], l, mid);
build(rson[rt], mid + 1, r);
pushup(rt);
}
void push_down(int rt, int len)
{
if (reverse_laze[rt] & len)
{
if (swap_laze[rt] & len)
{
swap_laze[rt] ^= len;
} else
{
swap(lson[rt], rson[rt]);
}
reverse_laze[rt] ^= len;
reverse_laze[rt] ^= len >> 1;
} else
{
if (swap_laze[rt] & len)
{
swap_laze[rt] ^= len;
swap(lson[rt], rson[rt]);
}
}
reverse_laze[lson[rt]] ^= reverse_laze[rt];
reverse_laze[rson[rt]] ^= reverse_laze[rt];
swap_laze[lson[rt]] ^= swap_laze[rt];
swap_laze[rson[rt]] ^= swap_laze[rt];
swap_laze[rt] = reverse_laze[rt] = 0;
}
void update(int rt, int l, int r, int pos, int w)
{
if (l == r && l == pos)
{
val[rt] = w;
return ;
}
int len = r - l + 1;
push_down(rt, len);
int mid = (l + r) >> 1;
if (pos <= mid)
{
update(lson[rt], l, mid, pos, w);
} else
{
update(rson[rt], mid + 1, r, pos, w);
}
pushup(rt);
}
ll ask(int rt, int tl, int tr, int l, int r)
{
if (tl >= l && tr <= r)
{
return val[rt];
}
int len = tr - tl + 1;
push_down(rt, len);
int mid = (tl + tr) >> 1;
ll res = 0ll;
if (l <= mid)
{
res += ask(lson[rt], tl, mid, l, r);
}
if (r > mid)
{
res += ask(rson[rt], mid + 1, tr, l, r);
}
return res;
}
int main()
{
#if DEBUG_Switch
freopen("C:\\code\\input.txt", "r", stdin);
#endif
//freopen("C:\\code\\output.txt","w",stdout);
n = readint();
q = readint();
int temp = n;
n = 1 << n;
repd(i, 1, n)
{
a[i] = readint();
}
build(1, 1, n);
int op, k, x, l, r;
while (q--)
{
op = readint();
if (op == 1)
{
x = readint();
k = readint();
update(1, 1, n, x, k);
} else if (op == 2)
{
k = readint();
// 另一种写法:
// reverse_laze[1] ^= (1 << k);
repd(i, 1, k)
{
swap_laze[1] ^= (1 << i);
}
} else if (op == 3)
{
k = readint();
k++;
k = min(temp, k);
swap_laze[1] ^= (1 << k);
} else
{
l = readint();
r = readint();
ll res = ask(1, 1, n, l, r);
printf("%lld\n", res );
}
}
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。