#HDU 6315 2018多校练习赛第二场 Naive Operations (线段树 + 算贡献)

Naive Operations

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


 

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

 题目大意 :

有两个长度为N的序列a, b, b序列为N的一种排列, a序列初始全为0, 现在有两种询问 + 操作, 一是将a序列的L到R区间每个数加 1, 二是输出  \sum _{L}^{R} Ai/Bi

思路 :要想Ai / Bi的值更新,一定是Ai的值加到能够整除Bi, 所以维护两个值,一个是区间的整除次数,表示贡献,而是区间最小值,用来判断是否需要更新贡献。当被包含区间的最小值 > 1时,直接让最小值 - 1, lazy标记下就好, 否则的话,就遍历到叶子结点,更新叶子结点的贡献,并将该点的最小值更新为原来的Bi,而这道题之所以说序列B是N的全排列,原因就在于保证每次最多只有一个点会产生贡献,否则的话可能就超时了

Accepted code

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

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Tree
{
	int l, r, bi, ans, lzy;
}t[MAXN * 4];
int c[MAXN], n, m;
char op[10];
void Build(int rt, int l, int r) {
	t[rt].l = l, t[rt].r = r;
	t[rt].lzy = t[rt].ans = 0;
	if (l == r) {
		sc("%d", &c[l]); t[rt].bi = c[l];
		return;
	}
	int mid = (l + r) >> 1;
	Build(ls, l, mid);
	Build(rs, mid + 1, r);
	t[rt].bi = min(t[ls].bi, t[rs].bi);
}
void Pushdown(int rt) {
	t[ls].lzy += t[rt].lzy, t[rs].lzy += t[rt].lzy;  // lazy标记
	t[ls].bi += t[rt].lzy, t[rs].bi += t[rt].lzy;
	t[rt].lzy = 0;
}
void Update(int rt, int l, int r) {
	if (t[rt].l >= l && t[rt].r <= r && t[rt].bi > 1) {  // 不需要更新
		t[rt].bi--; t[rt].lzy--;
		return;
	}
	if (t[rt].l == t[rt].r && t[rt].bi == 1) {  // 到叶子结点再更新,然后回溯上去
		t[rt].ans++; t[rt].bi = c[t[rt].l];
		return;
	}
	int mid = (t[rt].l + t[rt].r) >> 1;
	if (t[rt].lzy) Pushdown(rt);
	if (mid < l) Update(rs, l, r);
	else if (mid >= r) Update(ls, l, r);
	else {
		Update(ls, l, mid);
		Update(rs, mid + 1, r);
	}
	t[rt].bi = min(t[ls].bi, t[rs].bi);
	t[rt].ans = t[ls].ans + t[rs].ans;
}
int Query(int rt, int l, int r) {  // 查询
	if (t[rt].l >= l && t[rt].r <= r) return t[rt].ans;
	int mid = (t[rt].l + t[rt].r) >> 1, ui = 0, vi = 0;
	if (t[rt].lzy) Pushdown(rt);
	if (mid >= l) ui = Query(ls, l, r);
	if (mid < r) vi = Query(rs, l, r);
	return ui + vi;
}

int main()
{
	while (~sc("%d %d", &n, &m)) {
		Build(1, 1, n);
		for (int i = 0; i < m; i++) {
			int ui, vi; sc("%s %d %d", op, &ui, &vi);
			if (op[0] == 'a') Update(1, ui, vi);  
			else printf("%d\n", Query(1, ui, vi));
		}
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章