POJ 3468 线段树区间求和

线段树区间更新与查询
本文介绍了一种使用线段树进行区间更新与查询的方法,通过存储节点和的增量来提高效率。具体实现了线段树的构建、插入、添加操作及区间和的查询,并附带了一个整数操作问题的样例程序。
线段树区间求和树节点不能只存和,只存和,会导致每次加数的时候都要更新到叶子节点,速度太慢(O(nlogn))。所以我们要存两个量,一个是原来的和nSum,一个是累加的增量Inc。

在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新nSum(加上本次增量),再将增量往下传,这样更新的复杂度就是O(log(n))。

 

在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc清0,接下来再往下查询。 Inc往下带的过程也是区间分解的过程,复杂度也是O(log(n))。                                                                                                                                                   
 
                                                                                   A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 79334 Accepted: 24455
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15
  1 #include <iostream>
  2 #include<cstdio>
  3 using namespace std;
  4 struct CNode
  5 {
  6     int L ,R;
  7     CNode * pLeft, * pRight;
  8     long long nSum; //原来的和
  9     long long Inc; //增量c的累加
 10 };
 11 CNode Tree[200010]; // 2倍叶子节点数目就够
 12 int nCount = 0;
 13 int Mid( CNode * pRoot)
 14 {
 15     return (pRoot->L + pRoot->R)/2;
 16 }
 17 void BuildTree(CNode * pRoot,int L, int R)
 18 {
 19     pRoot->L = L;
 20     pRoot->R = R;
 21     pRoot->nSum = 0;
 22     pRoot->Inc = 0;
 23     if( L == R)
 24         return;
 25     nCount ++;
 26     pRoot->pLeft = Tree + nCount;
 27     nCount ++;
 28     pRoot->pRight = Tree + nCount;
 29     BuildTree(pRoot->pLeft,L,(L+R)/2);
 30     BuildTree(pRoot->pRight,(L+R)/2+1,R);
 31 }
 32 void Insert( CNode * pRoot,int i, int v)
 33 {
 34     if( pRoot->L == i && pRoot->R == i)
 35     {
 36         pRoot->nSum = v;
 37         return ;
 38     }
 39     pRoot->nSum += v;
 40     if( i <= Mid(pRoot))
 41         Insert(pRoot->pLeft,i,v);
 42     else
 43         Insert(pRoot->pRight,i,v);
 44 }
 45 void Add( CNode * pRoot, int a, int b, long long c)
 46 {
 47     if( pRoot->L == a && pRoot->R == b)
 48     {
 49         pRoot->Inc += c;
 50         return ;
 51     }
 52     pRoot->nSum += c * ( b - a + 1) ;
 53     if( b <= (pRoot->L + pRoot->R)/2)
 54         Add(pRoot->pLeft,a,b,c);
 55     else if( a >= (pRoot->L + pRoot->R)/2 +1)
 56         Add(pRoot->pRight,a,b,c);
 57     else
 58     {
 59         Add(pRoot->pLeft,a,
 60             (pRoot->L + pRoot->R)/2 ,c);
 61         Add(pRoot->pRight,
 62             (pRoot->L + pRoot->R)/2 + 1,b,c);
 63     }
 64 }
 65 long long QuerynSum( CNode * pRoot, int a, int b)
 66 {
 67     if( pRoot->L == a && pRoot->R == b)
 68         return pRoot->nSum +
 69                (pRoot->R - pRoot->L + 1) * pRoot->Inc ;
 70     pRoot->nSum += (pRoot->R - pRoot->L + 1) * pRoot->Inc ;
 71     Add( pRoot->pLeft,pRoot->L,Mid(pRoot),pRoot->Inc);
 72     Add( pRoot->pRight,Mid(pRoot) + 1,pRoot->R,pRoot->Inc);
 73     pRoot->Inc = 0;
 74     if( b <= Mid(pRoot))
 75         return QuerynSum(pRoot->pLeft,a,b);
 76     else if( a >= Mid(pRoot) + 1)
 77         return QuerynSum(pRoot->pRight,a,b);
 78     else
 79     {
 80         return QuerynSum(pRoot->pLeft,a,Mid(pRoot)) +
 81                QuerynSum(pRoot->pRight,Mid(pRoot) + 1,b);
 82     }
 83 }
 84 int main()
 85 {
 86     int n,q,a,b,c;
 87     char cmd[10];
 88     scanf("%d%d",&n,&q);
 89     int i,j,k;
 90     nCount = 0;
 91     BuildTree(Tree,1,n);
 92     for( i = 1; i <= n; i ++ )
 93     {
 94         scanf("%d",&a);
 95         Insert(Tree,i,a);
 96     }
 97     for( i = 0; i < q; i ++ )
 98     {
 99         scanf("%s",cmd);
100         if ( cmd[0] == 'C' )
101         {
102             scanf("%d%d%d",&a,&b,&c);
103             Add( Tree,a,b,c);
104         }
105         else
106         {
107             scanf("%d%d",&a,&b);
108             printf("%I64d\n",QuerynSum(Tree,a,b));
109         }
110     }
111     return 0;
112 }
View Code

 

转载于:https://www.cnblogs.com/zero-zz/p/4811331.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值