HDU - 6315 Naive Operations

Naive Operations

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


 

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

 

 

Recommend

chendu

 

 

 

     这道题的话,有两种操作:

  1. 给区间[l,r][l,r]的aa序列全部加上11
  2. 查询∑ri=l⌊ai/bi⌋∑i=lr⌊ai/bi⌋的值

     我们可以用c数组来代表着l,r的区间里,能够让某一个ai/bi增加的最小值的;然后用sum数组来表示区间l,r里面的总值;

#include
#include
#include
#include
using namespace std;
#define maxn 100005
#define mid m=(l+r)>>1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int b[maxn];
int c[maxn << 2];
int sum[maxn << 2];
int mark[maxn << 2];
void pushup(int rt)
{
	c[rt] = min(c[rt << 1] - mark[rt << 1], c[rt << 1 | 1] - mark[rt << 1 | 1]);
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void pushdown(int rt)
{
	if (mark[rt])
	{
		mark[rt << 1] += mark[rt];
		mark[rt << 1 | 1] += mark[rt];
		mark[rt] = 0;
	}
}
void build(int l, int r, int rt)
{
	mark[rt] = 0;
	if (l == r)
	{
		c[rt] = b[l];
		sum[rt] = 0;
		return;
	}
	int mid;
	build(lson);
	build(rson);
	pushup(rt);
}
void cal(int l, int r, int rt)
{
	if (l == r)
	{
		c[rt] -= mark[rt];
		mark[rt] = 0;
		if (c[rt] <= 0)
			sum[rt]++, c[rt] = b[l];
		return;
	}
	if (mark[rt] >= c[rt])
	{
		pushdown(rt);
		int mid;
		cal(lson);
		cal(rson);
		pushup(rt);
	}
	return;
}
void updata(int L, int R, int d, int l, int r, int rt)
{
	if (L <= l && r <= R)
	{
		mark[rt] += d;
		cal(l, r, rt);
		return;
	}
	pushdown(rt);
	int mid;
	if (L <= m)
	{
		updata(L, R, d, lson);
	}
	if (R > m)
	{
		updata(L, R, d, rson);
	}
	pushup(rt);
}
int query(int L, int R, int l, int r, int rt)
{
	if (L <= l&&r <= R)
	{
		return sum[rt];
	}
	int ans = 0, mid;
	if (L <= m)
	{
		ans += query(L, R, lson);
	}
	if (R > m)
	{
		ans += query(L, R, rson);
	}
	return ans;
}
int main()
{
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		for (int s = 1; s <= n; s++)
			scanf("%d", b + s);
		build(1, n, 1);
		while (m--)
		{
			char q[10];
			scanf("%s", q);
			if (q[0] == 'q')
			{
				int a, b;
				scanf("%d%d", &a, &b);
				printf("%d\n", query(a, b, 1, n, 1));
			}
			else
			{
				int a, b, c;
				scanf("%d%d", &a, &b);
				updata(a, b, 1, 1, n, 1);
			}
		}
	}
}

 

你可能感兴趣的:(树形结构)