博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Simple Problem with Integers(线段树入门题)
阅读量:7154 次
发布时间:2019-06-29

本文共 3295 字,大约阅读时间需要 10 分钟。

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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers.
 
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 using namespace std; 17 typedef long long LL; 18 const int INF=0x5fffffff; 19 const double EXP=1e-6; 20 const int MS=100005; 21 22 struct node 23 { 24 int l,r; 25 LL sum,inc; //特别注意这里 26 int mid() 27 { 28 return (l+r)/2; 29 } 30 }nodes[4*MS]; 31 32 void creat(int root,int l,int r) 33 { 34 nodes[root].l=l; 35 nodes[root].r=r; 36 nodes[root].sum=0; 37 nodes[root].inc=0; 38 if(l==r) 39 return ; 40 creat(root<<1,l,(l+r)/2); 41 creat(root<<1|1,(l+r)/2+1,r); 42 } 43 44 void insert(int root,int pos,int value) 45 { 46 if(nodes[root].l==nodes[root].r) 47 { 48 nodes[root].sum+=value; 49 return ; 50 } 51 nodes[root].sum+=value; 52 if(pos<=nodes[root].mid()) 53 insert(root<<1,pos,value); 54 else 55 insert(root<<1|1,pos,value); 56 } 57 58 void add(int root,int a,int b,int c) 59 { 60 if(nodes[root].l==a&&nodes[root].r==b) 61 { 62 nodes[root].inc+=c; 63 return ; 64 } 65 nodes[root].sum+=c*(b-a+1); 66 if(b<=nodes[root].mid()) 67 add(root<<1,a,b,c); 68 else if(a>nodes[root].mid()) 69 add(root<<1|1,a,b,c); 70 else 71 { 72 add(root<<1,a,nodes[root].mid(),c); 73 add(root<<1|1,nodes[root].mid()+1,b,c); 74 } 75 } 76 77 LL query(int root,int a,int b) 78 { 79 if(nodes[root].l>=a&&nodes[root].r<=b) 80 return nodes[root].sum+nodes[root].inc*(nodes[root].r-nodes[root].l+1); 81 nodes[root].sum+=nodes[root].inc*(nodes[root].r-nodes[root].l+1); 82 add(root<<1,nodes[root].l,nodes[root].mid(),nodes[root].inc); 83 add(root<<1|1,nodes[root].mid()+1,nodes[root].r,nodes[root].inc); 84 nodes[root].inc=0; 85 if(b<=nodes[root].mid()) 86 return query(root<<1,a,b); 87 else if(a>nodes[root].mid()) 88 return query(root<<1|1,a,b); 89 else 90 return query(root<<1,a,nodes[root].mid())+query(root<<1|1,nodes[root].mid()+1,b); 91 } 92 93 int main() 94 { 95 int N,Q,x,y,z; 96 scanf("%d%d",&N,&Q); 97 creat(1,1,N); 98 for(int i=1;i<=N;i++) 99 {100 scanf("%d",&x);101 insert(1,i,x);102 }103 char cmd[MS];104 while(Q--)105 {106 scanf("%s",cmd);107 if(cmd[0]=='Q')108 {109 scanf("%d%d",&x,&y);110 printf("%lld\n",query(1,x,y));111 }112 else113 {114 scanf("%d%d%d",&x,&y,&z);115 add(1,x,y,z);116 }117 }118 return 0;119 }

 

 
 

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/3861569.html

你可能感兴趣的文章
亿级数据时,内存性能低于IO性能
查看>>
asp.net 负载均衡下session存储的解决方法
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序
查看>>
Yii框架操作数据库的几种方式与mysql_escape_string
查看>>
公有云与私有云的差别(转)
查看>>
jQuery插件:jqGrid使用(一)
查看>>
mac显示隐藏文件
查看>>
FastDFS的配置、部署与API使用解读(6)FastDFS配置详解之Storage配置(转)
查看>>
学习心得:《十个利用矩阵乘法解决的经典题目》from Matrix67
查看>>
领域驱动开发推荐代码示例 — Microsoft NLayerApp
查看>>
Linux 安装Rsync和配置
查看>>
hadoop fs -mkdir testdata错误 提示No such file or directory
查看>>
「镁客早报」夏普分拆半导体业务;教育部要求高校组织开展基因编辑相关研究项目自查工作 ...
查看>>
烟沙浮生 | 此间少年(2018-10-15 第五周记)
查看>>
Python特性概要
查看>>
一次关于Flutter的碰壁 | VSCode中搭建开发环境(插件 | 虚拟机 | 新建项目并运行) ...
查看>>
我国首次实现Pb s级光传输,只需一根光纤可供300亿人同时通话 ...
查看>>
docker 入门应用
查看>>
【机器学习PAI实战】—— 玩转人工智能之美食推荐 ...
查看>>
k8s dns 带证书配置
查看>>