HDU - 3971 Play With Sequence 线段树 权值范围修改

题目链接:点击查看

When the girl was solving GSSX, a serious of tough problems about data structure on SPOJ, something intriguing once again comes to GYZ's mind. That is, for a changing sequences, how to count how many elements in a specific range efficiently. 

Without any beneficial idea, as usual, GYZ asks her friend, CLJ for help. But this time, unfortunately, CLJ is playing a gal-game at present, does not have sparse time. 

So now , it is your turn... 

Cause the original problem is not as easy as first glance, let's examine a simplified one: 

you are given a sequence A[1], A[2],..., A[N]. On this sequence you have to apply M operations: Add all the elements whose value are in range [l, r] with d or, ask for a query how many element are in range [l, r]. 
 

Input

There are only one test case, Process until the end of the file. The first line of each case contains two numbers, N, M, described as above. And then start from the second line, have N numbers described the sequence's initial value. 

( 1≤ N ≤ 250,000, M ≤ 50,000), |A[i]|≤ 1,000,000,000 .) 

The following M lines described the operation: 

C l r d: Add all the element whose value are in range [l, r] with d. (Redeclare: Not its Position! .. ) Q l r: ask for a query how many elements, whose value are in range [l, r]. 

( l ≤ r, |l|,|r|,|d|≤ 1,000,000,000 ) 

We guarantee every elements are suits 32-integer, and will not cause overflow, even during the running-time. (.. but still be careful ;) Besides, all the test-data are generated randomly. 
 

Output

For each query, print the result. Example

Sample Input

10 10
10 4 -5 8 8 3 0 -2 4 7
C -9 8 2
C -4 10 -3
C -10 0 5
Q -9 -1
C -9 -5 8
C -7 4 3
Q -2 7
C -10 -3 2
C -4 -1 -6
Q 7 10

Sample Output

1
10
4

题意:n个数,m次询问,C l r x表示大小在[l,r]范围内的数+x,Q l r表示大小在[l,r]范围内的数多少

题解:这种题第一次做,看着像套路,因为你线段树维护后,一修改,区间就会不是连续的了,在继续修改,连续区间就会越来越少,这样耗时会很大,所以这里卡了下时间进行排序,题目n是250000,m是50000,每次排序nlog(n),所以我们可以进行10-20次排序,这个题多试了几次,大约在每进行3000次操作排个序耗时比较少733ms。

#include <bits/stdc++.h>
using namespace std;
const int N=250010;
struct node{
	int l,r;
	int mx,mi;
	int lz;
}tree[N<<2];
int n,m,rt[N];
void pushup(int cur)
{
	tree[cur].mi=min(tree[cur<<1].mi,tree[cur<<1|1].mi);
	tree[cur].mx=max(tree[cur<<1].mx,tree[cur<<1|1].mx);
}
void pushdown(int cur)
{
	if(tree[cur].lz!=0)
	{
		tree[cur<<1].mi+=tree[cur].lz;
		tree[cur<<1].mx+=tree[cur].lz;
		tree[cur<<1].lz+=tree[cur].lz;
		
		tree[cur<<1|1].mi+=tree[cur].lz;
		tree[cur<<1|1].mx+=tree[cur].lz;
		tree[cur<<1|1].lz+=tree[cur].lz;
			
		tree[cur].lz=0; 
	} 
}
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	tree[cur].lz=0;
	if(l==r)
	{
		tree[cur].mi=tree[cur].mx=rt[l];
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur); 
}
void update(int pl,int pr,int cur,int val)
{
	if(tree[cur].mi>pr||tree[cur].mx<pl) return;
	if(pl<=tree[cur].mi&&tree[cur].mx<=pr)
	{
		tree[cur].lz+=val;
		tree[cur].mi+=val;
		tree[cur].mx+=val;
		return;
	}
	pushdown(cur);
	update(pl,pr,cur<<1,val);
	update(pl,pr,cur<<1|1,val);
	pushup(cur);

}
int query(int pl,int pr,int cur)
{
	if(tree[cur].mi>pr||tree[cur].mx<pl) return 0;
	if(tree[cur].mi>=pl&&tree[cur].mx<=pr) return tree[cur].r-tree[cur].l+1;
	pushdown(cur);
	int res=0;
	res+=query(pl,pr,cur<<1);
	res+=query(pl,pr,cur<<1|1);
	return res;
}
void R(int cur)
{
	if(tree[cur].l==tree[cur].r)
	{
		rt[tree[cur].l]=tree[cur].mi;
		return;
	}
	pushdown(cur);
	R(cur<<1);
	R(cur<<1|1);
}
int main()
{
	int l,r,x;
	char op[2];
	scanf("%d%d",&n,&m);
	
		for(int i=1;i<=n;i++) scanf("%d",&rt[i]);
		sort(rt+1,rt+1+n);
		build(1,n,1);
		for(int i=1;i<=m;i++)
		{
			if(i%3000==0)
			{
				R(1);
				sort(rt+1,rt+1+n);
				build(1,n,1);
			}
			scanf("%s%d%d",op,&l,&r);
			if(op[0]=='C')
			{
				scanf("%d",&x);
				update(l,r,1,x);	
			}
			else
			{
				printf("%d\n",query(l,r,1));
			}
		}
		
		
	return 0;
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值