【洛谷】P4883 mzf的考验

最近忽然放弃治疗开始随机跳题了

感觉还行

就是必须吸氧感觉有点糟糕。。。

这题翻转和求和都是平衡树基本操作,那个异或可以通过维护树中\(2\)进制下第\(2^{i}\)位的\(1\)的个数,即可\(O(\log d)\)快速维护

当敲板子玩了

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
mt19937 rnd(20020328);
struct node {
int pri;
int lc,rc,siz;
int cnt[20],d;
int64 sum,val;
bool rev;
}tr[MAXN];
int N,M,rt;
int a[MAXN];
#define lc(u) tr[u].lc
#define rc(u) tr[u].rc
#define sum(u) tr[u].sum
void update(int u) {
for(int j = 0 ; j < 20 ; ++j) {
tr[u].cnt[j] = ((tr[u].val >> j) & 1) + tr[lc(u)].cnt[j] + tr[rc(u)].cnt[j];
}
tr[u].sum = tr[u].val + tr[lc(u)].sum + tr[rc(u)].sum;
tr[u].siz = 1 + tr[lc(u)].siz + tr[rc(u)].siz;
}
void xorit(int u,int d) {
tr[u].val ^= d;tr[u].d ^= d;
for(int j = 0 ; j < 20 ; ++j) {
if(d >> j & 1) {
tr[u].sum -= 1LL * tr[u].cnt[j] * (1 << j);
tr[u].sum += 1LL * (tr[u].siz - tr[u].cnt[j]) * (1 << j);
tr[u].cnt[j] = tr[u].siz - tr[u].cnt[j];
}
} }
void Rev(int u) {
tr[u].rev ^= 1;
swap(lc(u),rc(u));
}
void pushdown(int u) {
if(tr[u].d) {
if(lc(u)) xorit(lc(u),tr[u].d);
if(rc(u)) xorit(rc(u),tr[u].d);
tr[u].d = 0;
}
if(tr[u].rev) {
if(lc(u)) Rev(lc(u));
if(rc(u)) Rev(rc(u));
tr[u].rev = 0;
}
}
int Merge(int u,int v) {
if(!u) return v;
if(!v) return u;
pushdown(u);pushdown(v);
if(tr[u].pri > tr[v].pri) {
tr[u].rc = Merge(tr[u].rc,v);
update(u);
return u;
}
else {
tr[v].lc = Merge(u,tr[v].lc);
update(v);
return v;
}
}
void Split(int u,int &L,int &R,int s) {
if(u == 0) {L = R = 0;return;}
int t = tr[lc(u)].siz + 1;
pushdown(u);
if(s >= t) {
L = u;
Split(tr[u].rc,tr[L].rc,R,s - t);
update(L);
}
else {
R = u;
Split(tr[u].lc,L,tr[R].lc,s);
update(R);
}
}
void Modify(int l,int r,int d) {
int L,R,p;
Split(rt,L,R,l - 1);
Split(R,p,R,r - l + 1);
xorit(p,d);
rt = Merge(L,p);rt = Merge(rt,R);
}
void Rev_Range(int l,int r) {
int L,R,p;
Split(rt,L,R,l - 1);
Split(R,p,R,r - l + 1);
Rev(p);
rt = Merge(L,p);rt = Merge(rt,R);
}
int64 Query(int l,int r) {
int L,R,p;
Split(rt,L,R,l - 1);
Split(R,p,R,r - l + 1);
int64 res = tr[p].sum;
rt = Merge(L,p);rt = Merge(rt,R);
return res;
} void Solve() {
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
tr[i].pri = rnd();tr[i].sum = a[i];tr[i].val = a[i];
for(int j = 0 ; j < 20 ; ++j) {
tr[i].cnt[j] = (a[i] >> j) & 1;
}
tr[i].siz = 1;
rt = Merge(rt,i);
}
int opt,l,r,d;
for(int i = 1 ; i <= M ; ++i) {
read(opt);read(l);read(r);
if(opt == 1) Rev_Range(l,r);
else if(opt == 2) {read(d);Modify(l,r,d);}
else {out(Query(l,r));enter;}
}
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif Solve();
}

