SPOJ IITWPC4F - Gopu and the Grid Problem (双线段树区间修改 区间查询)
Gopu and the Grid Problem
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
一开始想的是直接二维线段树或者二维树状数组搞一下,然后一看数据范围,GG。实在没办法看了网上的题解,用两个线段树分别处理行和列,然后查询的时候,再综合一下。
减去重叠的部分。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e5+;
const int M = ;
typedef long long LL; int q;
struct Seg {
int Tree[N << ]; // 标记的行数/列数
bool lazy[N << ];
void init() {
memset(Tree, , sizeof(Tree));
memset(lazy, , sizeof(lazy));
}
inline void pushup(int rt) { Tree[rt] = Tree[rt << ] + Tree[rt << | ]; }
inline void pushdown(int pos,int len)
{
if(lazy[pos])
{
Tree[pos<<]=len-len/-Tree[pos<<];
Tree[pos<<|]=len/-Tree[pos<<|];
lazy[pos<<]^=;
lazy[pos<<|]^=;
lazy[pos]=;
}
return;
}
void update(int L,int R,int l,int r,int pos)
{
if(l>=L&&r<=R)
{
Tree[pos]=(r-l+)-Tree[pos];
lazy[pos]^=;
return;
}
int mid=(l+r)>>;
pushdown(pos,r-l+);
if(L<=mid) update(L,R,l,mid,pos<<);
if(mid<R)update(L,R,mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)return Tree[pos];
int mid=(l+r)>>;
pushdown(pos,r-l+);
int ans=;
if(L<=mid) ans+=query(L,R,l,mid,pos<<);
if(R>mid) ans+=query(L,R,mid+,r,pos<<|);
return ans;
}
}row,col; int main() {
char op[];
int n = N;
row.init();
col.init();
int l,r,val;
int lx,ly,rx,ry;
char sign[];
int m;
scanf("%d",&m);
while(m--)
{
scanf("%s",sign);
if(sign[]=='x')
{
scanf("%d%d",&l,&r);
++l,++r;
row.update(l,r,,n,);
}
else if(sign[]=='y'){
scanf("%d%d",&l,&r);
++l,++r;
col.update(l,r,,n,);
}
else
{
scanf("%d%d%d%d",&lx,&ly,&rx,&ry);
++lx,++ly,++rx,++ry;
int nx=row.query(lx,rx,,n,);
int ny=col.query(ly,ry,,n,);
ll ans=(ll)(rx-lx+)*ny+(ll)(ry-ly+)*nx-(ll)*nx*ny;
printf("%lld\n",ans);
}
}
}
SPOJ IITWPC4F - Gopu and the Grid Problem (双线段树区间修改 区间查询)的更多相关文章
- spoj IITWPC4F - Gopu and the Grid Problem 线段树
IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the ...
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...
- SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)
BGSHOOT - Shoot and kill no tags The problem is about Mr.BG who is a great hunter. Today he has gon ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- A Simple Problem with Integers-POJ3468 区间修改+区间查询
题意: 给你n个数和2个操作,C操作是将一个区间内的每个数都加上k,Q操作是询问一个区间的和 链接:http://poj.org/problem?id=3468 思路: 线段树区间修改+区间查询 代码 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...
随机推荐
- 洛谷P4592 [TJOI2018]异或 【可持久化trie树】
题目链接 BZOJ4592 题解 可持久化trie树裸题 写完就A了 #include<algorithm> #include<iostream> #include<cs ...
- 洛谷 P2827 蚯蚓 解题报告
P2827 蚯蚓 题目描述 本题中,我们将用符号 \(\lfloor c \rfloor\) 表示对 \(c\) 向下取整,例如:\(\lfloor 3.0 \rfloor = \lfloor 3.1 ...
- ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库
ActiveMQ 消息持久化机制: ActiveMQ 消息的持久化机制有 JDBC.AMQ.KahaDB 和 LevelDB,其中本示例版本(5.15.2)默认机制为 KahaDB.无论哪种持久化机制 ...
- vue+koa+mysql简易demo
功能支持网址收藏编辑 代码: https://github.com/lanleilin/lanOdyssey/tree/master/vueKoa/webCollection1 运行方法: 在serv ...
- bootstrap再次回顾认识到的东西
1,需要使用html5文档类型(Doctype),因此在使用bootstrap项目的开头包含下面的代码段. <!DOCTYPE html> <html> ....... < ...
- C# using一般用法 (转)
using一般有着以下几种用法: 1.直接引入命名空间 a.using System ,这个是最常用的,就是using+命名空间,这样就可以直接使用命名空间中的类型,而免去了使用详细的命名空间 b.使 ...
- CMOS与BIOS
BIOS与CMOS的区别 : 1. 所谓BIOS,实际上就是微机的基本输入输出系统(Basic Input-Output System),其内容集成在微机主板上的一个ROM芯片上,主要保存着有关微机系 ...
- bzoj2161: 布娃娃
Description 小时候的雨荨非常听话,是父母眼中的好孩子.在学校是老师的左右手,同学的好榜样.后来她成为艾利斯顿第二 代考神,这和小时候培养的良好素质是分不开的.雨荨的妈妈也为有这么一个懂事的 ...
- HDU1025---(LIS 最长上升子序列 的应用)
分析: n行 每行包含两个整数p r;意思是p从到r 不能有交叉的路 p刚好从1->n, 可看做下标,到的地方看做值 就转化为了最长上升子序列的问题 此题难点,怎么将其转化为LIS问题 #inc ...
- HDU1869---(最短路+floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=1869 思路:最短路+floyd 分析:1 题目是要求所有的数据能否满足“六度分离”,那么我们就想到所有点之间的最 ...