poj3468

线段树中对一段区间操作的方法----记录增量。详细实现见代码。

还要好好体会!

 1 //Accepted    6688K    1485MS
2 #include <cstdio>
3 #include <cstring>
4 #define imax 100005
5 struct node
6 {
7 int l,r;
8 __int64 add,sum;
9 }f[imax*3];
10 int num[imax];
11 int n,m;
12 void build(int t,int l,int r)
13 {
14 f[t].l=l;
15 f[t].r=r;
16 f[t].add=0;
17 if (l==r)
18 {
19 f[t].sum=num[l];
20 return ;
21 }
22 int mid=(l+r)/2;
23 build(2*t,l,mid);
24 build(2*t+1,mid+1,r);
25 f[t].sum=f[2*t].sum+f[2*t+1].sum;
26 }
27 void Add(int t,int l,int r,int c)
28 {
29 if (f[t].l==l && f[t].r==r)
30 {
31 f[t].add+=c;
32 return ;
33 }
34 f[t].sum+=c*(r-l+1);
35 int mid=(f[t].l+f[t].r)/2;
36 if (r<=mid) Add(2*t,l,r,c);
37 else
38 {
39 if (l>mid) Add(2*t+1,l,r,c);
40 else
41 {
42 Add(2*t,l,mid,c);
43 Add(2*t+1,mid+1,r,c);
44 }
45 }
46 }
47 __int64 query(int t,int l,int r)
48 {
49 if (f[t].l==l && f[t].r==r)
50 {
51 return f[t].sum+f[t].add*(f[t].r-f[t].l+1);
52 }
53 int mid=(f[t].l+f[t].r)/2;
54 f[2*t].add+=f[t].add;
55 f[2*t+1].add+=f[t].add;
56 f[t].sum+=f[t].add*(f[t].r-f[t].l+1);
57 f[t].add=0;
58 if (r<=mid) return query(2*t,l,r);
59 else
60 {
61 if (l>mid) return query(2*t+1,l,r);
62 else
63 return query(2*t,l,mid)+query(2*t+1,mid+1,r);
64 }
65 }
66 int main()
67 {
68 while (scanf("%d%d",&n,&m)!=EOF)
69 {
70 for (int i=1;i<=n;i++)
71 scanf("%d",&num[i]);
72 build(1,1,n);
73 int x,y,c;
74 char ch[5];
75 for (int i=0;i<m;i++)
76 {
77 scanf("%s",ch);
78 if (ch[0]==‘Q‘)
79 {
80 scanf("%d%d",&x,&y);
81 printf("%I64d\n",query(1,x,y));
82 }
83 else
84 {
85 scanf("%d%d%d",&x,&y,&c);
86 Add(1,x,y,c);
87 }
88 }
89 }
90 return 0;
91 }

时间: 2024-10-23 14:25:09

poj3468的相关文章

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 【线段树】+【成段更新】

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(区间更新)

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

poj3468 线段树 or splay

poj3468  裸线段树.因为在熟悉splay 所以就用splay交了一发...开始用的scanf()!==2 居然TLE了...然后我就当单组测试数据做的 然后就过了  囧TZ #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> using namespac

POJ-3468(线段树+区间更新+区间查询)

A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是update函数里面还需要计算sum数组.因为这里查询的时候是直接用sum查询结点. //区间更新,区间查询 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #inc

POJ3468(KB7-C 线段树)

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

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 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(线段树)

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

poj3468 线段树

http://poj.org/problem?id=3468 题目链接, 很经典的线段树的应用, 这里复习一下, 再写一遍, 代码如下: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100000 + 10; int N, Q; int a[maxn]; struct Segment{ int l, r; long long

POJ3468 本来是一道线段树

// 然而博主用 Splay Tree 做的,4000+ ms...飘过 1 #include "cstdio" 2 using namespace std; 3 long long in[100010]; 4 const int INF = 1 << 28; 5 6 struct Node { 7 Node *pre, *ch[2]; 8 long long sz, val; 9 long long sum, add; 10 } *null, *root, buf[100