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 ...
随机推荐
- 图像切割—基于图的图像切割(Graph-Based Image Segmentation)
图像切割-基于图的图像切割(Graph-Based Image Segmentation) Reference: Efficient Graph-Based Image Segmentation ...
- programming-challenges Shoemaker's Problem (110405) 题解
Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., ...
- Android SQLite 简单使用演示样例
SQLite简单介绍 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也很的强大. 袖珍型的SQLite能够支持高达2TB大小的数据库, ...
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记3 Xcode、Auto Layout及MVC
继续上一话中的计算器Demo.上一话讲到类必须被初始化.类中的属性也必须被初始化,所以你不能仅仅声明而不给它一个处置,那么问题来了,我们从storyboard中拖拽的@IBOutlet为什么仅仅有声明 ...
- Introduction to MongoDB
https://docs.mongodb.com/getting-started/csharp/introduction/ MongoDB is an open-source document dat ...
- UITextField限制输入长度
首先,汉字的输入时的联想词在输入到TextFiled时,并不会走 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersIn ...
- 85.Mongoose指南 - Schema
转自:https://www.bbsmax.com/A/pRdBnKpPdn/ 定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的 ...
- jsp输出九九乘法表
<% String st = ""; for(int i = 1; i <= 9; i++){ for(int j = 1; j <= i; j++){ st + ...
- h5调用手机前后摄像头,拍照
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pacam.aspx.cs& ...
- View的双击动作
有时在android中需要为某一控件设置双击监听,实现也挺简单,自己动手吧.编码永远不是问题,思路才是最重要. public class DoubleClickDemo extends Activit ...