1 / 9

查询任务 140327032 曾雪娥

查询任务 140327032 曾雪娥. 程序员小明现在需要处理一段序列 A1,A2, ... ,AN 。 给出一个区间 [a,b] ,然后系统返回 Aa 到 Ab 的求和结果。 同时又必须经常修改其中一段子序列的值,修改过程为对一个区间 [a,b] ,将 Aa 到 Ab 的值加上一个值 c 。. ★数据输入 第一行是两个整数 N 和 Q 。 1 ≤ N,Q≤ 100000 。 第二行包含 N 个数字, A1,A2, ... ,AN 的初始值。其中 -1000000000 ≤Ai≤ 1000000000 。 接下来的 Q 行每行表示一个操作:

Download Presentation

查询任务 140327032 曾雪娥

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 查询任务 140327032 曾雪娥

  2. 程序员小明现在需要处理一段序列A1,A2, ... ,AN。 给出一个区间[a,b],然后系统返回Aa到Ab的求和结果。 同时又必须经常修改其中一段子序列的值,修改过程为对一个区间[a,b],将Aa到Ab的值加上一个值c。

  3. ★数据输入 第一行是两个整数N和Q。1 ≤N,Q≤ 100000 。 第二行包含N个数字,A1,A2, ... ,AN的初始值。其中-1000000000 ≤Ai≤ 1000000000 。 接下来的Q行每行表示一个操作: "Cabc"表示对Aa到Ab的每一个值均加上数值c。 "Qab"表示求Aa到Ab的求和结果。

  4. ★数据输出 对于每一次Q询问输出一行,表示这一次询问的结果。输入示例输出示例5 34 2 1 3 2 1Q 3 3 16 C 1 4 3Q 1 3

  5. 1. 1 ≤N,Q≤ 100000, -1000000000 ≤Ai≤ 1000000000 ,所以问题的解存在超出2^32的情况,因此用于存储累加值的变量的类型要设置成_int64,才能保证不溢出。 2.最简单的方法就是对该问题进行直接遍历,它的时间复杂度是o(N*Q)。虽然部分数据存在超时的问题,但是它的代码量相对于用二叉树而言,翻倍减少,而且在内存、耗时等指标上也是相对比较好的。

  6. int N,Q; int A[100002],i,j,k,a,b,c; char ch; _int64 sum; while(scanf("%d %d",&N,&Q)!=EOF) { for(i=1;i<=N;i++) scanf("%d",&A[i]); for(j=0;j<Q;j++) if(scanf("%c",&ch)&&ch=='Q') { scanf("%d %d",&a,&b); sum=0; for(k=a;k<=b;k++)

  7. sum=sum+A[k]; printf("%I64d\n",sum);} else if(ch=='C') { scanf("%d %d %d",&a,&b,&c); for(k=a;k<=b;k++) A[k]=A[k]+c; } }

  8. 优点:简单,代码量少,内存、耗时等指标都相对较好。优点:简单,代码量少,内存、耗时等指标都相对较好。 缺点:进行直接遍历,时间复杂度高,存在部分数据超时的问题。

More Related