spoj IITWPC4F - Gopu and the Grid Problem 线段树
IITWPC4F - 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
线段树
题目链接:http://www.spoj.com/problems/IITWPC4F/en/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define mk make_pair
#define eps 1e-7
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=; /// 数组大小
struct SGT
{
int tree[N],lazy[N];
void pushup(int pos)
{
tree[pos]=tree[pos<<|]+tree[pos<<];
}
void pushdown(int pos,int l,int r)
{
if(lazy[pos])
{
lazy[pos<<]^=;
lazy[pos<<|]^=;
int mid=(l+r)>>;
tree[pos<<]=(mid-l+)-tree[pos<<];
tree[pos<<|]=(r-mid)-tree[pos<<|];
lazy[pos]=;
}
}
void build(int l,int r,int pos)
{
tree[pos]=lazy[pos]=;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
}
void update(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
lazy[pos]^=;
tree[pos]=(r-l+)-tree[pos];
return;
}
pushdown(pos,l,r);
int mid=(l+r)>>;
if(L<=mid)
update(L,R,l,mid,pos<<);
if(R>mid)
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];
pushdown(pos,l,r);
int mid=(l+r)>>;
int ans=;
if(L<=mid)
ans+=query(L,R,l,mid,pos<<);
if(R>mid)
ans+=query(L,R,mid+,r,pos<<|);
return ans;
}
};
SGT x,y;
char ch[];
int main()
{
int q;
int n=;
while(~scanf("%d",&q))
{
x.build(,n,);
y.build(,n,);
while(q--)
{
int l,r;
scanf("%s%d%d",ch,&l,&r);
//if(l<r)swap(l,r);
if(ch[]=='x')
x.update(l+,r+,,n,);
else if(ch[]=='y')
y.update(l+,r+,,n,);
else
{
int L,R;
scanf("%d%d",&L,&R);
//if(L<R)swap(L,R);
int xx=x.query(l+,L+,,n,);
int yy=y.query(r+,R+,,n,);
ll ans=;
ans+=1LL*xx*(R-r+);
ans+=1LL*yy*(L-l+);
ans-=2LL*xx*yy;
printf("%lld\n",ans);
}
}
}
return ;
}
spoj IITWPC4F - Gopu and the Grid Problem 线段树的更多相关文章
- 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 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值
SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- Codeforces 803G Periodic RMQ Problem 线段树
Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...
- Codeforces 903G Yet Another Maxflow Problem - 线段树
题目传送门 传送门I 传送门II 传送门III 题目大意 给定一个网络.网络分为$A$,$B$两个部分,每边各有$n$个点.对于$A_{i} \ (1\leqslant i < n)$会向$A_ ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- 【CF903G】Yet Another Maxflow Problem 线段树
[CF903G]Yet Another Maxflow Problem 题意:一张图分为两部分,左边有n个点A,右边有m个点B,所有Ai->Ai+1有边,所有Bi->Bi+1有边,某些Ai ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
随机推荐
- CAT Caterpillar ET is really a exceptional obd2 solution
As a excellent obd2 solutions,Heavy Duty Diagnostic CAT Caterpillar ET Diagnostic Adapter features a ...
- TCP协议、算法和原理
TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人有很多收获. 关于TCP这个协议的细节,我还是推荐你 ...
- gulp和webpack的区别
一.概念 gulp 构建工具 我们可以通过给gulp配置不通的task(通过Gulp中的gulp.task()方法配置,比如启动server.sass/less预编译.文件的合并压缩等等)来让gulp ...
- linux学习笔记---grep
先来讲讲grep(搜索过滤) 1.命令格式: grep [option] pattern file 2.命令参数: -a 不要忽略二进制的数据 -A<显示行数> 除了显示 ...
- ECharts + Jquery 做大屏展示
HTML <!doctype html> <html> <head> <meta charset="utf-8"> <meta ...
- 在idea中不出现大波浪的设置
在idea中如果有重复代码时候,就会出现大波浪 ,然后,现在可以设置 Duplicated Code 的对号去掉就可以没有大波浪
- python基础:re模块匹配时贪婪和非贪婪模式
python贪婪和非贪婪 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符:非贪婪则相反,总是尝试匹配尽可能少 ...
- Prometheus监控学习笔记之Prometheus普罗米修斯监控入门
0x00 概述 视频讲解通过链接网易云课堂·IT技术快速入门学院进入,更多关于Prometheus的文章. Prometheus是最近几年开始流行的一个新兴监控告警工具,特别是kubernetes的流 ...
- 在win10下安装eclipse
1.在官网下载jdk.目前最新版本为jdk8. http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21331 ...
- Maven 工程读取resource下的文件
1:方式1: public static List<String> userList; static { userList = new LinkedList<String>() ...