[SPOJ IITWPC4F Gopu and the Grid Problem]线段树区间翻转
来源:互联网 发布:php fpm 错误日志 编辑:程序博客网 时间:2024/06/08 09:06
[SPOJ IITWPC4F Gopu and the Grid Problem]线段树区间翻转
知识点:data structure
segment tree
1. 题目链接
[SPOJ IITWPC4F Gopu and the Grid Problem]
2. 题意描述
在一个二维
-
-
-
3. 解题思路
一拿到题目,直接就是二维线段树。但是一看数据范围不对。那就细想一下,这个操作比较特殊,修改操作都是针对一整行,或者一整列的,实际上,就是相当于一个一维的操作。所以可以用两个线段树,互斥地分别处理行和列的修改操作,并分别维护行和列中,区间为
我们设行
但是查询又是二维的。所以需要将行列查询合并。因为行列的操作是异或的。所以查询的答案,就应该是
不懂的话在纸上比划比划就知道了。
4. 实现代码
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 100000 + 5;int q;template<class T>struct Seg { T sum[MAXN << 2]; // 标记的行数/列数 bool tag[MAXN << 2]; void init() { memset(sum, 0, sizeof(sum)); memset(tag, 0, sizeof(tag)); } inline void pushUp(int& rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } inline void pushDw(const int& w, int& rt) { if(tag[rt]) { sum[rt << 1] = (w - (w >> 1)) - sum[rt << 1]; sum[rt << 1 | 1] = (w >> 1) - sum[rt << 1 | 1]; tag[rt << 1] ^= 1; tag[rt << 1 | 1] ^= 1; tag[rt] = false; } } void update(int& L, int& R, int l, int r, int rt) { if(L <= l && r <= R) { tag[rt] ^= 1; sum[rt] = (r - l + 1) - sum[rt]; return; } int mid = (l + r) >> 1; pushDw(r - l + 1, rt); if(L <= mid) update(L, R, l, mid, rt << 1); if(R > mid) update(L, R, mid + 1, r, rt << 1 | 1); pushUp(rt); } T query(int& L, int& R, int l, int r, int rt) { if(L <= l && r <= R) return sum[rt]; int mid = (l + r) >> 1; T ret = 0; pushDw(r - l + 1, rt); if(L <= mid) ret += query(L, R, l, mid, rt << 1); if(R > mid) ret += query(L, R, mid + 1, r, rt << 1 | 1); return ret; }};Seg<int> row, col;int main() {#ifdef ___LOCAL_WONZY___ freopen("input.txt", "r", stdin);#endif // ___LOCAL_WONZY___ char op[5]; int l, r, x[2], y[2]; int n = MAXN; while(~scanf("%d", &q)) { row.init(); col.init(); while(q --) { scanf("%s", op); if(op[0] == 'q') { scanf("%d %d %d %d", &x[0], &y[0], &x[1], &y[1]); ++ x[0], ++ x[1], ++ y[0], ++ y[1]; int nr = row.query(x[0], x[1], 1, n, 1); int nc = col.query(y[0], y[1], 1, n, 1); LL ans = (LL)nr * (y[1] - y[0] + 1) + (LL)nc * (x[1] - x[0] + 1) - 2LL * nr * nc; printf("%lld\n", ans); continue; } scanf("%d %d", &l, &r); ++ l, ++ r; if(op[0] == 'x') { row.update(l, r, 1, n, 1); } else { col.update(l, r, 1, n, 1); } } } return 0;}
1 0
- [SPOJ IITWPC4F Gopu and the Grid Problem]线段树区间翻转
- SPOJ IITWPC4F - Gopu and the Grid Problem [线段树]【数据结构】
- SPOJ IITWPC4F 线段树
- 线段树区间翻转模板
- codevs3243:区间翻转,线段树
- SPOJ 18249 IITWPC4F
- spoj GSS7(树链剖分 + 线段树区间合并)
- Impossible Boss+SPOJ+线段树区间更新、区间查询
- codeforces 438D The Child and Sequence(线段树:单点更新+区间取模+区间和)
- SPOJ 2713(GSS4-线段树区间开方-多组数据)
- !SPOJ 1043 多次查询区间最大连续和-线段树
- CodeForces 276E - Little Girl and Problem on Trees 区间更新..N+1个线段树
- CodeForces 276E Little Girl and Problem on Trees(线段树区间更新)
- HDOJ 5443 The Water Problem(线段树区间最值)(长春网络赛)
- hdoj The Water Problem 5443 (线段树区间最值问题)
- [CODEVS3243]区间翻转(线段树||splay||块链)
- SPOJ TTM To the moon(主席树+区间操作)
- vjudge: spoj--to the moon(主席树区间修改)
- 李诗雨的2016
- 最小堆的实现--topk算法
- DLL Hell(DLL地狱)浅谈
- 关于RecyclerView的一切
- iOS 调取iPhone本地相册和开启相机
- [SPOJ IITWPC4F Gopu and the Grid Problem]线段树区间翻转
- Gradle学习笔记 使用Gradle Wrapper
- fuel部完高可用环境后vip__public无法启动从而导致创建虚机时无法创建virtualInterface.
- iOS - Swift 仿微信聊天图片显示
- linux学习笔记--chown命令
- 按月等额本息还款
- 对于JAVA集合的一些总结
- Java关键字系列 — volatile、synchronized、lock
- Microsoft Speech SDK 工具类