HDU 6315 Naive Operations(线段树)

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1244    Accepted Submission(s): 519


 

Problem Description

In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋

 

 

Input

There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.

 

 

Output

Output the answer for each 'query', each one line.

 

 

Sample Input

 

5 12 1 5 2 4 3 add 1 4 query 1 4 add 2 5 query 2 5 add 3 5 query 1 5 add 2 4 query 1 4 add 2 5 query 2 5 add 2 2 query 1 5

 

 

Sample Output

 

1 1 2 4 4 6

思路:
保存区间的sum,最大的a和(maxa)最小的b(minb), 若maxa<minb则没有必要向下更新
否则一直向下更新,一直到达要更新的点,然后tree[rt].sum++,tree[rt].minb+=b[l]。
代码:
 
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
struct node
{
	int sum,maxa,minb,add;
}tree[maxn<<2];
int n,Q;
int b[maxn];
void push_up(int rt)
{
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
	tree[rt].maxa=max(tree[rt<<1].maxa,tree[rt<<1|1].maxa);
	tree[rt].minb=min(tree[rt<<1].minb,tree[rt<<1|1].minb);
}
void push_down(int rt)
{
	if(tree[rt].add)
	{
		tree[rt<<1].add+=tree[rt].add;
		tree[rt<<1|1].add+=tree[rt].add;
		tree[rt<<1].maxa+=tree[rt].add;
		tree[rt<<1|1].maxa+=tree[rt].add;
		tree[rt].add=0;
	}
}
void build(int l,int r,int rt)
{
	tree[rt].add=0;
	if(l==r)
	{
		tree[rt].sum=0;
		tree[rt].maxa=0;
		tree[rt].minb=b[l];
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	push_up(rt);
}
void update(int L,int R,int l,int r,int rt)
{
	if(l>=L&&r<=R)
	{
		tree[rt].maxa++;
		if(tree[rt].maxa<tree[rt].minb)
		{
			 tree[rt].add++;
			 return;
		}
		if(l==r&&tree[rt].maxa>=tree[rt].minb)
		{
			tree[rt].sum++;
			tree[rt].minb+=b[l];
			return;
		}
	}
	push_down(rt);
	int mid=(l+r)/2;
	if(L<=mid) update(L,R,l,mid,rt<<1);
	if(R>mid) update(L,R,mid+1,r,rt<<1|1);
	push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
	if(l>=L&&r<=R) return tree[rt].sum;
	push_down(rt);
	int mid=(l+r)/2;
	int ans=0;
	if(L<=mid) ans+=query(L,R,l,mid,rt<<1);
	if(R>mid) ans+=query(L,R,mid+1,r,rt<<1|1);
	return ans;
}
int main()
{
	while(~scanf("%d%d",&n,&Q))
	{
		for(int i=1;i<=n;i++) scanf("%d",&b[i]);
		build(1,n,1);
		while(Q--)
		{
			char t[10];int x,y;
			scanf("%s%d%d",&t,&x,&y);
			if(t[0]=='a') update(x,y,1,n,1);
			else printf("%d\n",query(x,y,1,n,1));
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值