http://poj.org/problem?id=3657
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1180 | Accepted: 359 |
Description
The cows, who always have an inferiority complex about their intelligence, have a new guessing game to sharpen their brains.
A designated 'Hay Cow' hides behind the barn and creates N (1 ≤ N ≤ 1,000,000) uniquely-sized stacks (conveniently numbered 1..N) of hay bales, each with 1..1,000,000,000 bales of hay.
The other cows then ask the Hay Cow a series of Q (1 ≤ Q ≤ 25,000) questions about the the stacks, all having the same form:
What is the smallest number of bales of any stack in the range of stack numbers Q l.. Qh (1 ≤ Q l ≤ N; Q l ≤ Qh ≤ N)?
The Hay Cow answers each of these queries with a single integer A whose truthfulness is not guaranteed.
Help the other cows determine if the answers given by the Hay Cow are self-consistent or if certain answers contradict others.
Input
* Line 1: Two space-separated integers: N and Q
* Lines 2..Q+1: Each line contains three space-separated integers that represent a single query and its reply: Ql, Qh, and A
Output
* Line 1: Print the single integer 0 if there are no inconsistencies among the replies (i.e., if there exists a valid realization of the hay stacks that agrees with all Q queries). Otherwise, print the index from 1..Q of the earliest query whose answer is inconsistent with the answers to the queries before it.
Sample Input
20 4
1 10 7
5 19 7
3 12 8
11 15 12
Sample Output
3
Source
题意:给一段长度为n,每个位置上的数都不同的序列a[1..n]和q和问答,每个问答是(x, y, r)代表RMQ(a, x, y) = r, 要你给出最早的有矛盾的那个问答的编号。
大致思路:二分答案qi。然后将前qi个问答按r从大到小排序,用染色的思想——如果当前问答的区间已经被全部染色了,就表示前qi的问答构成有矛盾。
判断染色的方法有很多种,本人一开始想到线段树,后来在别人的指导下学会用并查集(并查集要比线段树好写很多),具体的方法就是把某个问答区间[x, y]所有值的父亲都只想区间头x,关键在于枚举是从后枚举,而且那些祖先不是他自己的可以不用判断,也就是说枚举到一个数,其实可以直接跳到他祖先那里,因为中间肯定都已经染了色。在有并查集的思想只需要吧他祖先的父亲指向新的点就可以了。。这样均摊下来每次只需O(n), 总的时间复杂度为O(nlogm),离散化可以做到O(mlongm)。还有就是判断是否有未染色区间用含有相同r的区间的交去判断,染色是用这些区间的并染色。
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXM 25100
#define MAXN 1100000
#define eps (1e-8)
#define INF 1000000000
#define abs(x) ((x) > 0? (x): -(x))
#define sqr(x) ((x) * (x))
#define MAX(a, b) ((a) > (b)? (a): (b))
#define MIN(a, b) ((a) < (b)? (a): (b))
typedef long long LL;
struct QUERY
{
int x, y, a, id;
} List[MAXM];
int n, m, fa[MAXN];
bool colored[MAXN];
bool cmp_a(QUERY p, QUERY q)
{
return p.a > q.a;
}
bool cmp_id(QUERY p, QUERY q)
{
return p.id < q.id;
}
void init()
{
for (int i = 1; i <= m; ++i)
{
scanf("%d%d%d", &List[i].x, &List[i].y, &List[i].a);
List[i].id = i;
}
}
int find(int x)
{
int y = x;
while (fa[x] != x) x = fa[x];
while (y != x)
{
int tf = fa[y];
fa[y] = x;
y = tf;
}
return x;
}
bool color(int x, int y)
{
bool ret = false;
for (int i = y; i >= x; --i)
{
if (fa[i] == i && !colored[i]) ret = true;
if (fa[i] == i) fa[i] = x; else
{
int anc = find(i);
if (anc >= x) fa[anc] = x;
i = anc;
}
}
if (fa[x] == x) colored[x] = true;
return ret;
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
init();
int L = 1, R = m + 1;
while (L < R)
{
memset(colored, 0, sizeof(colored));
for (int i = 1; i <= n; ++i) fa[i] = i;
int M = (L + R) >> 1;
sort(List + 1, List + M + 1, cmp_a);
bool flag = true;
for (int i = 1; i <= M && flag; ++i) if (i == 1 || List[i].a != List[i - 1].a)
{
for (int j = i; j <= M && flag; ++j) if (j == M || List[j + 1].a != List[i].a)
{
int left = List[i].x, right = List[i].y;
for (int k = i + 1; k <= j; ++k)
{
left = MAX(left, List[k].x);
right = MIN(right, List[k].y);
}
if (left > right) flag = false;
if (color(left, right) == false) flag = false;
for (int k = i; k <= j; ++k) color(List[k].x, List[k].y);
break;
}
}
if (!flag)
R = M;
else
L = M + 1;
sort(List + 1, List + M + 1, cmp_id);
}
if (L > m) puts("0"); else printf("%d\n", L);
}
return 0;
}