(线段树模板)A Simple Problem with Integers --POJ--3468

链接:

http://poj.org/problem?id=3468

代码:

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<stdlib.h>
  4 using namespace std;
  5
  6 #define Lson r<<1
  7 #define Rson r<<1|1
  8
  9 const int N = 1e5+5;
 10
 11 struct SegmentTree
 12 {
 13     int L, R;
 14     long long sum, e;
 15     int Mid()
 16     {
 17         return (L+R)>>1;
 18     }
 19     int len()
 20     {
 21         return R-L+1;
 22     }
 23 }a[N<<2];
 24
 25 void BuildSegTree(int r, int L, int R)
 26 {
 27     a[r].L=L, a[r].R=R;
 28     a[r].e=0;
 29
 30     if(L==R)
 31     {
 32         scanf("%lld", &a[r].sum);
 33         return ;
 34     }
 35
 36     BuildSegTree(Lson, L, a[r].Mid());
 37     BuildSegTree(Rson, a[r].Mid()+1, R);
 38
 39     a[r].sum = a[Lson].sum + a[Rson].sum;
 40 }
 41 void PushDown(int r)
 42 {
 43     a[Lson].sum += a[r].e*a[Lson].len();
 44     a[Lson].e += a[r].e;
 45     a[Rson].sum += a[r].e*a[Rson].len();
 46     a[Rson].e += a[r].e;
 47
 48     a[r].e=0;
 49 }
 50 void Update(int r, int L, int R, int e)
 51 {
 52     a[r].sum += (R-L+1)*e;
 53
 54     if(a[r].L==L && a[r].R==R)
 55     {
 56         a[r].e += e;
 57         return ;
 58     }
 59
 60     PushDown(r);
 61
 62     if(R<=a[r].Mid())
 63         Update(Lson, L, R, e);
 64     else if(L>a[r].Mid())
 65        Update(Rson, L, R, e);
 66     else
 67     {
 68         Update(Lson, L, a[r].Mid(), e);
 69         Update(Rson, a[r].Mid()+1, R, e);
 70     }
 71 }
 72 long long Query(int r, int L, int R)
 73 {
 74     if(a[r].L==L && a[r].R==R)
 75        return a[r].sum;
 76
 77     PushDown(r);
 78
 79     if(R<=a[r].Mid())
 80         return Query(Lson, L, R);
 81     else if(L > a[r].Mid())
 82         return Query(Rson, L, R);
 83     else
 84     {
 85         long long Lsum = Query(Lson, L, a[r].Mid());
 86         long long Rsum = Query(Rson, a[r].Mid()+1, R);
 87
 88         return Lsum+Rsum;
 89     }
 90 }
 91
 92 int main()
 93 {
 94    int n, m;
 95    while(scanf("%d%d", &n, &m)!=EOF)
 96    {
 97        BuildSegTree(1, 1, n);
 98
 99        while(m--)
100        {
101            char s[10];
102            int L, R, e;
103
104            scanf("%s", s);
105
106            if(s[0]==‘Q‘)
107            {
108                scanf("%d%d", &L, &R);
109                printf("%lld\n", Query(1, L, R));
110            }
111            else
112            {
113                scanf("%d%d%d", &L, &R, &e);
114                Update(1, L, R, e);
115            }
116        }
117    }
118    return 0;
119 }
时间: 2024-09-29 22:07:39

(线段树模板)A Simple Problem with Integers --POJ--3468的相关文章

A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

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

C - A Simple Problem with Integers POJ 3468

C - A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3468 Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of ope

【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

C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 1 #include<cstdio> 2 using namespace std; 3 const int maxn=1e5+6; 4 int n,a[maxn]; 5 struct Node{ 6 int l,r; 7 long long sum,lazy; 8 void update(long long x){//用于更新区间和 和懒标记 9 sum+=1ll*(r-l+1)*x; 10 lazy+=x; 11 } 12

A Simple Problem with Integers POJ - 3468 (线段树)

思路:线段树,区间更新,区间查找 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<cmath> 5 #include<set> 6 #include<algorithm> 7 #include<cstdio> 8 #include<map> 9 #include<cstring> 10 #include<

A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 100010; int n, m; int w[N]; struct Node { int l, r;

A Simple Problem with Integers POJ 3468

题目链接:   http://poj.org/problem?id=3468 题意: 给出一个序列,然后会有两种操作,一是让下标从 i - j 都增加 某个固定值, 二是询问你下标从 m - n 这个区域的和是多少? #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue&g

C - A Simple Problem with Integers - poj 3468(区间更新)

题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每个点都进行更新,不过我们可以使用一个op的开关,如果op等于0说明这个不需要更新,如果等于1就说明需要进行更新,这样只需要和插入的时候进行一下更新即可 *********************************************************************** 注意

A Simple Problem with Integers POJ - 3468 区间更新,区间查询板子

#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<math.h> #include<vector> #include<stack>

POJ3468---线段树模版--A Simple Problem with Integers

#include<stdio.h> #include<algorithm> #include<string.h> #define ll __int64 #define M 100007 using namespace std; struct node { ll l,r,mid,val,mark; }tree[M<<2]; ll s[M]; void build(ll left,ll right,ll i)//建树 { tree[i].l=left;tree[