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<list>
 11
 12 #define MAXSIZE 100010
 13
 14 using namespace std;
 15
 16 long long tree[4*MAXSIZE];
 17 long long lz[4*MAXSIZE];
 18 int N, Q;
 19
 20 void init()
 21 {
 22     memset(tree, 0, sizeof(tree));
 23     memset(lz, 0, sizeof(lz));
 24 }
 25
 26 void build(int node, int l, int r)
 27 {
 28     if(l == r)
 29     {
 30         scanf("%lld", &tree[node]);
 31         return;
 32     }
 33
 34     int mid = (l+r)/2;
 35     build(node*2, l, mid);
 36     build(node*2+1, mid+1, r);
 37
 38     tree[node] = tree[node*2] + tree[node*2+1];
 39 }
 40
 41 void push_down(int node, int l, int r)
 42 {
 43     if(lz[node])
 44     {
 45         int mid = (l+r)/2;
 46         lz[node*2] += lz[node];
 47         lz[node*2+1] += lz[node];
 48         tree[node*2] += (mid-l+1)*lz[node];
 49         tree[node*2+1] += (r-mid)*lz[node];
 50         lz[node] = 0;
 51     }
 52 }
 53
 54 void update_range(int node, int l, int r, int L, int R, int add)
 55 {
 56     if(l <= L && r >= R)
 57     {
 58         lz[node] += add;
 59         tree[node] += (R-L+1)*add;
 60         return;
 61     }
 62     push_down(node, L, R);
 63     int mid = (L+R)/2;
 64     if(mid >= l)
 65         update_range(node*2, l, r, L, mid, add);
 66     if(mid < r)
 67         update_range(node*2+1, l, r, mid+1, R, add);
 68     tree[node] = tree[node*2] + tree[node*2+1];
 69 }
 70
 71 long long query_range(int node, int l, int r, int L, int R)
 72 {
 73     if(l <= L && r >= R)
 74     {
 75         return tree[node];
 76     }
 77     push_down(node, L, R);
 78     int mid = (L+R)/2;
 79     long long sum = 0;
 80     if(mid >= l)
 81         sum += query_range(node*2, l, r, L, mid);
 82     if(mid < r)
 83         sum += query_range(node*2+1, l, r, mid+1, R);
 84
 85     return sum;
 86 }
 87
 88
 89
 90 int main()
 91 {
 92     scanf("%d%d", &N, &Q);
 93     init();
 94     build(1, 1, N);
 95     while(Q--)
 96     {
 97         char ch;
 98         scanf(" %c", &ch);
 99         if(ch == ‘Q‘)
100         {
101             int a, b;
102             scanf("%d%d", &a, &b);
103             printf("%lld\n", query_range(1, a, b, 1, N));
104         }
105         else if(ch == ‘C‘)
106         {
107             int a, b, c;
108             scanf("%d%d%d", &a, &b, &c);
109             update_range(1, a, b, 1, N, c);
110         }
111     }
112
113
114     return 0;
115 }

原文地址:https://www.cnblogs.com/FengZeng666/p/11458279.html

时间: 2024-11-09 03:29:44

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

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 线段树区间修改+区间查询

//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 多树状数组解决区间修改问题。

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

poj3468A Simple Problem with Integers区间和线段树

同上... #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include

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

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): 3708    Accepted Submission(s): 1139 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

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>