ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

暑假集训Day6 K(线段树模型)

2022-07-11 20:01:34  阅读:174  来源: 互联网

标签:rt Day6 LL pos 集训 ss que upd 线段


题目链接在这里: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 }

 

标签:rt,Day6,LL,pos,集训,ss,que,upd,线段
来源: https://www.cnblogs.com/keximeiruguo/p/16467646.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有