可惜没如果=_=
时光的河入海流

题目链接在这里:Problem - K - Codeforces

经过观察可以发现会见骑士结束的时间点可以表示成一个式子c_x=max(t_i+sigma(d_i+...d_x)) (i=1...x)

只需要把 t_i 离散化出来,这就是跟上一个一样的经典线段树模型。

码力要加强啊!写的时间太长了!

 1 #include "bits/stdc++.h"
 2 #define lson rt<<1,l,m
 3 #define rson rt<<1|1,m+1,r
 4 using namespace std;
 5 typedef long long LL;
 6 const int MAX=1e6+5;
 7 LL t,n,a[MAX],b[MAX],ss[MAX];
 8 LL cc[MAX],tr[MAX<<2],la[MAX<<2];
 9 struct Que{
10     char c;
11     int x,y;
12 }que[MAX];
13 void update(LL x,LL y,LL nn){for (;x<=nn;x+=(x&-x)) cc[x]+=y;}
14 LL find(LL x){LL an=0;for (;x>0;x-=(x&-x)) an+=cc[x];return an;}
15 void PushDown(LL rt){
16     if (la[rt]){
17         la[rt<<1]+=la[rt];
18         la[rt<<1|1]+=la[rt];
19         tr[rt<<1]+=la[rt];
20         tr[rt<<1|1]+=la[rt];
21         la[rt]=0;
22     }
23 }
24 void PushUp(LL rt){tr[rt]=max(tr[rt<<1],tr[rt<<1|1]);}
25 void upd(LL rt,LL l,LL r,LL x,LL y,LL z){
26     if (x<=l && r<=y){
27         la[rt]+=z;
28         tr[rt]+=z;
29         return;
30     }
31     LL m=(l+r)>>1;
32     PushDown(rt);
33     if (x<=m) upd(lson,x,y,z);
34     if (y>m) upd(rson,x,y,z);
35     PushUp(rt);
36 }
37 LL search(LL rt,LL l,LL r,LL x,LL y){
38     if (x<=l && r<=y) return tr[rt];
39     LL m=(l+r)>>1,an=-1;
40     PushDown(rt);
41     if (x<=m) an=max(an,search(lson,x,y));
42     if (y>m) an=max(an,search(rson,x,y));
43     return an;
44 }
45 int main(){
46     freopen ("k.in","r",stdin);
47     freopen ("k.out","w",stdout);
48     LL i,j,n=0,pos,zt;
49     memset(cc,0,sizeof(cc));
50     memset(tr,0,sizeof(tr));
51     memset(la,0,sizeof(la));
52     scanf("%lld",&t);
53     for (i=1;i<=t;i++){
54         scanf("\n%c",&que[i].c);
55         if (que[i].c=='?' || que[i].c=='-') scanf("%lld",&que[i].x);
56         else{
57             scanf("%lld%lld",&que[i].x,&que[i].y);
58             ss[++n]=que[i].x;
59         }
60     }
61     sort(ss+1,ss+n+1);
62     ss[n+1]=1e15;
63     for (i=1;i<=t;i++){
64         if (que[i].c=='?'){
65             pos=lower_bound(ss+1,ss+n+1,que[i].x)-ss;
66             if (ss[pos]>que[i].x) pos--;
67             if (pos>=1) zt=search(1,1,n,1,pos);
68             else zt=0;
69             printf("%lld\n",max(0ll,find(pos)+zt-que[i].x));
70         }
71         else if (que[i].c=='+'){
72             pos=lower_bound(ss+1,ss+n+1,que[i].x)-ss;
73             update(pos,que[i].y,n);
74             upd(1,1,n,pos,pos,que[i].x);
75             if (pos!=n)upd(1,1,n,pos+1,n,-que[i].y);
76         }
77         else if (que[i].c=='-'){
78             pos=lower_bound(ss+1,ss+n+1,que[que[i].x].x)-ss;
79             update(pos,-que[que[i].x].y,n);
80             upd(1,1,n,pos,pos,-que[que[i].x].x);
81             if (pos!=n) upd(1,1,n,pos+1,n,que[que[i].x].y);
82         }
83     }
84     return 0;
85 }

 

posted on 2022-07-11 19:50  珍珠鸟  阅读(17)  评论(0编辑  收藏  举报