2018 Multi-University Training Contest 2(HDU 6315)

Naive Operations

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


 

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   |   We have carefully selected several similar problems for you:  6318 6317 6316 6315 6314 

 

题意:

       他会给你两个字符串a,b。a字符串全部都是零,b字符串由1到n组成,add(I,L)表示将a字符串中的I到L位全部加一,query(I,L)表示求从I到L中每个ai/bi的和。

题解:

       由于是用整除,所以ai/bi中的ai每次加一可以看作是bi减一(因为当有除零以外的答案时,ai和bi成倍数关系。),所以需要维护bi和ai/bi即可。当bi减少为0时将bi变为一开始的数值且sum加一。结果返回sum。其他地方套用线段树模版就可以了。

答案:

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

const int MAXNUM  = 1e5+5;

int b[MAXNUM<<2],sum[MAXNUM<<2];
int n;

struct Tree{
	int val;
	int addMark;
}Tree[MAXNUM<<2];

void bulid(int root  ,int start , int end){
	Tree[root].addMark = 0;
	sum[root] = 0;
	if(start == end){
		Tree[root].val = b[start];
	}else{
		int mid = (start + end)>>1;
		bulid(root<<1, start , mid);
		bulid(root<<1|1, mid + 1 , end);
		
		Tree[root].val = min(Tree[root<<1].val,Tree[root<<1|1].val);
		sum[root] = sum[root<<1] + sum[root<<1|1];
	}
}

void pushDown(int root){
	if(Tree[root].addMark != 0){
		Tree[root<<1].addMark += Tree[root].addMark;
		Tree[root<<1|1].addMark += Tree[root].addMark;
		
		Tree[root<<1].val -= Tree[root].addMark;
		Tree[root<<1|1].val -= Tree[root].addMark;
		
		Tree[root].addMark = 0;
	}
}

void update(int root,int istart , int iend , int start , int end){
	if(istart > end || iend < start){
		return;
	}
	if(Tree[root].val>1&&start<= istart && iend <= end){
		Tree[root].addMark++;
		Tree[root].val--;
		return;
	}
	if(Tree[root].val==1&&istart==iend){
		sum[root]++;
		Tree[root].addMark = 0;
		Tree[root].val = b[istart];
		return;
	}
	
	pushDown(root);
	
	int mid = (istart + iend)>>1;
	if(start<=mid){
		update(root<<1 , istart , mid , start , end);
	}
	if(mid<end){
		update(root<<1|1 , mid + 1 , iend , start , end);
	}
	
	Tree[root].val = min(Tree[root<<1].val,Tree[root<<1|1].val);
	sum[root] = sum[root<<1] + sum[root<<1|1];
}

int query(int root,int istart , int iend , int start , int end){
	if(start <= istart && iend <= end){
		return sum[root];
	}
	if(Tree[root].val==0){
		update(1,1,n,start,end);
	}
	pushDown(root);
	int ans = 0;
	int mid = (istart + iend)>>1;
	if(start<=mid){
		ans += query(root<<1, istart , mid , start ,end);
	}
	if(end>mid){
		ans += query(root<<1|1, mid + 1 , iend , start ,end);
	}
	Tree[root].val = min(Tree[root<<1].val,Tree[root<<1|1].val);
	sum[root] = sum[root<<1] + sum[root<<1|1];
	return ans;
}

int main(){
	int m,l,r;
	char s[10];
	while(~scanf("%d %d",&n,&m)){
		for(int i = 1 ; i <= n ; i++){
			scanf("%d",&b[i]);
		}
		bulid(1,1,n);
		for(int i = 0 ; i < m ; i++){
			scanf("%s%d%d",s,&l,&r);
			if(s[0]=='a'){
				update(1,1,n,l,r);
			}else{
				printf("%d\n",query(1,1,n,l,r));
			}
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值