HDu 6315 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が増加すべきであり,sumを増加させる葉ノードiがbiを必要とするため,新しいminnを更新すべきであることを示す.そこでminnが0になった葉ノードiを見つけてsum+,minnをbiにして上へ更新する.
コードは次のとおりです.
#include 
#include 
#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
", query(l, r, 1)); } } return 0; }