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. AtCoder Beginner Contest 082 A - Round Up the Mean

    题目链接:https://abc082.contest.atcoder.jp/tasks/abc082_a Time limit : 2sec / Memory limit : 256MB Score ...

  2. 51Nod 1072 威佐夫游戏

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆 ...

  3. 关于mapreducer 读取hbase数据 存入mysql的实现过程

    mapreducer编程模型是一种八股文的代码逻辑,就以用户行为分析求流存率的作为例子 1.map端来说:必须继承hadoop规定好的mapper类:在读取hbase数据时,已经有现成的接口 Tabl ...

  4. PHP图片裁剪与缩放示例(无损裁剪图片)

    <?php /* *exif_imagetype -- 判断一个图像的类型 *功能说明:函数功能是把一个图像裁剪为任意大小的图像,并保持图像不变形 *参数说明:输入 需要处理图片的 文件名,生成 ...

  5. bzoj1088 P2327 [SCOI2005]扫雷

    P2327 [SCOI2005]扫雷 emmmmm.....这题真可以用状压写 因为每个数字只对3个格子有影响,相当于只有2^3=8种状态,所以可以用状压瞎搞 我们用8个数字代表二进制下的8种状态 0 ...

  6. Node.js初探

    1, 设计高性能.Web服务器的几个要点:事件驱动.非阻塞I/O 2,常见Web服务器架构: Web服务器的功能: 接受HTTP请求(GET.POST.DELETE.PUT.PATCH) 处理HTTP ...

  7. 前端 --- 3 css 属性

    一. 标签嵌套规则 块级标签能够嵌套某些块级标签和内敛标签(行内标签) 内敛标签不能嵌套块级标签,只能嵌套内敛标签 二.   属性 1.宽和高 (块级标签能够设置高度和宽度 内敛标签不能设置,设置了没 ...

  8. spring配置jax-ws

    在spring配置文件中新建bean(或者是在配置文件中添加bean),在该bean中添加指定的访问地址. @Bean public static SimpleJaxWsServiceExporter ...

  9. ibus-libpinyin 无法选择除第一个外的候选词

    其实不只一个人遇到这问题 https://github.com/libpinyin/ibus-libpinyin/issues/127 临时可用的解决办法是: 清理libpinyin的cache目录相 ...

  10. html 之 区块元素属性(待补充)

    区块标签:(会随着父类的宽度变化而变化) p,div 等(后续补充) 区块标签才能使用以下属性() width,height,min-height,max-height,min-width,max-h ...