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\n", ans);
109 }
110 int main() {
111     work();
112     return 0;
113 }

转载于:https://www.cnblogs.com/NaVi-Awson/p/7745103.html

[USACO 08JAN]Haybale Guessing相关推荐

  1. 线段树 + 二分答案:Haybale Guessing G

    参考文献:题解 P2898 [[USACO08JAN]haybale猜测Haybale Guessing] - レムの小屋 - 洛谷博客 题目链接:[USACO08JAN]Haybale Guessi ...

  2. POJ Haybale Guessing

    Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...

  3. Haybale Guessing (POJ-3657)

    Problem Description The cows, who always have an inferiority complex about their intelligence, have ...

  4. P2898 [USACO08JAN]haybale猜测Haybale Guessing

    好题. 搬运一下luogu的题解, 讲的挺清楚的. 题意:给出一些区间的最小值 求问 最早哪个问题会产生矛盾 输出 我们可以二分判断 哪个地方最早出现矛盾 然后每次针对二分的一个值 我去判断一下是否成 ...

  5. 《题目与解读》红书 训练笔记目录《ACM国际大学生程序设计竞赛题目与解读》

    虽然2012年出版的老书了,但是是由三次世界冠军的上海交大ACM队出版的书籍,选择的题目是ACM经典中的经典,书中有非常详细的题解,可以学到很多东西,值得一刷. 目录 第一部分 第一章 数学 1.1 ...

  6. 树形结构 —— 并查集

    [概述] 并查集(Union-Find Set)是一种用于分离集合操作的抽象数据类型,其处理的是集合(set)之间的关系,一般处理的是图的连通分量,当给出两个的元素的一个无序对 (a,b) 时,需要快 ...

  7. jzoj 1594: 【USACO】The Chivalrous Cow(骑士牛)( 待加入)

    1594: [USACO] 题目描述 Farmer John traded one of his cows for a cow that Farmer Don called 'The Knight' ...

  8. 如何准备好2023年的USACO?

    目录 1. 注册 2. 刷题 3. 备考 4. 考试流程/介绍 5. 铜组例题 关于usaco usaco是美国中学生的官方竞赛网站.是美国著名在线题库,专门为信息学竞赛选手准备.推荐直接阅读英语原文 ...

  9. usaco Shaping Regions

    这就是usaco 前面的windows area的变形. /* ID:jinbo wu TASK:rect1 LANG:C++ */ #include<iostream> #include ...

最新文章

  1. pytorch的一些函数
  2. Maven构建java项目
  3. RTX Server SDK跨服务器
  4. 另一种launch SAP CRM AET的方式
  5. 多线程—Thread类及线程三种创建方式及对比
  6. .Net Core 认证系统之基于Identity Server4 Token的JwtToken认证源码解析
  7. MongoDB的可视化工具之Navicat
  8. Spring Boot(3)---Spring Boot入门:系统要求
  9. WORD2010每次启动都要配置
  10. oracle数据库给用户解锁和修改密码和提升权限的命令
  11. php cms 新闻采集,自动新闻采集软件快速入门图文详细教程
  12. Java概述、Jdk的安装、关键字
  13. Scott Page  斯科特佩奇《模型思维》读书笔记
  14. 如何使用Keil5开发MSP430及Tiva系列开发板
  15. python基础教程:__call__用法
  16. 岁月如水-指间流逝-难觅难留
  17. JS实现简易图片时钟效果
  18. 网站进入前10名的需要的操作
  19. win11怎么升级更新显卡驱动
  20. 如何设计帮助中心才能真正地帮助客户解决问题?

热门文章

  1. MYSQL注入天书之order by后的injection
  2. [Linux]history 显示命令执行的时间
  3. RHCE笔记1-安裝
  4. Flash Builder4.7极其简单破解方法-三步搞定(亲测)
  5. sqlserver2000分页存储过程(原创)
  6. 关于Java API不能远程访问HBase的问题
  7. 剑指offer(Java实现) 顺时针打印矩阵
  8. Linux操作系统Ubuntu部署GCC之Libpcap库篇
  9. matplotlib更改networkx生成的图形的背景图。
  10. UBOOT添加命令的执行流程