【洛谷】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. 牛客小白月赛11 Rinne Loves Xor

    题目链接:https://ac.nowcoder.com/acm/contest/370/I code: #include<bits/stdc++.h> using namespace s ...

  2. [Luogu] 跑路

    https://www.luogu.org/problemnew/show/P1613 Floyd判断是否一步到达 将一步到达的连变跑Floyd #include <iostream> # ...

  3. openstack 本地导入镜像.

    网络很慢,直接本地传. openstack image create "Fedora30" --file Fedora-Cloud-Base-30-1.2.x86_64.qcow2 ...

  4. python常用模块介绍

    关于if __name__ == "__main__": 若执行文件为bin,调用文件为cal: 若在执行文件bin中执行print(__name__) 输出:__main__ 当 ...

  5. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  6. CF1195B

    CF1195B 题意: 有一个盒子,每次可以做两个操作: 1.每次吃掉一块蛋糕 2.每次放入比上一次放入数多1的蛋糕 当盒子为空时,只能执行第 $ 2 $ 个操作.第 $ 1 $ 次操作永远是放入一个 ...

  7. CISCO实验记录六:EIGRP路由协议

    一.要求 1.查看当前路由协议 2.清空路由设置 3.使用EIGRP协议创建路由 4.查看EIGRP的邻居表 5.关闭自动汇总 6.使用手工汇总 二.实现 1.查看当前路由协议 #show ip pr ...

  8. 添加sql距离现在多久以前时间条件

    UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(add_time)<=25200 其中now()是现在时间 add_time是其他时间点  25200:是秒,现在和ad ...

  9. 网络1911、1912 C语言第0次作业批改总结

    网络1911.1912 C语言第0次作业批改总结 题目:C博客作业00--我的第一篇博客 一.评分规则 总分10分,每个问题都务必回答,分值都在问题后面 抄袭 - 0分 博客作业格式不规范,没有用Ma ...

  10. GA算法及参数对结果的影响

    1.遗传算法简介 遗传算法是一种基于自然选择和群体遗传机理的搜索算法,它模拟了自然选择和自然遗传过程中的繁殖.杂交和突变现象.再利用遗传算法求解问题时,问题的每一个可能解都被编码成一个“染色体”,即个 ...