2024 CCPC Liaoning Provincial Contest——G. 顾影自怜(线段树上二分) · znzryb
## 思路讲解
<!-- Notion block type: unsupported -->
**线段树上二分**,其实还是挺简单的,跑的也比较快。
然后也不用很固执的不用递归,zkw确实好,但是递归的线段树上二分更好理解,跑的也比较快。
```cpp
// 找到最靠近r的符合位置
template<class F> int findl(int r,F&&fun){
return findl(1,r,0,sz-1,fun);
}
// 找到最靠近r的符合位置
template<class F> int findl(int o,int qr,int l,int r,F&&fun){
if(l==r){
return l;
}
// pushdown(o);
ll ls=o<<1,rs=ls|1;
ll mid=l+r>>1;
ll pos=-1;
if(fun(tr[rs]) && mid+1<=qr) pos=findl(rs,qr,mid+1,r,fun);
if(pos!=-1) return pos;
if(fun(tr[ls])) pos=findl(ls,qr,l,mid,fun);
return pos;
}
// 找最靠近l的符合位置
template<class F> int findr(int l,F&&fun){
return findr(1,l,0,sz-1,fun);
}
// 找最靠近l的符合位置
template<class F> int findr(int o,int ql,int l,int r,F&&fun){
if(l==r){
return l;
}
// pushdown(o);
ll ls=o<<1,rs=ls|1;
ll mid=l+r>>1;
ll pos=-1;
if(fun(tr[ls]) && mid>=ql) pos=findr(ls,ql,l,mid,fun);
if(pos!=-1) return pos;
if(fun(tr[rs])) pos=findr(rs,ql,mid+1,r,fun);
return pos;
}
```
## AC代码
[https://qoj.ac/submission/1263129](https://qoj.ac/submission/1263129)
::: 源代码
```cpp
// by Gospel_rock
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <vector>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<DB>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
inline void __();
// 来自wida github仓库 https://github.com/hh2048/XCPC/tree/main 加了一些新功能
//46 ms https://acm.hdu.edu.cn/contest/view-code?cid=1179&rid=14912 读入字符getc(a) 的AC记录
// AC https://vjudge.net/solution/63197697 一道测试读写的codechef题目
namespace QuickRead { // 快读
char buf[1 << 22], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
template <typename T> void Cin(T &a) {
T ans = 0;
bool f = 0;
char c = getc();
for (; c < '0' || c > '9'; c = getc()) {
if (c == '-') f = 1;
}
for (; c >= '0' && c <= '9'; c = getc()) {
ans = ans * 10 + c - '0';
}
a = f ? -ans : ans;
}
void getc(char &a){
char c=getc();
while (isspace(c)) {
c=getc();
}
a=c;
}
template <typename T, typename... Args> void Cin(T &a, Args &...args) {
Cin(a), Cin(args...);
}
// ===== 输出 =====
static constexpr size_t OUT_CAP = 1 << 22; // 大概是2MB
char obuf[OUT_CAP];
size_t op = 0;
inline void flush() {
if (op) {
fwrite(obuf, 1, op, stdout);
op = 0;
}
}
inline void putc(char c) {
if (op == OUT_CAP) flush();
obuf[op++] = c;
}
template <typename T> void write(T x) { // 注意,这里输出不带换行
if (x < 0) putc('-'), x = -x;
if (x > 9) write(x / 10);
putc(x % 10 + '0');
}
struct Flusher { // 析构时自动刷新
~Flusher() { flush(); }
} flusher;
} // namespace QuickRead
using namespace QuickRead;
// zkw线段树,开3倍空间的LOG
vector<int> LOG;
inline void initlog(ll n){
n+=5;
LOG.resize(n+5);
LOG[0]=-1;
for(int i=1;i<=n;++i){
LOG[i]=LOG[i>>1]+1;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
initlog(3e6);
Cin(lT);
while(lT--)
__();
return 0;
}
/*
*/
// 参考zkw的PPT 以及 https://www.luogu.com.cn/record/207546670
// 参考题解 https://www.luogu.com.cn/article/w3gfeebh
// AC https://www.luogu.com.cn/record/232824635 RMQ
// AC https://acm.hdu.edu.cn/contest/view-code?cid=1178&rid=19433 6s 以内
// AC https://qoj.ac/submission/1263129 线段树上二分
constexpr ll preset=INF;
struct SEG{
static ll op(const ll &a,const ll &b){
return max(a,b);
}
int n,sz=1,dep;
vector<ll> tr;
// vector<ll> tag;vector<bool> fixed;
SEG(int n){ // 传入的时候可以传大一点
this->n=n+2;
dep=__lg(this->n)+1;
sz=1<<dep;
tr.resize((sz<<1)+5,preset);
// tag.resize((sz<<1)+5,0);
// fixed.resize((sz<<1)+5,false);
}
void pushup(int o){tr[o]=op(tr[o<<1],tr[o<<1|1]);}
int dis(int o){return dep-LOG[o];}
int len(int o){return 1<<dis(o);}
int le(int o){return (o<<dis(o))-sz;}
int rr(int o){return (o+1<<dis(o))-sz-1;}
// void settag(int o,ll x){
// tr[o]+=x;
// if(!fixed[o]) tag[o]+=x;
// }
// void settag2(int o,ll x){ // 覆盖
// tr[o]=x;
// tag[o]=0;
// fixed[o]=true;
// }
// void pushdown(int o){
// int l=o<<1,r=l|1; // 左儿子,右儿子
// if(fixed[o]){
// settag2(l, tr[o]);
// settag2(r, tr[o]);
// fixed[o]=false;
// }
// if(tag[o]){
// settag(l, tag[o]);
// settag(r, tag[o]);
// tag[o]=0;
// }
// }
// void push(int l,int r){
// // 在j层,l,r变为了同一点
// int i=dep,j=__lg(l^r);
// while (i>j) { // 就是为了避免一下重复下推
// pushdown(l>>i--);
// }
// while(i){
// pushdown(l>>i),pushdown(r>>i--);
// }
// }
void assign(int i,ll x){
tr[i+sz]=x;
}
void build(){
for(int o=sz-1;o>=1;--o){
pushup(o);
}
}
ll query(int l,int r){
ll res=preset;
// 变为开区间,并加上sz
l=l-1+sz,r=r+1+sz;
// push(l,r);
for(;l^r^1;l>>=1,r>>=1){
// l^r^1 表示 只要 l 与 r 还不是同一个父亲的两兄弟,就继续上跳。
if(~l&1){
// l是偶数,说明l是父节点的左儿子,开区间,将其右儿子加入答案
res=op(res,tr[l^1]);
}
if(r&1){
res=op(tr[r^1],res);
}
}
return res;
}
// void add(int l,int r,ll x){
// int w=1;
// l=l-1+sz,r=r+1+sz;
// push(l, r);
// for(;l^r^1;pushup(l>>=1),pushup(r>>=1),w<<=1){
// if(~l&1){
// settag(l^1, x);
// }
// if(r&1){
// settag(r^1, x);
// }
// }
// for (l >>= 1; l; l >>= 1) pushup(l);
// }
// void modify(int l,int r,ll x){
// int w=1;
// l=l-1+sz,r=r+1+sz;
// push(l, r);
// for(;l^r^1;pushup(l>>=1),pushup(r>>=1),w<<=1){
// if(~l&1){
// settag2(l^1, x);
// }
// if(r&1){
// settag2(r^1, x);
// }
// }
// for (l >>= 1; l; l >>= 1) pushup(l);
// }
// 找到最靠近r的符合位置
template<class F> int findl(int r,F&&fun){
return findl(1,r,0,sz-1,fun);
}
// 找到最靠近r的符合位置
template<class F> int findl(int o,int qr,int l,int r,F&&fun){
if(l==r){
return l;
}
// pushdown(o);
ll ls=o<<1,rs=ls|1;
ll mid=l+r>>1;
ll pos=-1;
if(fun(tr[rs]) && mid+1<=qr) pos=findl(rs,qr,mid+1,r,fun);
if(pos!=-1) return pos;
if(fun(tr[ls])) pos=findl(ls,qr,l,mid,fun);
return pos;
}
// 找最靠近l的符合位置
template<class F> int findr(int l,F&&fun){
return findr(1,l,0,sz-1,fun);
}
// 找最靠近l的符合位置
template<class F> int findr(int o,int ql,int l,int r,F&&fun){
if(l==r){
return l;
}
// pushdown(o);
ll ls=o<<1,rs=ls|1;
ll mid=l+r>>1;
ll pos=-1;
if(fun(tr[ls]) && mid>=ql) pos=findr(ls,ql,l,mid,fun);
if(pos!=-1) return pos;
if(fun(tr[rs])) pos=findr(rs,ql,mid+1,r,fun);
return pos;
}
};
struct lr{
ll val,l,r,i;
bool operator<(const lr&other) const{
if(val!=other.val) return val<other.val;
if(l!=other.l) return l<other.l;
if(r!=other.r) return r<other.r;
return i<other.i;
}
lr(ll val,ll l,ll r,ll i):val(val),l(l),r(r),i(i) {}
};
inline void __(){
Cin(N,K);
vll A(N+5);
vll cnt(N+5);
SEG tr(N);
FOR(i,1,N){
Cin(A[i]);
cnt[A[i]]++;
tr.assign(i, A[i]);
}
tr.build();
vll ls;
// vb valid(N+5);
// ll validc=0;
FOR(i,1,N){
if(cnt[A[i]]>=K){
ls.EB(i);
// valid[i]=true;
// validc++;
}
}
if(ls.empty()){
write(0);putc('\n');
return;
}
vector<lr> a;
for(auto &i:ls){
ll l=tr.findl(i,[&](ll val){
if(val>A[i]) return true;
return false;
})+1;
ll r=tr.findr(i,[&](ll val){
if(val>A[i]) return true;
return false;
})-1;
#ifdef LOCAL
debug(l);debug(r);debug(i);
cend;
#endif
a.EB(A[i],l,r,i);
}
ll ans=0;
vector<ll> tmp;
ll lt=0,rt=0;
sort(all(a));
a.EB(0,0,0,0);
for(auto&[val,l,r,i]:a){
if(tmp.empty()){
lt=l;rt=r;
tmp.EB(i);
}else if(lt==l && rt==r){
tmp.EB(i);
}else{
if(SZ(tmp)>=K){
FOR(j,0,SZ(tmp)-1){
if(j+K-1>=SZ(tmp)) break;
ll idx=tmp[j]-(j==0?lt-1:tmp[j-1]);
ll num=rt-tmp[j+K-1]+1;
ans+=idx*num;
}
}
tmp.clear();
lt=l;rt=r;
tmp.EB(i);
// #ifdef LOCAL
// debug(i);
// debug(ans);
// #endif
}
}
write(ans);putc('\n');
}
/*
1
3 2
2 1 1
1
AC
https://qoj.ac/submission/1263129
更快
AC
https://qoj.ac/submission/1263556
AC
https://codeforces.com/gym/105481/submission/335602444
*/
```
:::
## 心路历程(WA,TLE,MLE……)