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 ...
随机推荐
- extjs Combox 调用数据
1方法一 从 json获取 var typeStore = new Ext.data.Store({ proxy : new Ext.data.HttpProxy({u ...
- css实现上下左右布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 51nod-1189: 阶乘分数
[传送门:51nod-1189] 简要题意: 给出一个数n,求出有多少个正整数x,y(0<x<=y)满足$1/n!=1/x+1/y$ 题解: 一开始还以为不可做 结果推一下柿子就会了 $1 ...
- ORM中基于对象查询与基于queryset查询
感谢老男孩~ 一步一步走下去 前面是视图函数 后面是表结构models.py from django.shortcuts import render, HttpResponse from djang ...
- django 笔记11 装饰器
在views.py创建 一般用来cookies的装饰器 def auth(func): def inner(request, *args, **kwargs): v = request.COOKIES ...
- js封装each函数
function each(ele,callback){ if(Object.prototype.toString.call(ele) == "[object Array]"){ ...
- 洛谷 P4180 【模板】严格次小生成树[BJWC2010] LCT
首次采用了压行,感觉还不错. Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #i ...
- [POI2009]KON-Ticket Inspector(二维前缀和+DP)
题意 有n个车站,现在有一辆火车从1到n驶过,给出aij代表从i站上车j站下车的人的个数.列车行驶过程中你有K次检票机会,所有当前在车上的人会被检票,问最多能检多少个不同的人的票 (n<=600 ...
- Object-C,文件路径API
犀利吐槽 1.同样都是"文件和目录操作",java中,就用java.util.File一个类,就封装了很多API,而Object-C搞了这么多类和函数.具体原因,有待分析啊. 2. ...
- 题解 P1198 【[JSOI2008]最大数】
说起来这还是蒟蒻AC的第一道省选线段树呢. 这道题和其他线段树最大的不同就是在于本题数组一直在增大. 寻常的线段树蒟蒻习惯用如下的结构体储存,然而对于此题就不行了: struct node{ int ...