HDU 6315 线段树-维护ai/bi多校第二场1007

Naive Operations

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


 

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

 

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <list>
#define INF 0x3f3f3f3f
#define maxn 105000
#define maxnn 6000
#define juzheng 300
#define line cout << "-------------------------" << endl;
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define fill_(a,b,n) fill(a,a + n,b)
#define esp 1e-9

#define ri(n) scanf("%d",&n)
#define ri2(a,b) scanf("%d %d",&a,&b)
#define ri3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define rd(n) scanf("%lf",&n)
#define rd2(a,b) scanf("%lf %lf",&a,&b)
#define rd3(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define rl(n) scanf("%lld",&n)
#define rl2(a,b) scanf("%lld %lld",&a,&b)
#define rl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pr(n) cout << n << endl
#define ll long long
#define int64 __int64

using namespace std;

//Date:2018-7-26
//Author:HarryBlackCat

/*线段数模板*/
struct node {
	int l,r;
	ll sum,lazy,maxA,minB;
} tree[maxn * 4]; // 开四倍大小

int b[maxn];
char str[maxn];

void push_up(int root) {
	tree[root].sum = tree[root << 1].sum + tree[(root << 1) | 1].sum;
	tree[root].maxA = max(tree[root << 1].maxA,tree[(root << 1) | 1].maxA);
	tree[root].minB = min(tree[root << 1].minB,tree[(root << 1) | 1].minB);
}   // 儿子的值相加为父亲的值

void push_down(int root) {
	if(tree[root].lazy) { //有懒惰值
		tree[root << 1].lazy += tree[root].lazy;
		tree[(root << 1) | 1].lazy += tree[root].lazy;//向下传递懒惰值
		tree[root << 1].maxA += tree[root].lazy;
		tree[(root << 1) | 1].maxA += tree[root].lazy; //根据题目要求处理
		tree[root].lazy = 0;
	}
}

void build_tree(int l,int r,int root) {
	tree[root].lazy = 0;
	tree[root].l = l;
	tree[root].r = r;
	if(l == r) {
		tree[root].minB = b[l]; // 这里给一个初始值
		tree[root].maxA = 0;
		tree[root].sum = 0;
		return ;
	}
	int mid = (l + r) >> 1;
	build_tree(l,mid,root << 1);
	build_tree(mid + 1,r,(root << 1) | 1);
	push_up(root); //向上更新值
}

void update_interval(int l,int r,int v,int root) { //区间更新
	if(l <= tree[root].l && r >= tree[root].r) {
		tree[root].maxA += v;

		if(tree[root].maxA < tree[root].minB) {
			tree[root].lazy += v;
			return;
		}
		if(tree[root].l == tree[root].r && tree[root].maxA >= tree[root].minB) {
			tree[root].minB += b[tree[root].l];
			tree[root].sum++;
			return ;
		}

	}

	push_down(root);
	int mid = (tree[root].l + tree[root].r) >> 1;
	if(l <= mid)
		update_interval(l,r,v,root << 1);
	if(r > mid)
		update_interval(l,r,v,(root << 1) | 1);
	push_up(root);
}

int query_interval(int l,int r,int root) { // 区间查询
	if(l <= tree[root].l && r >= tree[root].r)
		return tree[root].sum;

	push_down(root);// 查询中遇到未更新的值,进行更新
	int mid = (tree[root].l + tree[root].r) >> 1;
	int ans = 0;
	if(l <= mid)
		ans += query_interval(l,r,root << 1);
	if(r > mid)
		ans += query_interval(l,r,(root << 1) | 1);

	return ans;
}

void update_single(int p,int v,int root) { //单点更新
	if(tree[root].l == tree[root].r && tree[root].r == p) {
		tree[root].sum = v;
		return ;
	}
	int mid = (tree[root].l + tree[root].r) >> 1;
	if(p <= mid)
		update_single(p,v,root << 1);
	else
		update_single(p,v,(root << 1) | 1);
	push_up(root);  //每个点更新都要向上更新值
}

int query_single(int p,int root) { //单点查询
	if(tree[root].l == tree[root].r && tree[root].r == p) {
		return tree[root].sum;
	}

	push_down(root);
	int mid = (tree[root].l + tree[root].r) >> 1;
	if(p <= mid)
		query_single(p,root << 1);
	else
		query_single(p,(root << 1) | 1);
}


int main() {
	//cin.sync_with_stdio(false);//降低cin,cout时间
	int n,q;
	while(~ri2(n,q)) {
		for(int i = 1; i <= n ; i++)
			ri(b[i]);

		build_tree(1,n,1);

		int l,r;
		while(q--) {
			scanf("%s",str);
			ri2(l,r);

			if(!strcmp(str,"add")) {
				update_interval(l,r,1,1);
			} else {
				pr(query_interval(l,r,1));
			}
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值