[原创]SPOJ IITWPC4F - Gopu and the Grid Problem [线段树]【数据结构】
2017-01-11 00:06:46 Tabris_ 阅读数:241
博客爬取于 2020-06-14 22:42:19
以下为正文
版权声明:本文为 Tabris 原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54319679
题目链接:http://www.spoj.com/problems/IITWPC4F/
-------------------------------------------------------------------------------------.
IITWPC4F - Gopu and the Grid Problem
no tags
Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y<=100000). Each integer coordinate contain a lamp, initially all the lamps are in off mode. Flipping a lamp means switching it on if it is in off mode and vice versa. Maggu will ask gopu 3 type of queries.
Type 1: x l r, meaning: flip all the lamps whose x-coordinate are between l and r (both inclusive) irrespective of the y coordinate.
Type 2: y l r, meaning: flip all the lamps whose y-coordinate are between l and r (both inclusive) irrespective of the x coordinate.
Type 3: q x y X Y, meaning: count the number of lamps which are in ‘on mode’(say this number be A) and ‘off mode’ (say this number be B) in the region where x-coordinate is between x and X(both inclusive) and y-coordinate is between y and Y(both inclusive).
Input
First line contains Q-number of queries that maggu will ask to gopu. (Q <= 10^5)
Then there will be Q lines each containing a query of one of the three type described above.
Output
For each query of 3rd type you need to output a line containing one integer A.
Example
Input:
3
x 0 1
y 1 2
q 0 0 2 2
Output:
4
-------------------------------------------------------------------------------------.
题目大意:
就是有一个横纵坐标范围均为[0,100000]的二位平面 ,每个位置的值只有 0/1,初始的时候均为 0.
定义三种操作,
x a b 将 x 轴坐标范围[a,b]的区域翻转,即 0/1 互换
y a b 将 y 轴坐标范围[a,b]的区域翻转,即 0/1 互换
q a b A B 查询(a,b)(A,B)范围内的区间中 1 的个数有多少。
解题思路:
因为题目提示的是整行的操作,所以很容易想到用两个数组分别标记对应行/列的翻转次数,奇数次为 1,偶数次为 0.
然后因为动态求区间和,想到树状数组,
然后发现一点,树状数组虽然能够很好的维护某一个值增减的变化,但是对于区间内翻转的变化既有增也有减,所以不可行,于是换成麻烦写的线段树维护就好了,
用区间更新就能很好的维护翻转标记了。
然后查询的操作与二维树状数组求和的操作一模一样,不再赘述。
附本题代码
-------------------------------------------------------------------------------.
1 | # include <bits/stdc++.h> |


