树状数组成段更新——POJ 3468

A Simple Problem with IntegersCrawling in process...
Crawling failed
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 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 abc" means adding c to each of Aa,
Aa+1, ... ,
Ab. -10000 ≤ c ≤ 10000.

"Q ab" 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

这里讲得很详细:点击打开链接

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=100000+10;
using namespace std;
typedef long long LL;
LL sum[MAXN];
LL c1[MAXN];
LL c2[MAXN];

int lowbit(int x)
{
	return x&(-x);
}

void update(LL *c, int x, LL val)
{
	while(x<=MAXN)
	{
		c[x]+=val;
		x+=lowbit(x);
	}
}

LL Sum(LL *c, int x)
{
	LL res=0;
	while(x>0)
	{
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}

LL SUM(int x)
{
	return sum[x]+(x+1)*Sum(c1,x)-Sum(c2,x);
}

int main()
{
	//reopen("in.txt","r",stdin);
	int n,m;
	while(scanf("%d%d", &n,&m)==2)
	{
		memset(c1,0,sizeof(c1));
		memset(c2,0,sizeof(c2));
		int i,j,k;
		for(i=1; i<=n; i++){
			scanf("%d", &j);
			sum[i]=sum[i-1]+j;
		}
		char ch[3];
		while(m--)
		{
			scanf("%s", ch);
			if(ch[0]=='Q'){
				scanf("%d%d", &i,&j);
				LL ans=SUM(j)-SUM(i-1);
				printf("%I64d\n", ans);
			}
			else{
				scanf("%d%d%d", &i,&j,&k);
				update(c1,i,k);
				update(c1,j+1,-k);
				update(c2,i,i*k);
				update(c2,j+1,-k*(j+1));
			}
		}
	}
	return 0;
}

树状数组成段更新——POJ 3468

时间: 2024-11-15 21:19:59

树状数组成段更新——POJ 3468的相关文章

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

题目传送门 1 /* 2 线段树-成段更新:裸题,成段增减,区间求和 3 注意:开long long:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using namespace std; 11 12 #define lson l, mid, rt <<

POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39795   Accepted: 11552 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

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

Count the Colors(线段树之区间成段更新)

萌萌哒的传送门 这道题目是线段树区间成段更新的应用,我们只需在建立线段树时从原来的左右儿子不相连,改为相连即可以解决此类问题. 如从原来的[l,mid] , [mid + 1,r] 改为 [l,mid],[mid,r]即可; /********************* * zoj1610 * * 线段树的区间成段更新 * 延迟标记 * *********************/ #include <iostream> #include <cstring> #include &l

Codeforces 390E Inna and Large Sweet Matrix 树状数组改段求段

题目链接:点击打开链接 题意:给定n*m的二维平面 w个操作 1.0 (x1,y1) (x2,y2) value for i : x1 to x2 for j : y1 to y2 mp[i][j] += value; 2.1 (x1, y1) (x2 y2) ans1 = 纵坐标在 y1,y2间的总数 ans2 = 横坐标不在x1,x2间的总数 puts(ans1-ans2); 因为n最大是4e6, 所以用树状数组改段求段代替线段树 #include <stdio.h> #include &

线段树入门之成段更新

作者:zifeiy 标签:线段树 HDU1698 Just a Hook 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树功能: update:成段更新(因为query操作只涉及一次总区间,所以可以直接输出1节点的信息) #include <bits/stdc++.h> using namespace std; #define lson l, m, rt<<1 #define rson m+1, r, rt<<

HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15889    Accepted Submission(s): 7897 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

【树状数组】段修改,点查询

利用差分,先得到一个差分序列(如:(1,2,3,5)的差分序列为(1,1,1,2)) 当[i,j]段中所有数均加上数m时,在差分序列的i位置加上m,在j+1位置减去m即可 求和利用树状数组 以下是pascal程序: var a,fai:array[0..1000] of longint; c:char; n,i,x,y,f,m:longint; function lowbit(x:longint):longint; begin lowbit:=x and (-x); end; procedure

树状数组的区间更新与查询

树状数组天生用来动态维护数组前缀和,其特点是每次更新一个元素的值,查询只能查数组的前缀和, 但这个题目求的是某一区间的数组和,而且要支持批量更新某一区间内元素的值,怎么办呢?实际上, 还是可以把问题转化为求数组的前缀和. 首先,看更新操作update(s, t, d)把区间A[s]...A[t]都增加d,我们引入一个数组delta[i],表示 A[i]...A[n]的共同增量,n是数组的大小.那么update操作可以转化为: 1)令delta[s] = delta[s] + d,表示将A[s].