Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 110988 | Accepted: 34567 | |
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 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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 915
题意:
给你一个区间长度,并给出这个区间的每一个值,然后进行一些操作,当第一个字母是C的时候,需要对(a,b)区间内所有的数进行加上c的操作,当字母是Q的时候,需要输出(a,b)区间所有数的和。
解题思路:
先对这个区间进行建立二叉树,由于这里涉及到区间的整体增减操作,所以我们需要使用延迟标记,不然会超时,所以在建树的同时需要对所有的标记数组进行清0,树建好之后,就可以进行查询和更新操作了,在更新的时候要注意需要将延迟标记向下传递,因为是该区间的所有数值都需要进行增减c的操作,所以我们可以对该区间的和值进行整体的增减操作,同时标记,在查询的时候,有一点需要注意,这里也是需要进行向下传递延迟标记的,因为在更新的时候只会对处理的区间的下一层进行传递延迟标记,至于更下面的区域是没有操作的,还有在更新的时候找到区间后是直接返回的,并没有对其区间下面进行传递延迟标记的,所以在查询的时候也是需要向下传递延迟标记的,具体请看代码。
ac代码:
#include <cstdio> #include <iostream> using namespace std; #define Max 100005 #define ll long long ll sum[Max<<2]; //记录每个节点的sum值 ll co[Max<<2];//延迟标记数组 void Build(int l, int r, int rt) { co[rt] = 0;//标记初始全部置0 if(l == r) { scanf("%lld", &sum[rt]); return ; } int m = (l + r) >>1; Build(l, m, rt<<1); Build(m+1, r, rt<<1|1); sum[rt] = sum[rt<<1] + sum[rt<<1|1];//递归得到非叶子节点的sum值 } void Updown(int rt, int key)//向下标记函数 { if(co[rt])//如果有标记,即下面的区间需要处理 { co[rt<<1] += co[rt]; //注意这里是相加,不是赋值 co[rt<<1|1] += co[rt]; //hdu这里是赋值,因为那是区间替换 sum[rt<<1] += (key - (key >> 1)) * co[rt];//左子树sum值相加更新 sum[rt<<1|1] += (key >> 1) * co[rt];//右子树sum值相加更新 co[rt]=0;//标记处理完后要清为0,因为更新已经结束,任务结束 } } void Updata(int L, int R, int d, int l, int r, int rt)//区间更新函数 { if(l >= L && r <= R) { co[rt] += d; //标记,为后面查询做准备 sum[rt] += (ll)d * (r - l +1);//该节点需要在这里处理,因为已经是最下面的了,不会再下去了 return ; } Updown(rt, r - l + 1);//向下延迟标记 int m = (l + r) >> 1; if(L <= m) Updata(L, R, d, l, m, rt<<1); //这两行能节省大量时间 if(m < R) Updata(L, R, d, m+1, r, rt<<1|1);// 标准写法,代码简介 sum[rt] = sum[rt<<1] + sum[rt<<1|1];//回溯更新当前sum值 } ll Quest(int L, int R, int l, int r, int rt)//查询函数 { if(l >= L && r <= R) { return sum[rt]; } Updown(rt, r - l + 1);//必须要这一行,向下更新,很重要 int m = (l + r) >> 1; ll ret = 0; if(L <= m) ret += Quest(L, R, l, m, rt<<1);//这两行递归求解和 if(m < R) ret += Quest(L, R, m+1, r, rt<<1|1); return ret; } int main() { int N, Q; scanf("%d%d", &N, &Q); Build(1, N, 1); char c[5]; int a,b,d; while(Q--) { scanf("%s%d%d", &c, &a, &b); if(c[0] == 'Q') printf("%lld\n", Quest(a, b, 1, N, 1)); if(c[0] == 'C') { scanf("%d", &d); Updata(a, b, d, 1, N, 1); } } return 0; }
题目网址链接:点击打开链接http://poj.org/problem?id=3468
相似题目链接:点击打开链接http://blog.csdn.net/wang_heng199/article/details/75041206
相似题目链接:点击打开链接http://blog.csdn.net/wang_heng199/article/details/75036010