小P在课堂上课,老师留给他一个简单的问题:
有 n 条木棍,第 i 条木棍的长度是 ai。总共有 q 次询问,每次询问给定 l 和 r ,问从[l,r] 这个区间里的木棍中,能不能选出 4 根木棍组成一个长方形.
现在快要下课了,小P想尽快解决这个问题,希望你可以帮助他
自己敲得代码总是错。。。只好看大佬的代码,然后自己加上离散化的部分
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define mod 1000000007
#define Pi acos(-1.0)
#define eps 1e-8
#define N 200020
#define M 10000020
int cnt[M], a[N], ans[N];
int b[N];
int n, q;
int main(){
int cas;
scanf("%d", &cas);
while(cas--){
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
cnt[a[i]] = 0;
b[i]=a[i];
}
sort(b+1,b+1+n);
int l = 0, r = 1, cnt2 = 0, cnt4 = 0;
for(l = 1; l <= n; ++l){
int ll=lower_bound(b+1,b+1+n,a[l])-b;
while(r <= n && (cnt2 < 2 && cnt4 < 1)){
int rr=lower_bound(b+1,b+1+n,a[r])-b;
++cnt[rr];
if(cnt[rr] == 2) cnt2++;
if(cnt[rr] == 4) cnt4++;
++r;
}
if(cnt2 >= 2 || cnt4 >= 1) ans[l] = r - 1;
else ans[l] = n + 1;
cnt[ll]--;
if(cnt[ll] == 1) cnt2--;
if(cnt[ll] == 3) cnt4--;
}
while(q--){
scanf("%d%d", &l, &r);
if(ans[l] <= r) puts("YES");//学学puts吧。。
else puts("NO");
}
}
}