【线段树】【P2572】【SCOI2010】序列操作
Description
lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作:
0 a b 把[a, b]区间内的所有数全变成0
1 a b 把[a, b]区间内的所有数全变成1
2 a b 把[a,b]区间内的所有数全部取反,也就是说把所有的0变成1,把所有的1变成0
3 a b 询问[a, b]区间内总共有多少个1
4 a b 询问[a, b]区间内最多有多少个连续的1
对于每一种询问操作,lxhgww都需要给出回答,聪明的程序员们,你们能帮助他吗?
Input
输入数据第一行包括2个数,n和m,分别表示序列的长度和操作数目
第二行包括n个数,表示序列的初始状态
接下来m行,每行3个数,op, a, b,(0<=op<=4,0<=a<=b<n)表示对于区间[a, b]执行标号为op的操作
Output
对于每一个询问操作,输出一行,包括1个数,表示其对应的答案
Hint
对于30%的数据,1<=n, m<=1000
对于100%的数据,1<=n, m<=100000
Solution
测试题……睿智线段树……头一次写多标记线段树写了7k然后爆10分……最后发现标记传错了……
考虑一个区间维护的信息显然有1的个数,连续1长度,左连续1,右连续1。0也同理。标记打上区间置零,赋一,取反。
考虑pushdown操作。
发现这三个标记只能同时存在一个,所以下传的顺序是无所谓的。但是需要考虑打标记的时候标记的重叠问题。具体的,在make_tag的时候,如果打赋1标记,若存在取反标记,则改为打赋0标记。同时把其余标记置为false。区间置0同理。
考虑区间取反的时候,如果存在全部赋值标记,则将赋值标记取反,不打取反标记,否则将取反标记取反。因为把取反写成赋true爆10分了……
大概就这样
我好菜啊……写了7k只有10分
Code
#include <cstdio>
#include <iostream>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while ( x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 100010;
const int maxt = 200010;
int n, m;
int MU[maxn];
struct Tag {
bool zero,one,bk;
inline void clear() {
zero = one = bk = false;
}
};
struct Info {
int s0, s1, len0, len1, ll0, ll1, rl0, rl1;
inline void buildit(int _v) {
if (_v == 1) {
len1 = ll1 = rl1 = s1 = 1;
} else {
len0 = ll0 = rl0 = s0 = 1;
}
}
inline void clear() {
s0 = s1 = len0 = len1 = ll0 = ll1 = rl0 = rl1 = 0;
}
inline Info operator+(const Info &_others) const {
Info _ret;
_ret.clear();
_ret.s0 = this->s0 + _others.s0;
_ret.s1 = this->s1 + _others.s1;
_ret.len0 = std::max(std::max(this->len0,_others.len0), this->rl0 + _others.ll0);
_ret.len1 = std::max(std::max(this->len1,_others.len1), this->rl1 + _others.ll1);
_ret.ll0 = this->ll0 + (!this->s1) * (_others.ll0);
_ret.ll1 = this->ll1 + (!this->s0) * (_others.ll1);
_ret.rl0 = _others.rl0 + (!_others.s1) * (this->rl0);
_ret.rl1 = _others.rl1 + (!_others.s0) * (this->rl1);
return _ret;
}
inline void reset(ci k) {
if (k == 0) {
this->s0 = this->ll0 = this->len0 = this->rl0 = this->s0 + this->s1;
this->s1 = this->len1 = this->ll1 = this->rl1 = 0;
} else if (k == 1) {
this->s1 = this->ll1 = this->len1 = this->rl1 = this->s0 + this->s1;
this->s0 = this->len0 = this->ll0 = this->rl0 = 0;
} else {
std::swap(this->s0,this->s1);
std::swap(this->ll0,this->ll1);
std::swap(this->len0,this->len1);
std::swap(this->rl0,this->rl1);
}
}
};
struct Tree {
Tree *ls, *rs;
int l, r;
Tag tg;
Info v;
inline void pushup() {
this->v.clear();
if((this->ls) && (!this->rs)) this->v = this->ls->v;
else if((this->rs) && (!this->ls)) this->v = this->rs->v;
else this->v = this->ls->v + this->rs->v;
}
inline void maketag(ci k) {
if(k == 0) {
this->tg.zero = true;
this->v.reset(0);
this->tg.one = this->tg.bk = false;
} else if(k == 1) {
this->tg.one = true;
this->v.reset(1);
this->tg.zero = this->tg.bk = false;
} else {
if(this->tg.one) {
this->v.reset(0);
std::swap(this->tg.one,this->tg.zero);
} else if(this->tg.zero) {
this->v.reset(1);
std::swap(this->tg.one,this->tg.zero);
} else {
this->v.reset(2);
this->tg.bk ^= 1;
}
}
}
inline void pushdown() {
if (this->tg.zero) {
if(this->ls) this->ls->maketag(0);
if(this->rs) this->rs->maketag(0);
} else if (this->tg.one) {
if(this->ls) this->ls->maketag(1);
if(this->rs) this->rs->maketag(1);
} else if(this->tg.bk) {
if(this->ls) this->ls->maketag(2);
if(this->rs) this->rs->maketag(2);
}
this->tg.clear();
}
};
Tree *pool[maxt], qwq[maxt], *rot;
int top;
struct Ret {
int l,r,ans;
bool isall;
inline void clear() {
l = r = ans = isall = 0;
}
};
void buildpool();
void buildroot();
void build(Tree*, ci, ci);
void update(Tree*, ci, ci, ci);
int ask3(Tree*, ci, ci);
Ret ask4(Tree*, ci, ci);
int main() {
freopen("data.in", "r", stdin);
qr(n); qr(m);
for (rg int i = 1; i <= n; ++i) qr(MU[i]);
buildpool();
buildroot();
build(rot, 1, n);
int a, b, c;
while (m--) {
a = b = c = 0;
qr(a); qr(b); qr(c);
++b;++c;
switch(a) {
case 0: {
update(rot, b, c, 0);
break;
}
case 1: {
update(rot, b, c, 1);
break;
}
case 2: {
update(rot, b, c, 2);
break;
}
case 3: {
qw(ask3(rot, b, c), '\n', true);
break;
}
case 4: {
qw(ask4(rot, b, c).ans, '\n', true);
break;
}
}
}
}
void buildpool() {
for (rg int i = 0; i < maxt; ++i) pool[i] = qwq+i;
top = maxt - 1;
}
void buildroot() {
rot = pool[top--];
rot->l = 1; rot->r = n;
}
void build(Tree *u, ci l, ci r) {
if(l == r) {u->v.buildit(MU[l]); return;}
int mid = (l + r) >> 1;
if (l <= mid) {
u->ls = pool[top--];
u->ls->l = l; u->ls->r = mid;
build(u->ls, l, mid);
}
if(mid < r) {
u->rs = pool[top--];
u->rs->l = mid+1; u->rs->r = r;
build(u->rs, mid+1, r);
}
u->pushup();
}
void update(Tree *u, ci l, ci r, ci v) {
u->pushdown();
if((u->l >= l) && (u->r <= r)) {
u->maketag(v);return;
}
if((u->l > r) || (u->r < l)) return;
if(u->ls) update(u->ls, l, r, v);
if(u->rs) update(u->rs, l, r, v);
u->pushup();
}
int ask3(Tree *u, ci l, ci r) {
u->pushdown();
if((u->l >= l) && (u->r <= r)) return u->v.s1;
else if((u->l > r) || (u->r < l)) return 0;
int _ret = 0;
if(u->ls) _ret = ask3(u->ls, l ,r);
if(u->rs) _ret += ask3(u->rs, l, r);
return _ret;
}
Ret ask4(Tree *u, ci l, ci r) {
u->pushdown();
if((u->l >= l) && (u->r <= r)) {
return (Ret){u->v.ll1, u->v.rl1, u->v.len1, u->v.s0 == 0};
}
if((u->l > r) || (u->r < l)) {
return (Ret){0, 0, 0, 0};
}
Ret rl,rr,ret;
rl.clear();rr.clear();ret.clear();
if(u->ls) rl = ask4(u->ls, l, r);
if(u->rs) rr = ask4(u->rs, l, r);
ret.l = rl.l + rl.isall * rr.l;
ret.r = rr.r + rr.isall * rl.r;
ret.ans = std::max(std::max(rl.ans, rr.ans), rl.r + rr.l);
return ret;
}
Summary
在多标记下传时,考虑下传顺序间的影响,以及下传新标记时,旧标记对新标记的影响。
另外比赛时如果正解写挂一定要及时写暴力……
【线段树】【P2572】【SCOI2010】序列操作的更多相关文章
- 【题解】Luogu P2572 [SCOI2010]序列操作
原题传送门:P2572 [SCOI2010]序列操作 这题好弱智啊 裸的珂朵莉树 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 操作1:把区间内所有数推平成0,珂朵莉树基本操作 ...
- P2572 [SCOI2010]序列操作
对自己 & \(RNG\) : 骄兵必败 \(lpl\)加油! P2572 [SCOI2010]序列操作 题目描述 lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要 ...
- Luogu P2572 [SCOI2010]序列操作 线段树。。
咕咕了...于是借鉴了小粉兔的做法ORZ... 其实就是维护最大子段和的线段树,但上面又多了一些操作....QWQ 维护8个信息:1/0的个数(sum),左/右边起1/0的最长长度(ls,rs),整段 ...
- 洛谷$P2572\ [SCOI2010]$ 序列操作 线段树/珂朵莉树
正解:线段树/珂朵莉树 解题报告: 传送门$w$ 本来是想写线段树的,,,然后神仙$tt$跟我港可以用珂朵莉所以决定顺便学下珂朵莉趴$QwQ$ 还是先写线段树做法$QwQ$? 操作一二三四都很$eas ...
- 洛谷P2572 [SCOI2010]序列操作(珂朵莉树)
传送门 珂朵莉树是个吼东西啊 这题线段树代码4k起步……珂朵莉树只要2k…… 虽然因为这题数据不随机所以珂朵莉树的复杂度实际上是错的…… 然而能过就行对不对…… (不过要是到时候noip我还真不敢打… ...
- 洛谷 P2572 [SCOI2010]序列操作
题意简述 维护一个序列,支持如下操作 把[a, b]区间内的所有数全变成0 把[a, b]区间内的所有数全变成1 把[a,b]区间内所有的0变成1,所有的1变成0 询问[a, b]区间内总共有多少个1 ...
- 洛谷P2572 [SCOI2010]序列操作
线段树 pushdown写的很浪~ #include<cstdio> #include<cstdlib> #include<algorithm> #include& ...
- 洛谷P2572 [SCOI2010]序列操作(ODT)
题解 题意 题目链接 Sol ODT板子题..... // luogu-judger-enable-o2 #include<bits/stdc++.h> #define LL long l ...
- BZOJ 1858: [Scoi2010]序列操作( 线段树 )
略恶心的线段树...不过只要弄清楚了AC应该不难.... ---------------------------------------------------------------- #inclu ...
- BZOJ_1858_[Scoi2010]序列操作_线段树
BZOJ_1858_[Scoi2010]序列操作_线段树 Description lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询 ...
随机推荐
- Linux 安装Redis<集群版>(使用Mac远程访问)
阅读本文需要先阅读安装Redis<准备> 一 架构细节 所有的redis节点彼此互联(PING-PONG机制) 内部使用二进制协议优化传输速度和带宽 节点的fail是通过集群中超过半数的节 ...
- 一、初识 Django
一.引子 Django最初设计用于具有快速开发需求的新闻类站点,目的是要实现简单快捷的网站开发! 从好的方面来看,Web 开发激动人心且富于创造性:从另一面来看,它却是份繁琐而令人生厌的工作.通过减少 ...
- 使用performance进行前端性能监控
该文章仅作为自己的总结 1.performance.timing对象 navigationStart:当前浏览器窗口的前一个网页关闭,发生unload事件时的Unix毫秒时间戳.如果没有前一个网页,则 ...
- SICP读书笔记 1.2
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- MongoDB开启权限认证
MongoDB默认安装完后,如果在配置文件中没有加上auth = true,是没有用户权限认证的,这样对于一个数据库来说是相对不安全的,尤其是在外网的情况下. 接下来是配置权限的过程: //切入到 ...
- Python异常(基础) except
为什么要异常处理机制:在程序调用层数较深时,向主调函数传递错误信息需要层层return 返回比较麻烦,用异常处理机制可以较简单的传送错误信息 什么是错误 错误是指由于逻辑或语法等导致一个程序已无法正常 ...
- 在PHP中,是以分好结束一条语句的吗
在PHP中,是以分号结束一条语句的,这个和C语言类似. 但是,有一条例外,对于PHP结束tag之前的语句,是可以不写分号的: <?php if ($a == $b) { echo "R ...
- c# 简单日志记录
FileStream fs = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "log.txt",Fi ...
- java 中的 i=i++
记得大学刚开始学C语言时,老师就说:自增有两种形式,分别是i++和++i,i++表示的是先赋值后加1,++i是先加1后赋值,这样理解了很多年也没出现问题,直到遇到如下代码,我才怀疑我的理解是不是错了: ...
- Linux系统中增加swap空间大小
在我的树莓派pi3上编译dlib库时,发现由于内存不足导致编译失败.树莓派是1G内存,swap只有50M,因此将swap增加到500M,编译通过.具体设置方法如下: 使用free命令带上m参数,查看s ...