SCU2016-02 S题 区间二分
2016-07-07 23:23
246 查看
Analyse:
把连续相等的数缩为一个节点,节点内有区间起点终点和值.
然后每次给了区间查寻之后去二分查找这个区间左端点,然后判断一下就可。
以上是自己YY的方法。
这个题的标准做法是,用并查集。
把连续相等的数合并为一组。
然后对于每个区间查寻去看左右端点是否在同一组,如果不是,那么久一定能找到。
如果是,看这个组的值的比较。
Get:
并查集在各大分组问题中有其效果,在缩集合的时候我们应该首先考虑使用并查集。
把连续相等的数缩为一个节点,节点内有区间起点终点和值.
然后每次给了区间查寻之后去二分查找这个区间左端点,然后判断一下就可。
以上是自己YY的方法。
这个题的标准做法是,用并查集。
把连续相等的数合并为一组。
然后对于每个区间查寻去看左右端点是否在同一组,如果不是,那么久一定能找到。
如果是,看这个组的值的比较。
Get:
并查集在各大分组问题中有其效果,在缩集合的时候我们应该首先考虑使用并查集。
/**********************jibancanyang************************** *Author* :jibancanyang *Created Time* : 三 7/ 6 10:20:37 2016 **Problem**: **Code**: ***********************1599664856@qq.com**********************/ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #include <stack> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; #define pr(x) cout << #x << ": " << x << " " #define pl(x) cout << #x << ": " << x << endl; #define pri(a) printf("%d\n",(a)) #define xx first #define yy second #define sa(n) scanf("%d", &(n)) #define sal(n) scanf("%lld", &(n)) #define sai(n) scanf("%I64d", &(n)) #define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++) const int mod = int(1e9) + 7, INF = 0x3f3f3f3f; const int maxn = 2e5 + 13; int n, m; int A[maxn]; pii B[maxn]; int main(void) { #ifdef LOCAL freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif while (~scanf("%d%d", &n, &m)) { int cnt = -1; for (int i = 1; i <= n; i++) { int x; sa(x); if (cnt == -1 || x != B[cnt].yy) { if (cnt != -1) B[cnt].xx = i - 1; A[++cnt] = i; B[cnt].yy = x; } } B[cnt].xx = n; while (m--) { int l, r, x; sa(l), sa(r), sa(x); int t = lower_bound(A, A + cnt + 1, l) - A; //pr(A[t]), pr(B[t].xx), pl(B[t].yy); if (l > A[cnt]) { pri(x == B[cnt].yy ? -1 : l); } else if (A[t] > r) { pri(B[t - 1].yy == x ? -1 : r); } else { if (A[t] == l) { if (t + 1 > cnt) pri(B[t].yy == x ? -1 : l); else { if (r >= A[t + 1]) { pri(x == B[t].yy ? A[t + 1] : l); } else pri(B[t].yy == x ? -1 : l); } } else { pri(B[t].yy == x ? B[t - 1].xx : A[t]); } } } } return 0; }
相关文章推荐
- Redis 连接
- Linux下修改mysql用户密码
- Android
- Redis 脚本
- Redis 事务
- Redis 发布订阅
- Unity如何获取鼠标当前帧和上一帧的屏幕坐标差
- 黑马程序员_Java基础_我的Day08学习笔记
- 分享(一部分)
- 关于css清除浮动
- Redis HyperLogLog
- Redis3Cluster的学习笔记
- Redis 有序集合(sorted set)
- HDU 1029 Ignatius and the Princess IV
- 移动端前端UI库—Frozen UI、WeUI、SUI Mobile
- iOS开发添加向上下左右四个方向的手势以及获取触摸点的坐标
- 16.13
- 概述反射和序列化
- 表空间的管理方式有哪几种,各有什么优劣?
- Redis 集合(Set)