您的位置:首页 > 其它

POJ3468 A Simple Problem with Integers

2016-09-20 20:53 211 查看
A Simple Problem with Integers
Time Limit: 5000MS      Memory Limit: 131072K
Total Submissions: 97313        Accepted: 30386
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
9
15
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi


线段树简单题,lazy_tag成段增加或者成段减少,
还有就是区间求和。
基本就是这个样子,还要注意数据类型。
大概就这样子了。


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define MOD 1000000007

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 100000+10;
using namespace std;

inline int read(){
int x(0),f(1);
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
ll sum[maxn<<2];
ll Lazy_tag[maxn<<2];
inline void PushUp(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
inline void PushDown(int rt, int m)
{
if(Lazy_tag[rt]){
Lazy_tag[rt<<1] += Lazy_tag[rt];
Lazy_tag[rt<<1|1] += Lazy_tag[rt];
sum[rt<<1] += (m-(m>>1))*Lazy_tag[rt];
sum[rt<<1|1] += (m>>1)*Lazy_tag[rt];
Lazy_tag[rt] = 0;
}
}
inline void buildTree(int l, int r, int rt)
{
Lazy_tag[rt] = 0;
if(l == r){
scanf("%lld", &sum[rt]);
return;
}
int m = (l+r)>>1;
buildTree(lson);
buildTree(rson);
PushUp(rt);
}
inline void update(int L, int R, int p, int l, int r, int rt)
{
if(L <= l && r <= R){
Lazy_tag[rt] += p;
sum[rt] += (ll)p*(r-l+1);
return;
}
PushDown(rt, r-l+1);
int m = (l+r)>>1;
if(L <= m){
update(L, R, p, lson);
}
if(R > m){
update(L, R, p, rson);
}
PushUp(rt);
}
inline ll query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return sum[rt];
}
PushDown(rt, r-l+1);
ll ret = 0;
int m = (l+r)>>1;
if(L <= m){
ret += query(L, R, lson);
}
if(R > m){
ret += query(L, R, rson);
}
return ret;
}

int main()
{
int N, Q;
N = read();
Q = read();
buildTree(1, N, 1);
while(Q--){
int a,b, c;
char op[2];
scanf("%s", op);
if(op[0] == 'Q'){
a = read();
b = read();
printf("%lld\n", query(a, b, 1, N, 1));
}else{
a = read();
b = read();
c = read();
update(a, b, c, 1, N, 1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: