poj-3657 Haybale Guessing (half disjoint-set answer +)

http://poj.org/problem?id=3657

 

Under the Chinese version, the English do not want to look directly Click here to see the Chinese version of the problem

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 ≤  NQ 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: QlQh, 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

Explanation

The minimum number of bales in stacks 1..10 is 7, the minimum number of bales in stacks 5..19 is 7, the minimum number of bales in stacks 3..12 is 8, and the minimum number of bales in stacks 11..15 is 12.
Query 3 ("3 12 8") is the first that is inconsistent with those before it. From queries 1 and 2 and the fact that all hay stacks have a distinct number of bales, we deduce that one of stacks 5..10 must contain exactly 7 bales. However, this stack contradicts the answer to query 3.

 

Chinese Version

Meaning of the questions:

 To a length of n, the number of each position is different sequence a [1..n] and Q and q, then q is given intervals, and the minimum interval, the first few sections will obtain a contradiction

Ideas:

1. + binary tree line (not being later filled pit) 

2. dichotomy + disjoint-set (disjoint-set intervals for dyeing) // disjoint-set A classic usage - stained sections, so use disjoint-set maintenance

 

First, if we want to know before the cow cows wrong answer is not how to do it?

A reply to a row in descending order. There are several contradictory ways:

  • If the interval is completely behind the front section contains, it's wrong
  • If there are two disjoint intervals A is the same, and this is wrong (subject to ensure that no two piles of hay number is the same)

Note that the same taking interval when A is not more than half of the current mid

The following questions refer to the part solution: https://blog.csdn.net/wang2147483647/article/details/60142150

Since the number of unique for each position, for two intervals [l, r] is the minimum value a, [L, R] is the minimum value A.

If the interval [l, r] be the interval [L, R] is completely contained and a <A, and is unique at this time there is a contradiction contradiction.

Dichotomy may query Q, determined in 1 --- tot interrogation is legitimate. Every two time-sharing, the interrogation between 1 --- tot descending order according to the minimum value (priority processing large numbers, only after determining whether the interval is the interval where the fractional comprising large numbers where the judgment).

Because each unique number, the same for each of the minimum interval, it is determined whether the intersection is empty or larger minimum interval, when there is a conflict, continue half.

If not there is a conflict, it is the union staining (if no intersection where the interval unstained interval when such judgments contradiction, the intersection (minimum descending order in a larger minimum interval, the greater the minimum value all sections have been stained, the absence of undyed interval, then the minimum of this interval larger interval)).

When stained with a segment tree, Optimization no timeout, so it can be disjoint-set treatment staining: For some interval [l, r] if stained, then let fa [r] = l-1 (L is not, because l also stained point, such as data 121 and 122), representing the [l, R & lt] is the number of all of staining (looking forwardly from the rear section, for each point of the parent can jump directly node, because when this interval has been dyed all), it is determined if l> Find (r) indicates that the interval is no point unstained.

 code show as below:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 const int INF=0x3f3f3f3f;
10 using namespace std;
11 #define maxn 1000010
12 
13 struct node{
14     int l;
15     int r;
16     int num;
17 };
18 
19 int n,q;
20 int fa[maxn];
21 node a[maxn];
22 node tmp[maxn];
23 
24 int Find(int x)
25 {
26     return x==fa[x]?x:fa[x]=Find(fa[x]);
27 }
28 
29 bool cmp(node a,node b)
30 {
31     return a.num>b.num;
32 }
33 
34 bool judge(int tot)
35 {
36     for(int i=1;i<=n;i++)
37         fa[i]=i;
38     for(int i=1;i<=tot;i++)
39         tmp[i]=a[i];
40     sort(tmp+1,tmp+1+tot,cmp);
41     for(int i=1,j;i<=tot;i=j+1)
42     {
43         j=i;
44         int l=tmp[i].l;
45         int r=tmp[i].r;
46         int L=tmp[i].l;
47         int R=tmp[i].r;
48         while(j<tot&&tmp[j].num==tmp[j+1].num)
49         {
50             j++;
51             l=max(l,tmp[j].l);//取交集
52             r=min(r,tmp[j].r);
53             L=min(L,tmp[j].l);//取并集
54             R=max(R,tmp[j].r);
55         }
56         if(l>r||l>Find(r))//No empty or undyed point 
57 is              return  0 ;
 58          the while (L <= R & lt)
 59          {
 60              IF (the Find (R & lt) == R & lt)
 61 is              {
 62 is                  FA [R & lt] = the Find (L- . 1 ); // staining 
63 is                  R-- ;
 64              }
 65              the else 
66                  R & lt FA = [R & lt]; // jump directly to its parent node 
67          }
 68      }
 69      return  . 1 ;
 70  }
 71 is  
72  int main()
73 {
74     scanf("%d %d",&n,&q);
75     for(int i=1;i<=q;i++)
76     {
77         scanf("%d %d %d",&a[i].l,&a[i].r,&a[i].num);
78     }
79     int L=1;
80     int R=q;
81     int ans=0;
82     while(L<=R)
83     {
84         int mid=(L+R)/2;
85         if(judge(mid))
86             L=mid+1;
87         else
88         {
89             ans=mid;
90             R=mid-1;
91         }
92     }
93     printf("%d\n",ans);
94     return 0;
95 }

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11265547.html