Poj(3468)——A Simple Problem with Integers(线段树)

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 72424   Accepted: 22343
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q a b" means querying the sum of AaAa+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

这道题的大致题目意思是:

首先给你两个整数N,Q;N代表的是你有几个数,然后Q代表的是有几个操作:

然后只有两种操作类型,"C a b c"代表的意思是:给A[a]到A[b]的数都加上一个数c。 "Q a b”的意思是让你求下标为a到b的和。

线段树裸题,但是是第一次做,所以还是写写吧,而且网上线段树模板鱼龙混杂,我也是费了好大力气才找到一个适合我的模板的。

在此附上链接:http://www.tuicool.com/articles/BVVfi2E

如果不懂线段树的可以先去学习一下上面那个人写的内容然后这道题我想就很简单了吧。

Source Code

Problem: 3468		User: hclmaster
Memory: 8352K		Time: 2110MS
Language: C++		Result: Accepted
Source Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 111111
typedef long long ll;
ll ans=0;
ll N,Q;
struct node{
	ll l,r;
	ll add,sum;
}tree[maxn*4];
void pushdown(ll v){
	ll temp=v*2;
	tree[temp].add+=tree[v].add;
	tree[temp+1].add+=tree[v].add;
	tree[temp].sum+=tree[v].add*(tree[temp].r-tree[temp].l+1);
	tree[temp+1].sum+=tree[v].add*(tree[temp+1].r-tree[temp+1].l+1);
	tree[v].add=0;
}
void pushup(ll v){
	ll temp=2*v;
	tree[v].sum=tree[temp].sum+tree[temp+1].sum;
}
void build(int l,int r,int v){
	tree[v].l=l;
	tree[v].r=r;
	tree[v].add=0;
	if(l==r){
		scanf("%lld",&tree[v].sum);
		return ;
	}
	int temp=v<<1;
	int mid=(l+r)>>1;
	build(l,mid,temp);
	build(mid+1,r,temp+1);
	pushup(v);
}
void query(ll l,ll r,ll v){
	if(r<tree[v].l||l>tree[v].r) return ;
	if(l<=tree[v].l&&tree[v].r<=r){
		ans+=tree[v].sum;
		return;
	}
	if(tree[v].add){
		pushdown(v);
	}
	ll temp=v<<1;
	ll mid=(tree[v].l+tree[v].r)>>1;
	if(r<=mid) query(l,r,temp);
	else if(l>mid) query(l,r,temp+1);
	else{
		query(l,mid,temp);
		query(mid+1,r,temp+1);
	}
}
void update(ll l,ll r,ll v,ll x){
	if(r<tree[v].l||tree[v].r<l) return ;
	if(l<=tree[v].l&&tree[v].r<=r){
		tree[v].add+=x;
		tree[v].sum+=x*(tree[v].r-tree[v].l+1);
		return ;
	}
	if(tree[v].add){
		pushdown(v);
	}
	ll temp=v<<1;
	update(l,r,temp,x);
	update(l,r,temp+1,x);
	pushup(v);
}
int main(){
	while(~scanf("%d%d",&N,&Q)){
		build(1,N,1);
		char ss[6];
		while(Q--){
			scanf("%s",ss);
			if(ss[0]=='Q'){
				ll l,r;
				scanf("%lld%lld",&l,&r);
				ans=0;
				query(l,r,1);
				printf("%lld\n",ans);
			}
			else{
				ll l,r,x;
				scanf("%lld%lld%lld",&l,&r,&x);
				update(l,r,1,x);
			}
		}
	}
}

没有什么新的东西是刚开始就能够得心应手的,所以慢慢的进步吧,加油,hades!

时间: 2024-11-07 09:21:46

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

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 62431   Accepted: 19141 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 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

POJ 3468 A Simple Problem with Integers //线段树的成段更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   Accepted: 17974 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 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

POJ 3468 A Simple Problem with Integers(线段树区间更新区间求和)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   Accepted: 20818 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: 63565   Accepted: 19546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations.

POJ 3468 A Simple Problem with Integers 线段树 区间更新 区间查询

题目链接: http://poj.org/problem?id=3468 题目描述: 一组数列, 可进行一段区间加上某一个数, 和区间查询 解题思路: 线段树, 之前的那道题是求总区间直接输出sum[1] 就可以了, 这次有了区间查询, 同理, 查询的时候Pushdown 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <map

(简单) POJ 3468 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. 题意