@codeforces - 1214G@ Feeling Good
@description@
给定一个 n*m 的 01 矩阵 A,一开始所有格子都为 0。
我们定义一个子矩阵 (x1, y1) - (x2, y2) 是好矩阵,当且仅当 A(x1, y1) = A(x2, y2);A(x2, y1) = A(x1, y2);A(x1, y1) ≠ A(x1, y2)。
其中满足 x1 < x2, y1 < y2。
现有 q 次修改,每次形式为 (a, l, r),表示将第 a 行的 l~r 个元素全部取反。
现需要在每次修改后判断是否有解。如果有,任意输出一组解。
Input
第一行三个整数 n, m, q (1≤n,m≤2000,1≤q≤500000)。
接下来 q 行每行三个整数 ai, li, ri,(1≤ai≤n, 1≤li≤ri≤m)。
Output
输出 q 行,第 i 行表示第 i 次操作后的任意一组好矩阵 x1, y1, x2, y2(1≤x1<x2≤n, 1≤y1<y2≤m)。
若无解输出 -1。
Examples
Input
2 2 6
1 1 1
2 2 2
2 1 1
1 2 2
2 2 2
1 1 1
Output
-1
1 1 2 2
-1
-1
-1
1 1 2 2
@solution@
神仙题。
我们记第 i 行形成的二进制数为 Ai;同时 Ai 也可表示一个集合:1 为元素存在,0 为元素不存在。
我们下文将不加区分,即 Ai 既可以使用集合运算也可以使用二进制运算。
首先考虑假如给定两行 Ai, Aj,怎么才能快速得到解。实际上就是找 Ai 中 0 对应 Aj 中 1;Aj 中 0 对应 Ai 中 1。
将 Ai 取反得到 Ai',则 Ai' 中 1 对应 Aj 中 1,将 Ai' 与 Aj 进行 & 运算即可;同理可以将 Aj' 与 Ai 进行 & 运算。
而以上操作不难使用 bitset 实现。
现在假如两行 Ai 与 Aj 之间没有解意味着什么?要么 Ai' 与 Aj 没有相交部分,即 \(Ai' \bigcap Aj = \phi\),等价地就是 \(Aj \subset Ai\);反过来也可以是 \(Ai \subset Aj\)。
注意无解时,两行形成偏序关系(包含关系\(\subset\))。这意味着如果整个局面无解,则所有行形成一个偏序链。
而同时,这个偏序链是按照集合的大小排序的。
于是就可以设计出算法:我们把所有行用 bitset 维护修改操作。
对所有行对应的 bitset,按照其包含元素的多少,用 set 来进行维护。
每次在 set 里面插入删除时,维护 set 相邻两个元素是否为 包含关系\(\subset\):如果不是,说明这两行之间有解。
另开一个 set 存储这些行二元组。最后如果该 set 不为空则有解,按照上面的方法找出这样一个解(不过需要手写 bitset 支持 lowbit 查询 update in 2019/09/28:然而bitset里面可以使用_Find_first()函数找到第一个 1 所在的位置。。。)。
时间复杂度 O(p*(logn + m/64))。
@accepted code@
#include<cstdio>
#include<set>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2000 + 5;
#define BITNUM 64
ull f[BITNUM];
struct bitset{
#define SIZE 32
ull a[SIZE], cnt;
bitset() {
for(int i=0;i<SIZE;i++) a[i] = 0;
}
void count() {
cnt = 0;
for(int i=0;i<SIZE;i++)
cnt += __builtin_popcount(a[i]) + __builtin_popcount(a[i] >> 32);
}
friend bool operator < (const bitset &a, const bitset &b) {
return a.cnt < b.cnt;
}
friend bitset operator &(const bitset &a, const bitset &b) {
bitset c;
for(int i=0;i<SIZE;i++)
c.a[i] = a.a[i] & b.a[i];
return c;
}
friend bitset operator ^(const bitset &a, const bitset &b) {
bitset c;
for(int i=0;i<SIZE;i++)
c.a[i] = a.a[i] ^ b.a[i];
return c;
}
friend bitset operator ~(const bitset &a) {
bitset c;
for(int i=0;i<SIZE;i++)
c.a[i] = ~a.a[i];
return c;
}
friend bool operator == (const bitset &a, const bitset &b) {
for(int i=0;i<SIZE;i++)
if( a.a[i] != b.a[i] )
return false;
return true;
}
friend bool operator != (const bitset &a, const bitset &b) {
return !(a == b);
}
void set(ull k) {
for(int i=0;i<SIZE;i++) {
if( k < BITNUM ) {
a[i] |= f[k];
break;
}
else k -= BITNUM;
}
}
ull bit() {
for(int i=0;i<SIZE;i++)
if( a[i] ) {
int l = 0, r = BITNUM - 1;
while( l < r ) {
int mid = (l + r + 1) >> 1;
if( f[mid] <= a[i] ) l = mid;
else r = mid - 1;
}
return l + i*BITNUM;
}
puts("error");
exit(0);
}
};
set<pair<bitset, int> >st1;
set<pair<bitset, int> >::iterator it1, it2, it3;
set<pair<int, int> >st2;
set<pair<int, int> >::iterator it;
bitset bts[MAXN], b[MAXN];
int n, m, q;
bool check(const bitset &a, const bitset &b) {
if( (a & b) != a && (a & b) != b )
return true;
else return false;
}
void init() {
f[0] = 1;
for(int i=1;i<BITNUM;i++)
f[i] = f[i-1]<<1;
}
int main() {
init();
scanf("%d%d%d", &n, &m, &q);
for(int i=1;i<=m;i++)
b[i] = b[i-1], b[i].set(i - 1), b[i].count();
for(int i=0;i<n;i++)
st1.insert(make_pair(bts[i], i));
for(int i=1;i<=q;i++) {
int a, l, r; scanf("%d%d%d", &a, &l, &r), a--;
it1 = st1.find(make_pair(bts[a], a));
if( it1 == st1.begin() )
it2 = st1.end();
else it2 = it1, it2--;
it3 = it1, it3++;
if( it2 != st1.end() ) {
if( check(it2->first, it1->first) )
st2.erase(make_pair(it2->second, a));
}
if( it3 != st1.end() ) {
if( check(it1->first, it3->first) )
st2.erase(make_pair(a, it3->second));
}
if( it2 != st1.end() && it3 != st1.end() ) {
if( check(it2->first, it3->first) )
st2.insert(make_pair(it2->second, it3->second));
}
st1.erase(make_pair(bts[a], a));
bts[a] = bts[a] ^ b[r] ^ b[l - 1]; bts[a].count();
st1.insert(make_pair(bts[a], a));
it1 = st1.find(make_pair(bts[a], a));
if( it1 == st1.begin() )
it2 = st1.end();
else it2 = it1, it2--;
it3 = it1, it3++;
if( it2 != st1.end() ) {
if( check(it2->first, it1->first) )
st2.insert(make_pair(it2->second, a));
}
if( it3 != st1.end() ) {
if( check(it1->first, it3->first) )
st2.insert(make_pair(a, it3->second));
}
if( it2 != st1.end() && it3 != st1.end() )
if( check(it2->first, it3->first) )
st2.erase(make_pair(it2->second, it3->second));
if( st2.empty() ) {
puts("-1");
}
else {
pair<int, int> p = *st2.begin();
int x1 = min(p.first, p.second), x2 = max(p.first, p.second);
int y1 = (bts[p.first] & (~bts[p.second])).bit();
int y2 = (bts[p.second] & (~bts[p.first])).bit();
printf("%d %d %d %d\n", x1 + 1, min(y1, y2) + 1, x2 + 1, max(y1, y2) + 1);
}
}
}
@details@
update in 2019/09/28:感谢评论区。其实可以使用_Find_first()函数来找到第一个 1 所在位置。不过我懒得改了(
在网上查了半天确认了 bitset 真的不能用 lowbit。
表示 __builtin_popcount 函数的参数只能是 long int,调了半天。
还因为这个函数 T 了几发。。。辣鸡玩意儿
@codeforces - 1214G@ Feeling Good的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- idea小操作
1.IDEA 实用功能Auto Import:自动优化导包(自动删除.导入包) 2.设置System.out.println();等快捷键 3.将idea的背景修改为图片 4.Linux ifconf ...
- webstorm 2017.3.5之后 激活
选择"license server" 输入:http://idea.codebeta.cn
- python安装和环境变量配置
python环境安装 一.打开官网:http://www.python.org 点击Downloads下载,如下图 python官网 二.根据电脑型号选择下载的版本 下载对应版本号的executabl ...
- golang之vscode环境配置
go语言开发,选择vscode作为IDE工具也是一个不错的选择,毕竟goland收费,老是破解也挺麻烦,除了这点,不过说实话挺好用的.vscode的话相对来说就毕竟原始,适合初学者. 1.vscode ...
- 2019-9-2-win10-uwp-保存用户选择文件夹
title author date CreateTime categories win10 uwp 保存用户选择文件夹 lindexi 2019-09-02 12:57:38 +0800 2018-2 ...
- sar磁盘I/O统计数据
sar是一个研究磁盘I/O的优秀工具.以下是sar磁盘I/O输出的一个示例. 第一行-d显示磁盘I/O信息,5 2选项是间隔和迭代,就像sar数据收集器那样.表3-3列出了字段和说明. 表3-3 ...
- VS code 格式化插件, 仅需一步, 无须配置
1.安装Beautify 2.重启vscode后 按F1, 点击 Beautify 即可
- 让Drewtech的J2534 ToolBox 软件支持任何J2534的设备
更改windows注册表中的FunctionLibrary和ConfigApplication,将DLL和exe路径替换原来的,其他不要动. 或者 create second key in regis ...
- 开源PaaS平台:Cloudify
Cloudify是gigaspaces公司推出的基于java的paas平台. refer to :http://timeson.iteye.com/blog/1699730
- 项目ITP(七) javaWeb 整合 Quartz 实现动态调度 而且 持久化
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010378410/article/details/36255511 项目ITP(七) javaWe ...