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 ...
随机推荐
- AtCoder Beginner Contest 086 (ABCD)
A - Product 题目链接:https://abc086.contest.atcoder.jp/tasks/abc086_a Time limit : 2sec / Memory limit : ...
- 动手动脑-Java的方法重载
例: Using overloaded methods public class MethodOverload { public static void main(String[] args) { ...
- (2018干货系列八)最新VR学习路线整合
怎么学VR 即虚拟现实技术,是一种可以创建和体验虚拟世界的计算机仿真系统,它利用计算机生成一种模拟环境,是一种多源信息融合的.交互式的三维动态视景和实体行为的系统仿真使用户沉浸到该环境中.VR/AR/ ...
- GoldenGate 12.3 MA架构介绍系列(3) - 各功能模块介绍
在新版的ogg 12.3 microservice architect中,提供了4个不同的服务模块和命令行模块. Admin Server: 负责连接用户.trandata, checkpoint的添 ...
- Kali linux apt-get update 失败,无release……(最有效)
设置源 编辑 /etc/apt/sources.list nano /etc/apt/sources.list 清空文件内所有内容后添加 deb http://mirrors.ustc.edu.cn/ ...
- FastStone Capture无法录制系统声音解决方法(win10)
步骤一: 右键桌面-->个性化 步骤二: 主题-->高级声音设置 步骤三: 录音-->右键“立体声混音”,单击启用就OK了.
- 如何解决win10关机状态下,按键盘会自动开机的问题
关机状态下按下键盘会自动开机,是因为所装的系统默认设置了快速启动功能 下面是关闭快速启动的方法: 步骤一: 在win10桌面右击,点击显示设置 步骤二: 电源和睡眠-->其他电源设置 步骤三: ...
- 在nginx的http模块下面,一个server就可以看做一个站点,配置形式大概是这样的:
http { index index.php index.htm index.html; server { server_name www.site1.com; location / { # [... ...
- 使用maven profile实现多环境配置相关打包
项目开发需要有多个环境,一般为开发,测试,预发,正式4个环境,通过maven可以实现按不同环境进行打包部署,命令为: mvn package -P dev 在eclipse中可以右击选项run con ...
- 【题解】Luogu P3217 [HNOI2011]数矩形
原题链接:P3217 [HNOI2011]数矩形 什么??!怎么又是计算几何,您钛毒瘤了-- 这道题真的是毒瘤 凸包?旋转卡壳? 看一下数据,N<=1500? 暴力 没错,就是暴力,N^2没毛病 ...