hdu6315 Naive Operations(线段树)

Naive Operations

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


 

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

 

 

Source

2018 Multi-University Training Contest 2

 

解题思路

线段树维护以下元素

struct T{
	int l, r;   //区间范围 
	int sum;    //区间和 
	int minn;   //此区间最少还需要增加多少,sum才会增加 
	int lazy;   //懒惰标记 
}tree[maxn];

每次更新区间的时候,让minn--,当minn等于0的时候,说明此区间的sum应该增加了,且应该更新新的minn,因为这个增加sum的叶子节点i需要bi才能再增加sum了。所以此时就要找到minn为0了的叶子节点i,使sum++,minn变为bi,再向上更新。

代码如下

#include <iostream>
#include <cstdio>
#define maxn 400005
using namespace std;
typedef long long ll;
struct T{
	int l, r;   //区间范围 
	int sum;    //区间和 
	int minn;   //此区间最少还需要增加多少次sum才会增加 
	int lazy;   //懒惰标记 
}tree[maxn];
int a[100005];
void build(int l, int r, int k)
{
	tree[k].l = l;
	tree[k].r = r;
	if(l == r){
		tree[k].minn = a[l];
		tree[k].lazy = tree[k].sum = 0;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, 2*k);
	build(mid + 1, r, 2*k+1);
	tree[k].minn = min(tree[2*k].minn, tree[2*k+1].minn);
	tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;
	tree[k].lazy = 0;
}
void down(int k)
{
	tree[2*k].minn -= tree[k].lazy;
	tree[2*k+1].minn -= tree[k].lazy;
	tree[2*k].lazy += tree[k].lazy;  //+=
	tree[2*k+1].lazy += tree[k].lazy;
	tree[k].lazy = 0;
}
void work(int k)
{
	if(tree[k].l == tree[k].r){
		tree[k].minn = a[tree[k].l];
		tree[k].sum ++;
		return;
	}
	if(tree[k].lazy)
		down(k);
	if(tree[2*k].minn == 0)
		work(k*2);
	if(tree[k*2+1].minn == 0)
		work(k*2+1);
	tree[k].minn = min(tree[2*k].minn, tree[2*k+1].minn);
	tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;
}
void update(int l, int r, int k)
{
	if(tree[k].l >= l && tree[k].r <= r){
		tree[k].lazy ++;
		tree[k].minn --;
		if(tree[k].minn == 0)
			work(k);
		return;		
	}
	if(tree[k].lazy)
		down(k);
	int mid = (tree[k].l + tree[k].r) / 2;
	if(l <= mid)
		update(l, r, 2*k);
	if(r > mid)
		update(l, r, 2*k+1);
	tree[k].minn = min(tree[2*k].minn, tree[2*k+1].minn);
	tree[k].sum = tree[2*k].sum + tree[2*k+1].sum; 
}
int query(int l, int r, int k)
{
	if(tree[k].l >= l && tree[k].r <= r){
		return tree[k].sum;
	}
	if(tree[k].lazy)
		down(k);
	int mid = (tree[k].l + tree[k].r) / 2;
	int sum = 0;
	if(l <= mid)
		sum += query(l, r, 2*k);
	if(r > mid)
		sum += query(l, r, 2*k+1);
	return sum;
}
int main()
{
	int n, q;
	while(scanf("%d%d", &n, &q) != EOF){
		for(int i = 1; i <= n; i ++)
			scanf("%d", &a[i]);
		build(1, n, 1);
		for(int i = 1; i <= q; i ++){
			char str[10];
			int l, r;
			scanf("%s%d%d", str, &l, &r);
			if(str[0] == 'a'){
				update(l, r, 1);
			}
			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、付费专栏及课程。

余额充值