• [USACO 08JAN]Haybale Guessing


    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 Ql..Qh (1 ≤ Ql ≤ N; Ql ≤ 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.

    给一段长度为n,每个位置上的数都不同的序列a[1..n]和q和问答,每个问答是(x, y, r)代表RMQ(a, x, y) = r, 要你给出最早的有矛盾的那个问答的编号。

    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

    题解

    二分求解。

    二分答案,将答案范围内的最小值$Ai$进行降序排序 然后我们可以观察一下得到的这些区间

    对于不同$Ai$想一想如果它被之前出现的区间(比它大的$Ai$)都覆盖了,那么肯定就是有矛盾的

    给点提示:对于同样的$Ai$询问要用交集,覆盖要用并集

    这样就可以很明显地用线段树来搞了

      1 //It is made by Awson on 2017.10.27
      2 #include <set>
      3 #include <map>
      4 #include <cmath>
      5 #include <ctime>
      6 #include <queue>
      7 #include <stack>
      8 #include <vector>
      9 #include <cstdio>
     10 #include <string>
     11 #include <cstdlib>
     12 #include <cstring>
     13 #include <iostream>
     14 #include <algorithm>
     15 #define LL long long
     16 #define Max(a, b) ((a) > (b) ? (a) : (b))
     17 #define Min(a, b) ((a) < (b) ? (a) : (b))
     18 #define Lr(o) (o<<1)
     19 #define Rr(o) (o<<1|1)
     20 using namespace std;
     21 const int N = 1000000;
     22 const int INF = ~0u>>1;
     23 
     24 int n, q;
     25 struct tt {
     26     int l, r, a;
     27 } a[N+5], b[N+5];
     28 bool comp(const tt &a, const tt &b) {
     29     if (a.a != b.a) return a.a > b.a;
     30     return a.l == b.l ? a.r < b.r : a.l < b.l;
     31 }
     32 struct segment {
     33     int sgm[(N<<2)+5], lazy[(N<<2)+5];
     34     void build(int o, int l, int r) {
     35         lazy[o] = 0;
     36         if (l == r) {
     37             sgm[o] = INF; return;
     38          }
     39          int mid = (l+r)>>1;
     40          build(Lr(o), l, mid);
     41          build(Rr(o), mid+1, r);
     42          sgm[o] = Max(sgm[Lr(o)], sgm[Rr(o)]);
     43     }
     44     void pushdown(int o) {
     45         if (lazy[o]) {
     46             sgm[Lr(o)] = sgm[Rr(o)] = lazy[Lr(o)] = lazy[Rr(o)] = lazy[o];
     47             lazy[o] = 0;
     48         }
     49     }
     50     void update(int o, int l, int r, int a, int b, int key) {
     51         if (a <= l && r <= b) {
     52             sgm[o] = lazy[o] = key; return;
     53         }
     54         pushdown(o);
     55         int mid = (l+r)>>1;
     56         if (a <= mid) update(Lr(o), l, mid, a, b, key);
     57         if (mid < b) update(Rr(o), mid+1, r, a, b, key);
     58          sgm[o] = Max(sgm[Lr(o)], sgm[Rr(o)]);
     59     }
     60     int query(int o, int l, int r, int a, int b) {
     61         if (a <= l && r <= b) return sgm[o];
     62         pushdown(o);
     63         int mid = (l+r)>>1;
     64         int a1 = 0, a2 = 0;
     65         if (a <= mid) a1 = query(Lr(o), l, mid, a, b);
     66         if (mid < b) a2 = query(Rr(o), mid+1, r, a, b);
     67         return Max(a1, a2);
     68     }
     69 }T;
     70 
     71 bool get(int l, int r, int &x, int &y) {
     72     int ll = b[l].l, rr = b[l].r;
     73     for (int i = l+1; i <= r; i++) {
     74         int lll = b[i].l, rrr = b[i].r;
     75         if (rr < lll) return false;
     76         ll = lll;
     77     }
     78     x = ll, y = rr;
     79     return true;
     80 }
     81 bool judge(int mid) {
     82     T.build(1, 1, n);
     83     for (int i = 1; i <= mid; i++) b[i] = a[i];
     84     sort(b+1, b+1+mid, comp);
     85     for (int i = 1; i <= mid; i++) {
     86         int loc, l, r;
     87         for (loc = i; loc <= mid; loc++) if (b[loc].a != b[i].a) break;
     88         loc--;
     89         if (!get(i, loc, l, r)) return false;
     90         int t = T.query(1, 1, n, l, r);
     91         if (t != INF && t != b[i].a) return false;
     92         for (int k = i; k <= loc; k++)
     93             T.update(1, 1, n, b[k].l, b[k].r, b[k].a);
     94         i = loc;
     95     }
     96     return true;
     97 }
     98 void work() {
     99     scanf("%d%d", &n, &q);
    100     for (int i = 1; i <= q; i++)
    101         scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].a);
    102     int L = 1, R = q, ans = 0;
    103     while (L <= R) {
    104         int mid = (L+R)>>1;
    105         if (judge(mid)) L = mid+1;
    106         else R = mid-1, ans = mid;
    107     }
    108     printf("%d
    ", ans);
    109 }
    110 int main() {
    111     work();
    112     return 0;
    113 }
  • 相关阅读:
    【bzoj2724】[Violet 6]蒲公英 分块+STL-vector
    【bzoj4026】dC Loves Number Theory 可持久化线段树
    【bzoj3744】Gty的妹子序列 分块+树状数组+主席树
    【bzoj3166】[Heoi2013]Alo 可持久化Trie树+STL-set
    【bzoj3060】[Poi2012]Tour de Byteotia 并查集
    【bzoj3510】首都 LCT维护子树信息(+启发式合并)
    【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息
    【bzoj3261】最大异或和 可持久化Trie树
    【bzoj2081】[Poi2010]Beads Hash
    【bzoj4260】Codechef REBXOR Trie树
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7745103.html
Copyright © 2020-2023  润新知