日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU-4027 Can you answer these queries? --线段树

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU-4027 Can you answer these queries? --线段树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:

http://acm.hdu.edu.cn/showproblem.php?pid=4027

題意及思路:

? ? ? 有一排戰艦,給出每個戰艦的能力值,存在兩種操作:第一種是把一定范圍內所有戰艦能力值開根號并向下取整,第二種是求一定區域內所有戰艦能力值之和。如果我們暴力遞歸更新區間上的每一個點,會TLE(不要問我是怎么知道的)。我們可以稍微做一些優化。例如能力值0,1。它們開根號還是它們本身,不需要更新。我們可以增加一個標記tag。如果這個區間都是0或者1,我們就把tag標記為1。以后我們再次更新的時候如果區間tag為1,我們就不繼續向下遞歸。

代碼:

1 #include <iostream> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 #define maxn 100000+5 7 #define LL long long 8 using namespace std; 9 typedef struct node 10 { 11 LL a; 12 int tag; 13 }node; 14 node tree[maxn<<2]; 15 void pushup(int rt) //更新節點的和以及tag值 16 { 17 tree[rt].a=tree[rt<<1].a+tree[rt<<1|1].a; 18 if(tree[rt<<1].tag&&tree[rt<<1|1].tag) 19 tree[rt].tag=1; 20 return ; 21 } 22 int build(int l,int r,int rt) 23 { 24 if(l==r) 25 { 26 scanf("%lld",&tree[rt].a); 27 if(tree[rt].a==0||tree[rt].a==1) 28 tree[rt].tag=1; 29 return 1; 30 } 31 int m=(l+r)>>1; 32 build(l,m,rt<<1); 33 build(m+1,r,rt<<1|1); 34 pushup(rt); 35 } 36 void update(int L,int R,int l,int r,int rt) 37 { 38 if( L<=l && r <= R&&tree[rt].tag==1) //如果區間tag值為1,直接返回 39 return ; 40 if(l==r) 41 { 42 tree[rt].a=(LL)(sqrt(1.0*tree[rt].a)); 43 if(tree[rt].a==1||tree[rt].a==0) 44 tree[rt].tag=1; 45 return ; 46 } 47 int m=(l+r)>>1; 48 if(L <= m) 49 update(L,R,l,m,rt<<1); 50 if(R > m) 51 update(L,R,m+1,r,rt<<1|1); 52 pushup(rt); 53 } 54 LL query(int L,int R,int l,int r,int rt) 55 { 56 if(L <= l && r <= R) 57 { 58 return tree[rt].a; 59 } 60 int m=(l+r)>>1; 61 LL ans=0; 62 if(L <= m) 63 ans+=query(L,R,l,m,rt<<1); 64 if(R > m) 65 ans+=query(L,R,m+1,r,rt<<1|1); 66 return ans; 67 } 68 int main() 69 { 70 int n,jishu; 71 jishu=0; 72 while(scanf("%d",&n)!=EOF) 73 { 74 printf("Case #%d:\n",++jishu); 75 build(1,n,1); 76 int k; 77 scanf("%d",&k); 78 while(k--) 79 { 80 int c,a,b; 81 scanf("%d%d%d",&c,&a,&b); 82 if(a>b) 83 swap(a,b); 84 if(c==0) 85 update(a,b,1,n,1); 86 else 87 cout<<query(a,b,1,n,1)<<endl; 88 } 89 cout<<endl; 90 } 91 }

?

轉載于:https://www.cnblogs.com/blame/p/11369474.html

總結

以上是生活随笔為你收集整理的HDU-4027 Can you answer these queries? --线段树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。