---
id: 167
lang: en
translation_of: index.md
source_sha256: 23ec98f727da751cd7f689933f46e1d8eccf63b9847edaae51e880c3b09e02e3
title: "P2898: [USACO08JAN] Haybale Guessing — Solution"
description: "Use a disjoint-set union for the set operations; the data range is huge and only the earliest answer is needed, so binary search on the answer."
tags: ["OI","Binary Search","Binary Search on the Answer","Disjoint-Set Union"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "haybale猜测 rendered with the problem's official English name, Haybale Guessing."
---

## Approach

This is another problem where I copied the solution. Let's first briefly analyze the cases that trigger a contradiction:

- Given two intervals $A, B$ with $A \cap B \neq \emptyset$, but $A_{min} \neq B_{min}$, this case is a contradiction.
- Given two intervals $A, B$ with $A \subset B$, but $A_{min} \neq B_{min}$, this case is also a contradiction.

After that, we can use a disjoint-set union to carry out the set operations. The data range is very large, and the answer only needs the earliest solution, so we can try binary search on the answer.

<!--more-->

## Code

```cpp
// P2898.cpp
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1000020;
const int INF = 1e9 + 1e8;
int n, q, mem[maxn];
struct RMQ
{
    int left, right, exp;
} seq[maxn], tmp[maxn];

bool cmp(RMQ r1, RMQ r2)
{
    if (r1.exp == r2.exp)
        return r1.left < r2.left;
    return r1.exp > r2.exp;
}

int find(int x)
{
    return (mem[x] == x) ? mem[x] : mem[x] = find(mem[x]);
}

bool check(int val)
{
    int l1 = 0, r1 = INF, l2 = INF, r2 = 0;
    // initialize the mem[]
    for (int i = 0; i <= n + 1; i++)
        mem[i] = i;
    for (int i = 1; i <= val; i++)
        tmp[i] = seq[i];
    sort(tmp + 1, tmp + 1 + val, cmp);
    int lval = tmp[1].exp;
    l1 = l2 = tmp[1].left, r1 = r2 = tmp[1].right;
    for (int i = 2; i <= val; i++)
        if (tmp[i].exp != lval)
        {
            int now = find(l2);
            if (find(l1) <= r1)
                while (now <= r2)
                    mem[now] = mem[now + 1], now = find(now + 1);
            else
                return false;
            lval = tmp[i].exp;
            l1 = l2 = tmp[i].left;
            r1 = r2 = tmp[i].right;
        }
        else
        {
            l1 = max(l1, tmp[i].left), r1 = min(r1, tmp[i].right);
            l2 = min(l2, tmp[i].left), r2 = max(r2, tmp[i].right);
            lval = tmp[i].exp;
            if (l1 > r1)
                return false;
        }
    if (find(l1) > r1)
        return false;
    return true;
}

int main()
{
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= q; i++)
        scanf("%d%d%d", &seq[i].left, &seq[i].right, &seq[i].exp);
    int l = 0;
    int r = q;
    while (l <= r)
    {
        int mid = ((l + r) >> 1);
        if (check(mid))
            l = mid + 1;
        else
            r = mid - 1;
    }
    if (l > q)
        printf("0");
    else
        printf("%d", l);
    return 0;
}
```
