#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;
}

 

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