@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的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. php学习知识点框架

    图片来源于知乎,感觉挺全面,通过查看可以更好的了解自己的薄弱知识点,大家共勉.

  2. Spring_Hibernate整合准备

    1,Spring整合Hibernate   整合什么? 1)由IOC容器来生成Hibernate的SessionFactory 2)让Hibernate使用上Spring的声明式事务 2,整合步骤 1 ...

  3. tomcat9 gzip

    我认为apr模式比较屌所以 <Connector port=" protocol="org.apache.coyote.http11.Http11AprProtocol&qu ...

  4. FreeMarker 对null值的处理技巧

    以下引用官方描述: ? The FreeMarker template language doesn't know the Java language null at all. It doesn't ...

  5. 使用Workstation虚拟机部署Linux操作系统

    一.安装虚拟机: 1.安装VMware Workstation; 2.选择主页.点创建新的虚拟机: 3.选择“典型”然后点下一步: 4.选择稍后安装操作系统: 5.客户机从左系统选择“Linux”版本 ...

  6. Vue--使用watch、computed、filter方法来监控

    watch与computed.filter: watch:监控已有属性,一旦属性发生了改变就去自动调用对应的方法 computed:监控已有的属性,一旦属性的依赖发生了改变,就去自动调用对应的方法 f ...

  7. 关于502 bad gateway报错的解决办法

  8. django的admin后台管理

    Admin后台管理 要进入admin后台管理首先要创建管理员账户 createsuperuser 其中密码要大于8位 使用之前要到应用下的admin.py中注册要管理的模型表 from django. ...

  9. .net core/.net 使用 CommandLineParser 来标准化地解析命令行

    CommandLineParser 是一款用于解析命令行参数的 NuGet 包.你只需要关注你的业务,而命令行解析只需要极少量的配置代码. 本文将介绍如何使用 CommandLineParser 高效 ...

  10. JS的八大数据类型

    js中的数据类型,包括基本数据类型(Number,String,Boolean, Undefined,Null)和   复杂(引用)数据类型(Object,Array,Function) 基本数据类型 ...