【线段树区间更新】 Naive Operations HDU6315 2018多校第二场

Naive Operations

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


 

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

 

 

/// 每个节点保存amax及bmin 若amax < bmin 无法更新
/// 若 amax >= bmin 不一定是否更新
/// lazy标记保存区间 + 1 次数

#include <bits/stdc++.h>
using namespace std;

const int mn = 1e5 + 10;

int b[mn];

int id[mn];
struct node
{
	int l, r;
	int amax, bmin;
	int lazy, sum;
} t[8 * mn];

void pushup(int i)
{
	t[i].amax = max(t[2 * i].amax, t[2 * i + 1].amax);
	t[i].bmin = min(t[2 * i].bmin, t[2 * i + 1].bmin);
	t[i].sum = t[2 * i].sum + t[2 * i + 1].sum;
}

void build(int l, int r, int i)
{
	t[i].l = l, t[i].r = r;
	t[i].lazy = 0;
    if (l == r)
	{
		id[l] = i;
		t[i].sum = 0;
		t[i].amax = 0;
		t[i].bmin = b[l];
		return;
	}
	int m = (l + r) / 2;
	build(l, m, 2 * i);
	build(m + 1, r, 2 * i + 1);
	pushup(i);
	return;
}

void pushdown(int i)
{
	t[2 * i].lazy += t[i].lazy;
	t[2 * i + 1].lazy += t[i].lazy;
	t[2 * i].amax += t[i].lazy;
	t[2 * i + 1].amax += t[i].lazy;
	t[i].lazy = 0;
}

void update(int x, int y, int i)
{
	int l = t[i].l, r = t[i].r;
	int m = (l + r) / 2;

	if (x <= l && y >= r)  /// 区间包含时更新
	{
		t[i].amax++;
		if (t[i].amax < t[i].bmin)
		{
			t[i].lazy++;
			return;
		}
	}

	if (l == r)
	{
		if (t[i].amax >= t[i].bmin)
		{
			t[i].sum++;
			t[i].bmin += b[l];
		}
		return;
	}

	if (t[i].lazy > 0)
		pushdown(i);

	if (y <= m)
		update(x, y, 2 * i);
	else if (x > m)
		update(x, y, 2 * i + 1);
	else
	{
		update(x, m, 2 * i);
		update(m + 1, y, 2 * i + 1);
	}
	pushup(i);
	return;
}

int query(int x, int y, int i)
{
	int l = t[i].l, r = t[i].r;
	int m = (l + r) / 2;

	if (x == l && r == y)
		return t[i].sum;

	if (t[i].lazy > 0)
		pushdown(i);

    if (y <= m)
		return query(x, y, 2 * i);
	else if (x > m)
		return query(x, y, 2 * i + 1);
	else
		return query(x, m, 2 * i) + query(m + 1, y, 2 * i + 1);
}

int main()
{
	int n, q;
	while (~scanf("%d %d", &n, &q))
	{
		for (int i = 1; i <= n; i++)
			scanf("%d", &b[i]);

		build(1, n, 1);
        while (q--)
		{
			char ch[7];
			int x, y;
			scanf("%s %d %d", ch, &x, &y);
			if (ch[0] == 'a')
				update(x, y, 1);
			else if (ch[0] == 'q')
				printf("%d\n", query(x, y, 1));
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值