神奇 DS 题。
提供一个与众不同的大常数解法!!!
题目分析
根据生活经验,优先做 小的任务一定不劣。
证明:设 ,则先做第 个任务的时间为 ,先做第 个任务的时间为 。显然 。证毕。
注意到做任务的时间段可能会连在一起形成一个大段,而只有最后一个大段会影响答案。设排好序后最后一个大段从 开始,则答案为 。
记 。考虑这个 有什么特别之处。摆结论: 是所有 中最大的。
证明:对于 ,一定有 ,因为其中存在空的部分,如果计算的时候把它去掉了必定会使其减小。对于 ,一定有 ,因为去掉第一段后可能导致后面的前移,会使其减小。
考虑用平衡树维护。对每个节点维护 ,子树内的 的最大值,子树内的总和。push_up 应该比较简单了,详见代码实现。
AC Code
by wangyizhi(571247)
#include<bits/stdc++.h>
//#include<bits/extc++.h>
using namespace std;
using ll=long long;
using ld=long double;
//#define int ll
using pii=pair<int,int>;
//bool Mst;
const int V=2e6+5;
mt19937 rnd(time(nullptr));
struct piii
{
ll fi,se,th;
};
inline piii nxt(piii x){return {x.fi,x.se,x.th+1};}
inline piii pre(piii x){return {x.fi,x.se,x.th-1};}
bool operator==(piii x,piii y){return x.fi==y.fi&&x.se==y.se&&x.th==y.th;}
bool operator<(piii x,piii y)
{
if(x.fi!=y.fi) return x.fi<y.fi;
if(x.se!=y.se) return x.se<y.se;
return x.th<y.th;
}
bool operator>(piii x,piii y)
{
if(x.fi!=y.fi) return x.fi>y.fi;
if(x.se!=y.se) return x.se>y.se;
return x.th>y.th;
}
bool operator<=(piii x,piii y){return x==y||x<y;}
bool operator>=(piii x,piii y){return x==y||x>y;}
struct treap
{
struct node
{
node *ch[2];
int w;
piii v; // fir:s sec:t
ll ans,st; // ans:max{s+pret},st:sumt
node(piii _v={0,0,0}){ch[0]=ch[1]=nullptr,w=rnd(),v=_v,ans=v.fi+v.se,st=v.se;}
node(node &x){w=x.w,v=x.v,ans=x.ans,st=x.st;}
void push_up()
{
ans=st=0;
if(ch[1]!=nullptr) ans=max(ans,ch[1]->ans),st=ch[1]->st;
ans=max(ans,v.fi+v.se+st),st+=v.se;
if(ch[0]!=nullptr) ans=max(ans,ch[0]->ans+st),st+=ch[0]->st;
}
};
node *rt;
pair<node*,node*> split_val(piii v,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr};
else if(cur->v<=v)
{
auto tmp=split_val(v,cur->ch[1]);
cur->ch[1]=tmp.first,cur->push_up();
return {cur,tmp.second};
}
else
{
auto tmp=split_val(v,cur->ch[0]);
cur->ch[0]=tmp.second,cur->push_up();
return {tmp.first,cur};
}
}
node *merge(node *u,node *v)
{
if(u==nullptr&&v==nullptr) return nullptr;
else if(u==nullptr) return v;
else if(v==nullptr) return u;
else if(u->w<v->w)
{
u->ch[1]=merge(u->ch[1],v),u->push_up();
return u;
}
else
{
v->ch[0]=merge(u,v->ch[0]),v->push_up();
return v;
}
}
void insert(piii v)
{
auto tmp=split_val(v,rt);
node *nd=new node(v);
rt=merge(merge(tmp.first,nd),tmp.second);
}
void erase(piii v)
{
auto tmp=split_val(v,rt);
auto tmpl=split_val(pre(v),tmp.first);
if(tmp.first==tmpl.second) tmp.first=nullptr;
delete tmpl.second;
tmpl.second=nullptr;
rt=merge(tmpl.first,tmp.second);
}
}tr;
const int N=1e6+5,p=1e6+3;
ll s[N],t[N];
//bool Med;
signed main()
{
// cerr<<"Memory Size: "<<abs((&Med)-(&Mst))/1024.0/1024<<" MB\n";
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int q,cnt=0,x;
ll lstans=0;
char op;
cin>>q;
while(q--)
{
cin>>op;
if(op=='A') cnt++,cin>>s[cnt]>>t[cnt],(s[cnt]+=lstans)%=p,(t[cnt]+=lstans)%=p,tr.insert({s[cnt],t[cnt],cnt});
else if(op=='D') cin>>x,x=(x+lstans)%p,tr.erase({s[x],t[x],x});
cout<<(lstans=tr.rt->ans-1)<<"\n",lstans%=p;
}
return 0;
}