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

线段树

题目链接: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 线段树的更多相关文章

  1. 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 ...

  2. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

  3. 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 ...

  4. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  5. Codeforces 803G Periodic RMQ Problem 线段树

    Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...

  6. Codeforces 903G Yet Another Maxflow Problem - 线段树

    题目传送门 传送门I 传送门II 传送门III 题目大意 给定一个网络.网络分为$A$,$B$两个部分,每边各有$n$个点.对于$A_{i} \ (1\leqslant i < n)$会向$A_ ...

  7. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  8. 【CF903G】Yet Another Maxflow Problem 线段树

    [CF903G]Yet Another Maxflow Problem 题意:一张图分为两部分,左边有n个点A,右边有m个点B,所有Ai->Ai+1有边,所有Bi->Bi+1有边,某些Ai ...

  9. 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 ...

随机推荐

  1. 安装mysql警告 warning: mysql-community-server-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

    摘自:https://www.cnblogs.com/royfans/p/7243641.html 红帽安装rpm安装MySQL时爆出警告: 警告:MySQL-server-5.5.46-1.linu ...

  2. Mysql初级第二天(wangyun)

    SQL 1.LIKE 操作符 SELECT 列名称 FROM 表名称 WHERE 列 LIKE 值('N%'/'%N%'/'%N','N_') SELECT 列名称 FROM 表名称 WHERE 列 ...

  3. codeSourcery交叉编译环境

    arm-none-Linux-gnueabi-gcc是 Codesourcery 公司(目前已经被Mentor收购)基于GCC推出的的ARM交叉编译工具.可用于交叉编译ARM系统中所有环节的代码,包括 ...

  4. PYTHON 对SQLITE3的简单使用

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成.Python就内 ...

  5. await

    单个的task await task 多个await asyncio.wait(tasks)

  6. VMware环境安装MacOS

    环境: win10专业版 VMware 14 Pro 开始吧 1. 停止服务 2. 解压并管理员权限运行unlocker,目的是使得 win10 环境下的 VMWare14Pro 支持 mac 系统的 ...

  7. P3380 【模板】二逼平衡树(树套树)(线段树套平衡树)

    P3380 [模板]二逼平衡树(树套树) 前置芝士 P3369 [模板]普通平衡树 线段树套平衡树 这里写的是线段树+splay(不吸氧竟然卡过了) 对线段树的每个节点都维护一颗平衡树 每次把给定区间 ...

  8. 【题解】Luogu P5072 [Ynoi2015]盼君勿忘

    众所周知lxl是个毒瘤,Ynoi道道都是神仙题,题面好评 原题传送门 一看这题没有修改操作就知道这是莫队题 我博客里对莫队的简单介绍 既然是莫队,我们就要考虑每多一个数或少一个数对答案的贡献是什么 假 ...

  9. Hibernate properties文件

    ###################### ### Query Language ### ###################### ## define query language consta ...

  10. ZVAL——PHP源码分析

    基于 PHP 5.6.20 ZVAL——php变量实现的基础 _zval_struct 结构体的定义位于 Zend/zend.h 322 行 typedef union _zvalue_value { ...