有心对2025省选基础八月第一周T2——K个串(13719-kstring)
题意:
给定一个长度为
输入输出格式:
输入第一行两个整数
第二行
输出一个整数表示第
样例
输入
7 5
3 -2 1 2 2 1 3
输出
4
样例解释
以下标
以下标
以下标
以下标
以下标
以下标
以下标
此时,前
数据范围
对于
另有
对于
20 pts
(区分度是这样的())
考虑记录以
例如样例
显然,当删去第一个数时,以第
考虑通用转移,记录下标
考虑用线段树维护以每一个下标为开头的子串最大的贡献,将每一颗线段树的最大值取出用优先队列维护。每次取出一个数就将其在原线段树上删除,再在原线段树上取出删除后的最大值放入优先队列中。
显然开
总时间复杂度
对于仙丹树封装中:
ll modify(ll l,ll r,ll k)
为以最新一个版本的树为基础,将
ll askmax(ll root,ll l,ll r);
为查询以
ll askmaxl(ll root,ll l,ll r,ll k)
为查询以
(封装比较丑看接口就行)
主要代码:
const ll maxn=110000;
struct q4{
ll root;
ll max;
ll l,r;
bool operator<(const q4&b) const {
return max<b.max;
}
bool operator>(const q4&b) const {
return max>b.max;
}
};
ll n,k;
ll a[maxn];
ll next[maxn];
std::unordered_map<ll,ll>last;
ll r[maxn];
SegmentTree tr;
priority_queue<q4,std::vector<q4>,std::less<q4> >q;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%lld%lld",&n,&k);
tr.init(n);
for(ll i=1,sum=0;i<=n;i++){
next[i]=n+1;
scanf("%lld",&a[i]);
if(last.find(a[i])!=last.end())
next[last[a[i]]]=i;
else
sum+=a[i];
last[a[i]]=i;
tr.modify(i,i,sum);
}
r[0]=tr.modify(1,1,0);
q.push({r[0],tr.askmax(r[0],1,n),1,n});
for(ll i=1;i<=n;i++){
r[i]=tr.modify(i,next[i]-1,-a[i]);
q.push({r[i],tr.askmax(r[i],i+1,n),i+1,n});
}
for(ll i=1;i<k;i++){
q4 t=q.top();
q.pop();
ll mid=tr.askmaxl(t.root,t.l,t.r,t.max);
if(t.l<mid) q.push({t.root,tr.askmax(t.root,t.l,mid-1),t.l,mid-1});
if(mid<t.r) q.push({t.root,tr.askmax(t.root,mid+1,t.r),mid+1,t.r});
}
q4 t=q.top();
printf("%lld",t.max);
return 0;
}
完整代码:
(真动态开点仙丹树喵~)
#include <cstdio>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using std::max; using std::min; using std::priority_queue;
using std::cin; using std::cout; using std::cerr; using std::endl;
typedef long long ll;
const ll INFLL=1152921504606846976;
const ll ParallelArrayLen=128;
template<typename T>
class ParallelArray{
private:
std::vector<T*>d;
std::vector<ll>cnt;
public:
ParallelArray(){
d.push_back(new T[ParallelArrayLen]);
cnt.push_back(0);
}
ll push_back(T _d){
if(cnt[cnt.size()-1]>=ParallelArrayLen){
d.push_back(new T[ParallelArrayLen]);
cnt.push_back(0);
}
d[cnt.size()-1][cnt[cnt.size()-1]]=_d;
cnt[cnt.size()-1]++;
return cnt[cnt.size()-1]+(cnt.size()-1)*ParallelArrayLen-1;
}
ll size(){
return cnt[cnt.size()-1]+(cnt.size()-1)*ParallelArrayLen;
}
T& operator[](const ll& key){
// ll begk=key/ParallelArrayLen;
ll begk=key>>7;
ll endk=key-begk*ParallelArrayLen;
if(begk>=d.size()) return d[0][0];
return d[begk][endk];
}
};
struct SegmentTreeNode{
ll max,lazy;
ll lson,rson;
SegmentTreeNode() { max=0; lazy=0; lson=-1; rson=-1; }
};
class SegmentTree{
private:
ll n;
ParallelArray<SegmentTreeNode>t;
ParallelArray<ll>Root;
void updata(ll w){
ll lson=t[w].lson;
ll rson=t[w].rson;
t[w].max=0;
if(lson>0)
t[w].max=t[lson].max;
if(rson>0)
t[w].max=max(t[w].max,t[rson].max);
return;
}
void pushdown(ll w){
if(t[w].lazy==0) return;
ll lson=t[w].lson;
ll rson=t[w].rson;
if(lson>0){
int temp=t.push_back(SegmentTreeNode());
t[temp]=t[lson];
t[w].lson=temp;
t[temp].lazy+=t[w].lazy;
t[temp].max+=t[w].lazy;
}
if(rson>0){
int temp=t.push_back(SegmentTreeNode());
t[temp]=t[rson];
t[w].rson=temp;
t[temp].lazy+=t[w].lazy;
t[temp].max+=t[w].lazy;
}
t[w].lazy=0;
updata(w);
return;
}
void modify(ll l,ll r,ll L,ll R,ll k,ll w){
if(l<=L&&R<=r){
t[w].lazy+=k;
t[w].max+=k;
return;
}
pushdown(w);
ll lson=t[w].lson;
ll rson=t[w].rson;
ll mid=(L+R)>>1;
if(l<=mid){
ll temp=t.push_back(SegmentTreeNode());
if(lson>0)
t[temp]=t[lson];
t[w].lson=temp;
this->modify(l,r,L,mid,k,temp);
}
if(r>mid){
ll temp=t.push_back(SegmentTreeNode());
if(rson>0)
t[temp]=t[rson];
t[w].rson=temp;
this->modify(l,r,mid+1,R,k,temp);
}
updata(w);
return;
}
ll askmax(ll l,ll r,ll L,ll R,ll w){
if(l<=L&&R<=r)
return t[w].max;
pushdown(w);
ll lson=t[w].lson;
ll rson=t[w].rson;
ll mid=(L+R)>>1;
ll ret=-INFLL;
if(l<=mid&&lson>0)
ret=this->askmax(l,r,L,mid,lson);
if(r>mid&&rson>0)
ret=max(ret,this->askmax(l,r,mid+1,R,rson));
updata(w);
return ret;
}
ll askmaxl(ll l,ll r,ll L,ll R,ll k,ll w){
if(t[w].max<k)
return INFLL;
if(t[w].max==k&&L==R)
return L;
pushdown(w);
ll lson=t[w].lson;
ll rson=t[w].rson;
ll mid=(L+R)>>1;
ll ret=INFLL;
if(l<=mid&&lson>0){
ret=this->askmaxl(l,r,L,mid,k,lson);
if(ret<INFLL)
return ret;
}
if(r>mid&&rson>0)
return this->askmaxl(l,r,mid+1,R,k,rson);
return ret;
}
public:
void init(ll _n){
n=_n;
t.push_back(SegmentTreeNode());
Root.push_back(0);
for(ll i=1;i<=n;i++)
this->modify(i,i,1,n,0,0);
return;
}
ll modify(ll l,ll r,ll k){
ll temp=t.push_back(t[Root[Root.size()-1]]);
Root.push_back(temp);
this->modify(l,r,1,n,k,temp);
return temp;
}
ll askmax(ll root,ll l,ll r){
return this->askmax(l,r,1,n,root);
}
ll askmaxl(ll root,ll l,ll r,ll k){
return this->askmaxl(l,r,1,n,k,root);
}
};
const ll maxn=110000;
struct q4{
ll root;
ll max;
ll l,r;
bool operator<(const q4&b) const {
return max<b.max;
}
bool operator>(const q4&b) const {
return max>b.max;
}
};
ll n,k;
ll a[maxn];
ll next[maxn];
std::unordered_map<ll,ll>last;
ll r[maxn];
SegmentTree tr;
priority_queue<q4,std::vector<q4>,std::less<q4> >q;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%lld%lld",&n,&k);
tr.init(n);
for(ll i=1,sum=0;i<=n;i++){
next[i]=n+1;
scanf("%lld",&a[i]);
if(last.find(a[i])!=last.end())
next[last[a[i]]]=i;
else
sum+=a[i];
last[a[i]]=i;
tr.modify(i,i,sum);
}
r[0]=tr.modify(1,1,0);
q.push({r[0],tr.askmax(r[0],1,n),1,n});
for(ll i=1;i<=n;i++){
r[i]=tr.modify(i,next[i]-1,-a[i]);
q.push({r[i],tr.askmax(r[i],i+1,n),i+1,n});
}
for(ll i=1;i<k;i++){
q4 t=q.top();
q.pop();
ll mid=tr.askmaxl(t.root,t.l,t.r,t.max);
if(t.l<mid) q.push({t.root,tr.askmax(t.root,t.l,mid-1),t.l,mid-1});
if(mid<t.r) q.push({t.root,tr.askmax(t.root,mid+1,t.r),mid+1,t.r});
}
q4 t=q.top();
printf("%lld",t.max);
return 0;
}
(如果有看不懂的可以私信询问)
(神秘 45 pts 做法)
我们考虑并不一定要枚举所有的子串才能找到最大值,更早开头的似乎会更有优势(尤其对于
赛事代码(节选):
const ll maxn=100010;
const ll maxtime=4800000;
int n,k,sk;
int a[maxn],b[maxn];
priority_queue<ll,vector<ll>,std::greater<ll> >q;
map<int,int>m;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
m[a[i]]=1;
map<int,int>::iterator it=m.begin();
int cnt=1;
while(it!=m.end()){
it->second=cnt;
it++; cnt++;
}
for(int i=1;i<=n;i++)
b[i]=m[a[i]];
for(int i=1;i<=n;i++){
if(clock()>maxtime) break;
bitset<maxn>cnt;
cnt.reset();
ll sum=0;
for(int j=i;j<=n;j++){
if(cnt[b[j]]==0)
sum+=a[j];
cnt[b[j]]=1;
q.push(sum);
if(q.size()>k) q.pop();
}
}
printf("%lld",q.top());
return 0;
}