FZU 1968 Twinkling lights III
Twinkling lights III
This problem will be judged on FZU. Original ID: 1968
64-bit integer IO format: %I64d Java class name: Main
Twinkling lights一直以来都很好玩的游戏。或许你还记得FZU1069 Twinkling lights 和FZU1420 Twinkling lights II。现在,Bluewind改变了一下游戏规则,游戏将变得更好玩。
N盏灯排成一行,编号1..N,起初的时候,所有的灯是开着的。Bluewind将执行M个操作,操作分成五种:
C x y,把编号从x到y的灯都关掉,原来关着的灯保持不变。
S x y,把编号从x到y的灯都开起来,原来开着的灯依旧开着。
A x y,让编号从x到y的灯都改变状态,即把原来开的灯关了,原来关了的灯开起来。
Q x y,查询编号从x到y中开着的灯的个数。
L x y,查询编号从x到y中最长连续开着的灯的个数。
Input
第一行两个整数N,M(1<=N,M<=500,000)表示有N盏灯,M个操作。
接下来M行,每行按指定格式给出一个操作,其中(1<=x<=y<=N)。
Output
对于每条Q查询操作和L查询操作,输出相应的结果。
Sample Input
10 5
C 2 8
S 5 7
A 1 10
Q 1 10
L 1 10
Sample Output
4
3
Source
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = ;
struct node {
int lt,rt,cover;
} tree[maxn<<];
int ret;
void build(int L,int R,int v) {
tree[v].lt = L;
tree[v].rt = R;
tree[v].cover = ;
if(L == R) return;
int mid = (L + R)>>;
build(L,mid,v<<);
build(mid+,R,v<<|);
}
inline void pushup(int v) {
if(tree[v<<].cover == tree[v<<|].cover)
tree[v].cover = tree[v<<].cover;
else tree[v].cover = -;
}
inline void pushdown(int v) {
if(tree[v].cover >= ) {
tree[v<<].cover = tree[v<<|].cover = tree[v].cover;
tree[v].cover = -;
}
}
void update(int lt,int rt,int val,bool sel,int v) {
if(sel && tree[v].cover == val) return;
if(lt <= tree[v].lt && rt >= tree[v].rt && (sel || !sel && tree[v].cover >= )) {
if(sel) tree[v].cover = val;
else tree[v].cover ^= ;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) update(lt,rt,val,sel,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,val,sel,v<<|);
pushup(v);
}
void query(int lt,int rt,int &ans,int &r,bool sel,int v) {
if(!tree[v].cover) return;
if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].cover > ) {
if(sel) ans += tree[v].rt - tree[v].lt + ;
else {
if(r + == tree[v].lt) ans += tree[v].rt - tree[v].lt + ;
else ans = tree[v].rt - tree[v].lt + ;
}
r = tree[v].rt;
ret = max(ans,ret);
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) query(lt,rt,ans,r,sel,v<<);
if(rt >= tree[v<<|].lt) query(lt,rt,ans,r,sel,v<<|);
pushup(v);
}
int main() {
int n,m,x,y,ans,r;
char op[];
while(~scanf("%d %d",&n,&m)) {
build(,n,);
while(m--) {
scanf("%s%d%d",op,&x,&y);
switch(op[]) {
case 'C':
update(x,y,,true,);
break;
case 'S':
update(x,y,,true,);
break;
case 'A':
update(x,y,,false,);
break;
case 'Q':
ret = ans = r = ;
query(x,y,ans,r,true,);
printf("%d\n",ret);
break;
case 'L':
ret = ans = r = ;
query(x,y,ans,r,false,);
printf("%d\n",ret);
break;
}
}
}
return ;
}
FZU 1968 Twinkling lights III的更多相关文章
- HDOJ 4770 Lights Against Dudely
状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- FZU 2137 奇异字符串 后缀树组+RMQ
题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 用Kotlin开发Android应用(III):扩展函数和默认值
这是关于Kotlin的第三篇. 原文标题:Kotlin for Android (III): Extension functions and default values 原文链接:http://an ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- BZOJ 1968: [Ahoi2005]COMMON 约数研究
1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2032 Solved: 1537[Submit] ...
- 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律
F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...
随机推荐
- android mvp高速开发框架介绍(dileber使用之小工具使用)
android mvp框架:dileber(https://github.com/dileber/dileber.git) 继续为大家介绍android mvp开源框架 dileber 官方交流qq群 ...
- 【递推DP】POJ1163The Triangle
题目链接:http://poj.org/problem?id=1163 事实上这个题目有非常多解法,可是我们能够看下这个用一位数组的高效动规解法,这个我上课时老师讲的,非常不错. 先保存最后一行4 5 ...
- WPF-MVVM-Demo
MVVM The model-view-viewmodel is a typically WPF pattern. It consists of a view that gets all the us ...
- 卸载虚拟机时错误关闭了某个服务,使得电脑除了chrome浏览器都不能联网
不是中毒,也不需要重装系统,可能是关闭了svchost服务 以下为搜索到的答案,亲测第一种好用 最近两周在三班四班有5位同学电脑7次出现网络故障,表现为能连上锐捷.DNS正常却不能上网,其中在我自己的 ...
- Can not Stop-Computer in powershell 6.0
1 PS C:\Program Files\PowerShell\6.0.0-beta.6> Stop-ComputerStop-Computer : Failed to stop the co ...
- 4.Mocha的基本用法
转自:http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html 有了测试脚本以后,就可以用Mocha运行它.请进 ...
- 鼠标滑过,解决ul下 li下a的背景与父级Li不同宽的问题
我们在写导航或者页面有超链接的地方,有一些是需要超链接的背景和Li的宽度一样的.但是,却没有达到这种效果?为什么? 我们做的效果图:如下 期望的效果:如下 出现这样的原因:由于a是个行内元素,它没有宽 ...
- salt的grains
grains作用: 1.匹配 minion 2.收集信息 (每次重启minion才会收集) grains 数据存储在minion端. salt '*' grains.ls salt '*' grain ...
- cygwin下调用make出现的奇怪现象
<lenovo@root 11:48:03> /cygdrive/d/liuhang/GitHub/rpi_linux/linux$make help 1 [main] make 4472 ...
- Spring拦截器 /* 和 /** 的区别
SpringMVC 拦截器拦截 /* 和 /** 的区别: /* : 匹配一级,即 /add , /query 等 /** : 匹配多级,即 /add , /add/user, /add/user/u ...