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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于微信小程序的家政服务预约系统采用PHP语言和微信小程序技术,数据库采用Mysql,运行软件为微信开发者工具。本系统实现了管理员和客户、员工三个角色的功能。管理员的功能为客户管理、员工管理、家政服务管理、服务预约管理、员工风采管理、客户需求管理、接单管理等。客户的功能为查看家政服务进行预约和发布自己的需求以及管理预约信息和接单信息等。员工可以查看预约信息和进行接单。本系统实现了网上预约家政服务的流程化管理,可以帮助工作人员的管理工作和帮助客户查询家政服务的相关信息,改变了客户找家政服务的方式,提高了预约家政服务的效率。 本系统是针对网上预约家政服务开发的工作管理系统,包括到所有的工作内容。可以使网上预约家政服务的工作合理化和流程化。本系统包括手机端设计和电脑端设计,有界面和数据库。本系统的使用角色分为管理员和客户、员工三个身份。管理员可以管理系统里的所有信息。员工可以发布服务信息和查询客户的需求进行接单。客户可以发布需求和预约家政服务以及管理预约信息、接单信息。 本功能可以实现家政服务信息的查询和删除,管理员添加家政服务信息功能填写正确的信息就可以实现家政服务信息的添加,点击家政服务信息管理功能可以看到基于微信小程序的家政服务预约系统里所有家政服务的信息,在添加家政服务信息的界面里需要填写标题信息,当信息填写不正确就会造成家政服务信息添加失败。员工风采信息可以使客户更好的了解员工。员工风采信息管理的流程为,管理员点击员工风采信息管理功能,查看员工风采信息,点击员工风采信息添加功能,输入员工风采信息然后点击提交按钮就可以完成员工风采信息的添加。客户需求信息关系着客户的家政服务预约,管理员可以查询和修改客户需求信息,还可以查看客户需求的添加时间。接单信息属于本系统里的核心数据,管理员可以对接单的信息进行查询。本功能设计的目的可以使家政服务进行及时的安排。管理员可以查询员工信息,可以进行修改删除。 客户可以查看自己的预约和修改自己的资料并发布需求以及管理接单信息等。 在首页里可以看到管理员添加和管理的信息,客户可以在首页里进行家政服务的预约和公司介绍信息的了解。 员工可以查询客户需求进行接单以及管理家政服务信息和留言信息、收藏信息等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值