【洛谷】P4883 mzf的考验的更多相关文章

  1. 洛谷 P4883 mzf的考验 解题报告

    P4883 mzf的考验 题目背景 \(mzf\)立志要成为一个豪杰,当然,他也是一个\(OIer\). 他希望自己除了会\(OI\)之外还会各种东西,比如心理学.吉他.把妹等等. 为了让自己有更大的 ...

  2. P4883 mzf的考验[平衡树]

    P4883 mzf的考验 维护一种数据结构 支持区间翻转 区间异或 区间求和- 显然 fhq treap 区间异或显然是拆位 ~~然后复杂度*20~~ 第一次先遍历一下整棵树 pushup 一下 就可 ...

  3. 洛谷 P4240 - 毒瘤之神的考验(数论+复杂度平衡)

    洛谷题面传送门 先扯些别的. 2021 年 7 月的某一天,我和 ycx 对话: tzc:你做过哪些名字里带"毒瘤"的题目,我做过一道名副其实的毒瘤题就叫毒瘤,是个虚树+dp yc ...

  4. 洛谷P4240 毒瘤之神的考验 【莫比乌斯反演 + 分块打表】

    题目链接 洛谷P4240 题解 式子不难推,分块打表真的没想到 首先考虑如何拆开\(\varphi(ij)\) 考虑公式 \[\varphi(ij) = ij\prod\limits_{p | ij} ...

  5. 烦神的斐波那契&&洛谷-1306-斐波那契公约数

    传送门 洛谷1306传送门 -------------------------------------------------------------------------------------- ...

  6. 洛谷P5274 优化题(ccj)

    洛谷P5274 优化题(ccj) 题目背景 CCJCCJ 在前往参加 Universe \ OIUniverse OI 的途中... 题目描述 有一个神犇 CCJCCJ,他在前往参加 Universe ...

  7. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  8. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  9. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. Gradle 发布 Jar 到 Archiva 时提示不能 Overwriting released artifacts is not allowed

    系统提示错误信息: Received status code 409 from server: Overwriting released artifacts is not allowed. 这是在 A ...

  2. [Luogu] 最大收益

    题面:https://www.luogu.org/problemnew/show/P2647 题解:https://www.zybuluo.com/wsndy-xx/note/1142685

  3. 2019 Multi-University Training Contest 10

    目录 Contest Info Solutions C - Valentine's Day D - Play Games with Rounddog E - Welcome Party G - Clo ...

  4. 2018 Nowcoder Multi-University Training Contest 5

    Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ ...

  5. idea 2018注册码(激活码)

    最近做一个项目,用idea 社区版的   但是缺少了好多功能 无奈只能用专业版的,但是需要注册激活  下面是我的注册方法 1.打开了idea  会提示让激活  选择Licensse server 2. ...

  6. 最全的tcpdump使用详解

    简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...

  7. kotlin标准委托之可观察属性

    所谓可观察属性就是当属性变化时可以拦截其变化,实现观察属性值变化的委托函数是Delegates.observable.该函数接受二个参数,第一个是初始化值,第2个属性值变化事件的响应器.每次我们向属性 ...

  8. kotlin中访问封闭作用内的变量

    在java中,匿名对象访问封闭作用域内的变量,需要用final 声明变量在java8中,如果只是使用封闭作用域内的变量,该变量并不需要使用final,但是一旦修改值,就需要使用final 来声明变量. ...

  9. Linux通过AIO进行异步读文件

    下面列出源代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <a ...

  10. OpenStack Manila发展动态系列--Austin峰会

    1 Manila Mitaka版本概述 在Austin峰会上介绍到,Manila Mitaka发布版本Driver个数达到了18个, M版本新加入14家公司(中国公司继华为之后又有99cloud等公司 ...