Light on or off

本文解析了一道关于线段树的经典题目,通过实例详细介绍了如何使用线段树进行区间更新和查询操作,特别关注了区间反转操作的实现细节。

一道曾经校赛的题目,也算是线段树的一道很好的题。

http://222.196.33.254/oj/showproblem?problem_id=1684

 

Description

There is N lights on the wall of Dreamone’s house from left to right.Initially,some lights on and some lights
off, and we use ‘1’ represented the light on, use ‘0’ represented the light off. As we know, there is a cat in
dremone’s house, and she is very naught. She always change the status continuously from Ath light to Bth
one. (1<=A, B<=N).If the light is on, then the light will be off, If the light is off, then the light will be on.
Actually, she can do it K times like this. Then the cat puts forward another problem: How many lights on
from Cth light to Dth (1<=C, D<=N) one? For example:
When N=4 K=2, and the initial status is assumed as:
1 0 1 1
‘1’ represented on,’0’ represented off.
From the initial status we can get: There is 3 lights on from 1st light to 4th, 2 lights on from 2nd to 4th and so
on. Then we assume the first operation that we change the status from 2nd to 4th, and then the status will be:
1 1 0 0
Then there are 2 lights on from 1st light to 4th one, 1 light on from 2nd to 4th one and so on. Then the second
operation is assumed as from the 1st to 2nd .Then the status will be 0 0 0 0. And there will be no lights on.
Can you get the main idea? Can you help the naught cat?

Input

The first line of input will be a positive integer indicating how many test cases will be included (T) and T
will be less than 10. Each of the next T cases will contain two parts:
The first part: two integer N, K (1<=N<=100000, 1<=K<=100000)
The second part: N numbers (which is ‘0’ or ‘1’) represented the initial status from left to right.
Then third part: K lines. Each line will be X C D (1<=C, D<=N) X is a letter which is either ‘Q’ or ‘C’. If
X=’Q’, you will be output the numbers of lights on from Cth to Dth, and if X=’C’, you will be change the
status of lights as the rules described above.

Output

For each query,(when X=’Q’),you should output the numbers of lights on. What’s more, you must output a
blank line after you have processed a test case.

Sample Input

2

4 3
1 0 1 1
Q 2 4
C 2 3
Q 1 4
4 1
1 0 1 1
Q 2 4

Sample Output

2

3
2
 

 

Load Port Indicators (1) Manual Mode Indicator (Yellow/Red)  Function: Indicate Load Port access mode: “Manual” mode or “Auto” mode”  Color: Yellow LED on normal functions. Red LED on assist mode  States: This indicator has 3 potential states: Yellow ON, Red ON and OFF. Manual Mode Indicator Description Yellow ON Manual mode OFF Auto mode  Notes: This indicator is effective only in manual mode. It is OFF (indicating that hand-off button is not usable) when the state of the load port is set to AUTO VENDOR COMPLIANCE LEVEL: __________ SUPPLER COMMENT: __________________________________________________ (2) Load Ready Indicator (Green/Red)  Function:Indicate “Load Ready” and “Loading” states  Color: Green LED on normal functions. Red LED on assist mode  States: This indicator has 4 potential states – Green ON, Breen BLINKING, Red ON and OFF Load Ready Indicator Definition Green ON Ready for loading Green BLINKING Loading is in progress OFF No manual load is allowed  Notes: This indicator will only be effective in manual access mode. During AUTO access mode, the indicator will always stay OFF VENDOR COMPLIANCE LEVEL: _______ SUPPLER COMMENT: __________________________________________________ (3) Carrier Presence Indicator (Light Orange/Red)  Function: To indicate the presence of the carrier detected on the load port.  Color:Orange LED on normal functions. Red LED on assist mode  States: The indicator has 3 potential states – Light Orange ON, Red ON and OFF Carrier Presence Indicator Carrier Presence Sensor Light Orange ON Carrier is present OFF Carrier is NOT present VENDOR COMPLIANCE LEVEL: ___________ SUPPLER COMMENT: ___________________________________________________
08-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值