2018 Multi-University Training Contest 2 Naive Operations

Naive Operations

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

 

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

 

题意:给一个b数组,为n的全排列,有一个初始化为零的a数组,有两种操作:

add x y 表示a数组中区间为[x,y]的元素全部加1

query x y 表示查找区间为[x,y]的a[i]/b[i]之和

 

可知每当a[i]加了b[i]次时a[i]/b[i]的值加一,可以维护一个线段树,表示当前区间内加1的情况,每当a[i]增加了b[i]次时更新,可以求得最多更新10000+10000/2+10000/3+...+1次。在维护一个树状数组求出答案。


#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
const int maxm = 100005;
struct node
{
	int l, r, mi, val;
}tr[maxm*8];
int c[maxm], b[maxm], n;
char str[maxm];
void add(int k, int val)
{
	while (k <= n)
		c[k] += val, k += k&-k;
}
int query(int k)
{
	int ans = 0;
	while (k)
		ans += c[k], k -= k&-k;
	return ans;
}
void make(int l, int r, int num)
{
	tr[num].l = l, tr[num].r = r;
	tr[num].val = 0;
	if (l == r)
	{
		tr[num].mi = b[l], tr[num].val = 0;
		return;
	}
	int mid = (tr[num].l + tr[num].r) / 2;
	make(l, mid, num * 2);
	make(mid + 1, r, num * 2 + 1);
	tr[num].mi = min(tr[num * 2].mi, tr[num * 2 + 1].mi);
}
void updown(int num)
{
	tr[num * 2].val += tr[num].val;
	tr[num * 2 + 1].val += tr[num].val;
	tr[num * 2].mi -= tr[num].val;
	tr[num * 2 + 1].mi -= tr[num].val;
	tr[num].val = 0;
}
void update(int l, int r, int num,int val)
{
	if (tr[num].l == l&&tr[num].r == r || val == 0)
	{
		tr[num].val += val, tr[num].mi -= val;
		if (tr[num].mi > 0) return;
		if (l != r)
		{
			updown(num);
			int mid = (tr[num].l + tr[num].r) / 2;
			update(l, mid, num * 2, 0);
			update(mid + 1, r, num * 2 + 1, 0);
			tr[num].mi = min(tr[num * 2].mi, tr[num * 2 + 1].mi);
		}
		else
		{
			tr[num].mi = b[l], tr[num].val = 0;
			add(l, 1);
		}
		return;
	}
	updown(num);
	int mid = (tr[num].l + tr[num].r) / 2;
	if (l > mid) update(l, r, num * 2 + 1, 1);
	else if (r <= mid) update(l, r, num * 2, 1);
	else
	{
		update(l, mid, num * 2, 1);
		update(mid + 1, r, num * 2 + 1, 1);
	}
	tr[num].mi = min(tr[num * 2].mi, tr[num * 2 + 1].mi);
}
int main()
{
	int  i, j, k, sum, m, x, y;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		for (i = 1;i <= n;i++)
		{
			scanf("%d", &b[i]);
			c[i] = 0;
		}
		make(1, n, 1);
		for (i = 1;i <= m;i++)
		{
			scanf("%s%d%d", str, &x, &y);
			if (str[0] == 'a')
				update(x, y, 1, 1);
			else printf("%d\n", query(y) - query(x - 1));
		}
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值