BZOJ3212 Pku3468 A Simple Problem with Integers 题解

题目大意:

一个数列,有两个操作:1.修改操作,将一段区间内的数加上c;2.查询操作,查询一段区间内的数的和。

思路:

线段树裸题,区间修改、区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5
 6 long long sum[400001],mor[400001];
 7
 8 void up(int cur,int t)
 9 {
10     sum[cur]=sum[cur<<1]+sum[cur<<1|1]+t*mor[cur];
11 }
12
13 void add(int L,int R,int l,int r,int x,int cur)
14 {
15     if (L>=l && R<=r)
16     {
17         mor[cur]+=x;
18         sum[cur]+=(R-L+1)*x;
19         return;
20     }
21     int mid=L+R>>1;
22     if (l<=mid) add(L,mid,l,r,x,cur<<1);
23     if (r>mid) add(mid+1,R,l,r,x,cur<<1|1);
24     up(cur,R-L+1);
25 }
26
27 long long ask(int L,int R,int l,int r,int cur)
28 {
29     if (L>=l && R<=r) return sum[cur];
30     int mid=L+R>>1;
31     long long ans=mor[cur]*(r-l+1);
32     if (l<=mid) ans+=ask(L,mid,l,min(mid,r),cur<<1);
33     if (r>mid) ans+=ask(mid+1,R,max(l,mid+1),r,cur<<1|1);
34     return ans;
35 }
36
37 int main()
38 {
39     int n,m,i,a,b,c;
40     scanf("%d%d",&n,&m);
41     for (i=1;i<=n;i++) scanf("%d",&a),add(1,n,i,i,a,1);
42     for (i=1;i<=m;i++)
43     {
44         char ch=getchar();
45         while (ch<‘A‘ || ch>‘Z‘) ch=getchar();
46         if (ch==‘Q‘) scanf("%d%d",&a,&b),printf("%lld\n",ask(1,n,a,b,1));
47         else scanf("%d%d%d",&a,&b,&c),add(1,n,a,b,c,1);
48     }
49     return 0;
50 }
时间: 2024-10-25 03:59:48

BZOJ3212 Pku3468 A Simple Problem with Integers 题解的相关文章

BZOJ3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 810  Solved: 354[Submit][Status] Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to

bzoj3212: Pku3468 A Simple Problem with Integers(线段树)

3212: Pku3468 A Simple Problem with Integers 题目:传送门 题解: 感谢Rose_max大佬的倾情相(推)助(水题) 一起打线段树啊~~~~ 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 type

3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1053  Solved: 468[Submit][Status][Discuss] Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of opera

BZOJ 3212 Pku3468 A Simple Problem with Integers

题目大意:你拍一,我拍一,大家一起刷水题. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100010 #define LEFT (pos << 1) #define RIGHT (pos << 1|1) #define CNT (r - l + 1) using namespace std

POJ 3468 A Simple Problem with Integers(详细题解)

这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的. 此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的 具体思路 在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新Sum(加上本次增量),再将增量往下传. 这样更新的复杂度就是O(log(n))在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到Sum上后将Inc清0,接下来再往下查询. Inc往下带的过程也是区

HDU 4267 A Simple Problem with Integers 多个树状数组

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4000    Accepted Submission(s): 1243 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each

poj3468 A Simple Problem with Integers 2011-12-20

A Simple Problem with Integers Time Limit: 5000MSMemory Limit: 131072K Total Submissions: 26062Accepted: 7202 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operat

HDU 4267 A Simple Problem with Integers(树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4191    Accepted Submission(s): 1309 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with