Codeforces Round #439 E. The Untended Antiquity (树状数组+随机化)
来源:互联网 发布:软件开发做账 编辑:程序博客网 时间:2024/06/05 10:06
Problem
Problem Link
在
1 r1 c1 r2 c2
表示沿着 (r1, c1, r2, c2) 所表示的矩形的外边框建围墙。(其中 (r1, c1) 为矩形左上角,(r2, c2) 表示矩形右下角)。2 r1 c1 r2 c2
表示取消 (r1, c1, r2, c2) 所示矩形的围墙。(保证最初图不存在围墙,删除的围墙一定是之前通过操作 1 建立的)。3 r1 c1 r2 c2
表示询问 (r1, c1) 到 (r2, c2) 是否可以不翻越围墙到达。对于每个操作 3,可以输出Yes
,否则输出No
。
Limit
Idea
基础的二维树状数组可以实现单点更新,区间查询的操作。与一维的类似,显然可以快速转换为区间更新,单点查询。考虑利用状压,则此二维树状数组最多可以维护 64 个不同的围墙(以长整型存储)。但是,显然这仍然不足以处理描述种的
考虑异或,将二维树状数组的更新操作全部改为异或,对每个新的围墙,都利用随机化产生一个长整型的值代表该围墙内所有数都将异或上该值。则对于操作 3 的询问,一旦查询到的 (r1, c1) 与 (r2, c2) 的异或值不同,显然它们是由多个不同的围墙包围,则不可直接到达(必须翻墙:) )。否则可达。
HINT:注意 C++ 随机数的范围在
more: 请不要问关于证明该随机化可行的问题,博主自己也表示想当迷糊,无法给出碰撞量级的证明。
Code
#include<bits/stdc++.h>using namespace std;const int N = 2500 + 10;int n, m, q, t, r1, r2, c1, c2;long long rnd;map<long long, long long> mp;long long rect[N][N];int lowbit(int x) { return x & -x; }void add(int x, int y, long long w) { for(int i=x;i<N;i+=lowbit(i)) for(int j=y;j<N;j+=lowbit(j)) rect[i][j] ^= w;}long long get(int x, int y) { long long ret = 0; for(int i=x;i;i-=lowbit(i)) for(int j=y;j;j-=lowbit(j)) ret ^= rect[i][j]; return ret;}long long getRand() { return (RAND_MAX + 1ll) * rand() * rand(); }int main(){ scanf("%d %d %d", &n, &m, &q); for(int i=1;i<=q;i++) { scanf("%d %d %d %d %d", &t, &r1, &c1, &r2, &c2); if(t == 3) { printf("%s\n", get(r1, c1) == get(r2, c2) ? "Yes" : "No"); continue; } if(t == 2) { rnd = mp[ r1*(1ll<<36) + c1*(1ll<<24) + r2*(1ll<12) + c2 ]; } else { rnd = getRand(); mp[ r1*(1ll<<36) + c1*(1ll<<24) + r2*(1ll<12) + c2 ] = rnd; } add(r1, c1, rnd); add(r1, c2+1, rnd); add(r2+1, c1, rnd); add(r2+1, c2+1, rnd); }}
阅读全文
0 0
- Codeforces Round #439 E. The Untended Antiquity (树状数组+随机化)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity 二维树状数组 随机化
- Codeforces Round #439 (Div. 2) E:The Untended Antiquity(Hash+二维树状数组)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity 二维线段树||二维树状数组
- Codeforces 869 E. The Untended Antiquity 二维树状数组
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity hash+二维树状数组或二维线段树
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity (hash+数状数组)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity(二维BIT)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity(Hash)
- CF #439 E The Untended Antiquity
- CF869 E. The Untended Antiquity
- CF869 E. The Untended Antiquity
- codeforces 869E The Untended Antiquity 二维BIT + hash
- [哈希+差分] Codeforces 869E. The Untended Antiquity
- codeforces—— 869E —— The Untended Antiquity
- codeforces869EThe Untended Antiquity(二维树状数组)
- React+Redux,从MVC到Flux
- 单例模式
- 中缀表达式与前缀、后缀表达式的转换
- 表达式语言
- FileWritter写入文件
- Codeforces Round #439 E. The Untended Antiquity (树状数组+随机化)
- MODBUS-RTU数据帧格式、报文实例
- 基于react+redux的轻量级框架dva初使用及应用逻辑分析
- office2016(包括其他版本)办公软件破解方式
- Linux 基本操作和相关命令
- koa2 入门及express应用迁移到koa2实例
- python里使用正则表达式的重复模式
- 用node从零开始去写一个简单的爬虫
- java写入文件的几种方法