Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has pxchanged? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Yes
No
Yes
Yes
No
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
- [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
- [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
- [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
- [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
- [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
题目链接:https://vjudge.net/problem/CodeForces-811B#author=ZhengruiOI
中文题意:
给定一个长为 n 的数列 P = [p1, p2, ..., pn]。现在对于这个数列有 m 组询问,每组询问给定三个整数 l,r,x ,表示询问将数列P区间 [l,r] 中的数从小到大排序后,原本在数列第 x 个位置的数是否还在原位。询问两两之间独立,也就是所有询问都是对初始状态的 P 数组进行操作。
思路:如果按照题目要求进行排序后进行比较是否更变了x位置的元素,那么处理后元素位置就变了,想进行下一次操作就要把排序过的数组恢复过来,
这样的时间复杂度是
m*n*(1+logn)
题目中的n和m的范围是1e4,
那么不算常数这也是一个1e9的算法,有特卡的数据显然是过不了的。
那么我们就要想另一种方法了。
每一次查询的时候不去排序,而是从l到r区间里寻找有多少个数比a[x]小。我们记下个数为cnt
如果l+cnt==x的话,就是yes,反而反之。
细节见我的AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), ' ', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== "<<x<<" =="<<endl; using namespace std; typedef long long ll; inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n,m; int a[maxn]; int b[maxn]; int l,r,x; int cnt; int main() { gg(n); gg(m); repd(i,1,n) { gg(a[i]); b[i]=a[i]; } repd(jj,1,m) { gg(l),gg(r),gg(x); // sort(a+l,a+1+r); cnt=0; repd(i,l,r) { if(a[i]<a[x]) { cnt++; } } if(l+cnt==x) { printf("Yes "); }else { printf("No "); } } return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == ' '); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }