前置芝士:莫队、(正常解法:值域分块)
由于是366ms题解,所以还有二分,但是应该不会有人不会二分吧
由于这篇题解主要讲解的是优化,所以这里现附上我的第一版AC代码
#include <bits/stdc++.h>
using namespace std;
int n,m,a[100005],ans[100005],cntid[100005],cntht[100005],cnthtr[325],awa[200005],res = 0,subr[100005],sz;
unordered_map<int,int> vis;
struct query{
int l,r,k,id;
}q[100005];
void add(int x){
cntht[cntid[a[x]]]--;
cnthtr[subr[cntid[a[x]]]]--;
cntht[++cntid[a[x]]]++;
cnthtr[subr[cntid[a[x]]]]++;
}
void sub(int x){
cnthtr[subr[cntid[a[x]]]]--;
cntht[cntid[a[x]]--]--;
cnthtr[subr[cntid[a[x]]]]++;
cntht[cntid[a[x]]]++;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int __T__ = 1;
#ifdef debug
cin >> __T__;
#endif
while(__T__--){
cin >> n >> m;
sz = sqrt(n);
res = n;
for(int i = 1;i<=n;i++){
cin >> a[i],subr[i] = (i-1)/sz+1;
if(!vis[a[i]]) awa[++awa[0]] = a[i];
}
sort(awa+1,awa+1+awa[0]);
for(int i = 1;i<=awa[0];i++) vis[awa[i]] = i;
for(int i = 1;i<=n;i++) a[i] = vis[a[i]];
for(int i = 1;i<=m;i++){
cin >> q[i].l >> q[i].r >> q[i].k;
q[i].id = i;
}
sort(q+1,q+1+m,[](query a,query b){
if(subr[a.l]^subr[b.l]) return subr[a.l]<subr[b.l];
return a.r<b.r;
});
int l = 1,r = 0;
for(int i = 1;i<=m;i++){
if(q[i].l>r){
while(l<=r) sub(l++);
l = q[i].l,r = q[i].l-1;
}
while(l<q[i].l) sub(l++);
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(r>q[i].r) sub(r--);
int j;
for(j = 1;q[i].k>cnthtr[j]&&j<=320;j++) q[i].k-=cnthtr[j];
if(j==321){
ans[q[i].id] = -1;
continue;
}
j = (j-1)*sz+1;
for(;q[i].k>cntht[j];j++) q[i].k-=cntht[j];
ans[q[i].id] = j;
}
for(int i = 1;i<=m;i++) cout << ans[i] << "\n";
}
return 0;
}我们先来分析一下整个程序的复杂度,莫队是 的,但是前提是单次移动操作为 当莫队移动操作为 时,莫队的复杂度就会翻 倍,所以我们优先从移动操作下手
现在的移动操作是这样的:
void add(int x){
cntht[cntid[a[x]]]--;
cnthtr[subr[cntid[a[x]]]]--;
cntht[++cntid[a[x]]]++;
cnthtr[subr[cntid[a[x]]]]++;
}
void sub(int x){
cnthtr[subr[cntid[a[x]]]]--;
cntht[cntid[a[x]]--]--;
cnthtr[subr[cntid[a[x]]]]++;
cntht[cntid[a[x]]]++;
}我们的 cntht 指的是当前热度的股票种数
考虑换一种意义使得 和 函数的操作数更少
将 cntht 作为大于或等于当前热度的股票种数,此时 cntht 就变成了单调递增的,我们就可以同时优化下面的值域分块代码,变成寻找第一个股票种数小于 的热度后
这里举一个例子,比如有一个股票热度为 ,且只有这个股票,那么如果是小于或等于 ,就会搜到 ,然而并没有热度为 的股票,因为我们的 cntht 变成了前缀和,cntht 值相等的区间只有最右边的端点是有的,所以要取小于 的再加一
最后再加上一点卡常的优化,就有了下面的最优解代码:
#include <bits/stdc++.h>
using namespace std;
#define BUF_SIZE 50005
char readbuf[BUF_SIZE], printbuf[BUF_SIZE], *readp0, *readp1, *printp0 = printbuf,resbf[15];
int printtot = -1;
#define getchar() (readp0 == readp1 && (readp1 = (readp0 = readbuf) + fread(readbuf, 1, BUF_SIZE, stdin), readp0 == readp1) ? EOF : *readp0++)
#define putchar(x) ((printp0 == printbuf + BUF_SIZE) && (fwrite(printbuf, printp0 - printbuf, 1, stdout), printp0 = printbuf), *printp0++ = (x))
#define endputchar() fwrite(printbuf, printp0 - printbuf, 1, stdout)
class QReadPrint {
public:
~QReadPrint () { endputchar(); }
} qreadprint;
inline int read () {
char ch;
while (ch = getchar(), ch < '0' || ch > '9');
int num = ch - '0';
for (; ch = getchar(), '0' <= ch && ch <= '9'; num = (num << 3) + (num << 1) + ch - '0');
return num;
}
int n,m,a[100005],ans[100005],cntid[100005],cntht[100005],awa = 0,res = 0,subr[100005],sz;
unordered_map<int,int> vis;
struct query{
int l,r,k,id;
}q[100005];
inline void add(int x){
cntht[++cntid[a[x]]]++;
}
inline void sub(int x){
cntht[cntid[a[x]]--]--;
}
signed main(){
ios::sync_with_stdio(0);
n = read(),m = read();
sz = (double)n/(double)sqrt(m);
res = cntht[0] = n;
for(register int i = 1;i<=n;++i){
subr[i] = (i-1)/sz+1;
if(!vis[a[i] = read()]) vis[a[i]] = ++awa;
a[i] = vis[a[i]];
}
for(register int i = 1;i<=m;++i){
q[i].l = read(),q[i].r = read(),q[i].k = read();
q[i].id = i;
}
sort(q+1,q+1+m,[](query a,query b)->bool{
return (subr[a.l]^subr[b.l])?subr[a.l]<subr[b.l]:((subr[a.l]&1)?(a.r<b.r):(a.r>b.r));
});
int l = 1,r = 0;
for(register int i = 1;i<=m;++i){
if(q[i].l>r){
while(l<=r) sub(l++);
l = q[i].l,r = q[i].l-1;
}
while(l<q[i].l) sub(l++);
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(r>q[i].r) sub(r--);
int tmp = lower_bound(cntht,cntht+2+n,cntht[1]-q[i].k,greater<int>())-cntht-1;
if(tmp>n) tmp = -1;
ans[q[i].id] = tmp;
}
for(register int i = 1;i<=m;++i){
if(~ans[i]){
for (; ans[i]; resbf[++printtot] = (ans[i] % 10)+'0', ans[i] /= 10);
for (++printtot; ~printtot; putchar(resbf[--printtot]));
}else{
putchar('-'),putchar('1');
}
putchar('\n');
}
return 0;
}