【POJ3468】A Simple Problem with Integers

单一标记线段树。

在给定数列上建树:build时读入即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=100010;
 5 long long tag[N<<2],sum[N<<2],c;
 6 void build(int,int,int),release(int,int,int),update(int),
 7     add(int,int,int,long long),modify(int,int,int,int,int,long long);
 8 long long query(int,int,int,int,int);
 9 int main(){
10     int x,y,n,q;
11     scanf("%d %d",&n,&q);
12     build(1,1,n);
13     for (int i=1;i<=q;i++){
14         getchar();
15         char ch=getchar();
16         scanf("%d %d",&x,&y);
17         if (ch==‘C‘){
18             scanf("%lld",&c);
19             modify(1,1,n,x,y,c);
20         }
21         else printf("%lld\n",query(1,1,n,x,y));
22     }
23 }
24 void build(int i,int l,int r){
25     tag[i]=0;
26     if (l==r){
27         scanf("%lld",&sum[i]);
28         return;
29     }
30     int mid=(l+r)>>1;
31     build(i<<1,l,mid);
32     build(i<<1|1,mid+1,r);
33     update(i);
34 }
35 void update(int i){
36     sum[i]=sum[i<<1]+sum[i<<1|1];
37 }
38 void modify(int i,int l,int r,int L,int R,long long d){
39     if (L<=l&&r<=R){
40         add(i,l,r,d);return;
41     }
42     release(i,l,r);
43     int mid=(l+r)>>1;
44     if (L<=mid) modify(i<<1,l,mid,L,R,d);
45     if (R>mid) modify(i<<1|1,mid+1,r,L,R,d);
46     update(i);
47 }
48 void add(int i,int l,int r,long long d){
49     tag[i]+=d;
50     sum[i]+=(r-l+1)*d;
51 }
52 void release(int i,int l,int r){
53     if (tag[i]){
54         int mid=(l+r)>>1;
55         add(i<<1,l,mid,tag[i]);
56         add(i<<1|1,mid+1,r,tag[i]);
57         tag[i]=0;
58     }
59 }
60 long long query(int i,int l,int r,int L,int R){
61     if (L<=l&&r<=R) return sum[i];
62     release(i,l,r);
63     int mid=(l+r)>>1;
64     long long ans=0;
65     if (L<=mid) ans+=query(i<<1,l,mid,L,R);
66     if (R>mid) ans+=query(i<<1|1,mid+1,r,L,R);
67     return ans;
68 }

STD

时间: 2024-11-03 21:37:51

【POJ3468】A Simple Problem with Integers的相关文章

【POJ】A Simple Problem with Integers(线段树区间修增减求和)

线段树区间修改增加问题,模板题,注意懒惰处理. 13443449 201301052100 3468 Accepted 4308K 1610MS C++ 2362B 2014-09-15 15:46:35 #include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<queue> #include<map> #include<cst

一本通1548【例 2】A Simple Problem with Integers

1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],-,a[n],你需要依次进行 q 个操作,操作有两类: 1 l r x:给定 l,r,x,对于所有 i∈[l,r],将 a[i] 加上 x(换言之,将 a[l],a[l+1],-,a[r] 分别加上 x): 2 l r:给定 l,r,求 a[i]∑i=[l,r].?a[i] 的值(换言之,求 a[l]+a[l+1]+?+a[r] 的值). 输入格式 第一行包

【POJ3468】【zkw线段树】A Simple Problem with Integers

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. In

【POJ3468】【树状数组区间修改】A Simple Problem with Integers

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. In

【成端更新线段树模板】POJ3468-A Simple Problem with Integers

http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲一下pushdown和pushup出现的几个位置. pushup: (1)build的结尾,当叶子节点分别有对应的值后,它的父亲们就等于它们求和. (2)update的结尾,因为此时当前根的左右孩子已经更新了,故它也需要更新. pushdown(延迟标记): *pushdown会出现在一切要从当前结

【POJ2761】【fhq treap】A Simple Problem with Integers

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. In

POJ3468 A Simple Problem with Integers 【线段树】+【成段更新】

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

POJ 3468 A Simple Problem with Integers 【树状数组】

题目链接:http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b,要求输出v[a]到v[b]之间的和,另一种给出三个数a,b,c,让v[a]到v[b]之间的数全都加上c. 完全是树状数组能够实现的功能,但是如果就这样单纯的套用模板,做第二种操作是更新每个值,这样的操作就有可能超时. 换一种思路,既然第二种操作是给某区间上的所有数加上相同的值,那么应该是能够简化的才对. 假设数组sum[i]为原数组从v[1]到v[i]的和,数

【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