福州大學第十一屆程序設計競賽E fzuoj2171(線段樹)

點擊打開鏈接

Problem 2171 防守陣地 II

Accept: 109    Submit: 426
Time Limit: 3000 mSec    Memory Limit : 32768 KB

Problem Description

部隊中總共有N個士兵,每個士兵有各自的能力指數Xi,在一次演練中,指揮部確定了M個需要防守的地點,指揮部將選擇M個士兵依次進入指定地點進行防守任務,獲得的參考指數即爲M個士兵的能力之和。隨着時間的推移,指揮部將下達Q個指令來替換M個進行防守的士兵們,每個參加完防守任務的士兵由於疲憊等原因能力指數將下降1。現在士兵們排成一排,請你計算出每次進行防守的士兵的參考指數。

Input

輸入包含多組數據。

輸入第一行有兩個整數N,M,Q(1<=N<=100000,1<=M<=1000,1<=Q<=100000),第二行N個整數表示每個士兵對應的能力指數Xi(1<=Xi<=1000)。

接下來Q行,每行一個整數X,表示在原始隊列中以X爲起始的M個士兵替換之前的士兵進行防守。(1<=X<=N-M+1)

對於30%的數據1<=M,N,Q<=1000。

Output

輸出Q行,每行一個整數,爲每次指令執行之後進行防守的士兵參考指數。

Sample Input

5 3 3
2 1 3 1 4
1
2
3

Sample Output

6
3
5

線段樹模板題,區間更新,區間求和

#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<utility>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define Inf (1<<30)
#define LL long long
#define MOD 1000000009
#pragma comment(linker, "/STACK:102400000,102400000")  
using namespace std;
const int MM = 110005;
LL sum[MM << 2];
LL add[MM << 2];
void pushup(int o)
{
	sum[o] = sum[2 * o] + sum[2 * o + 1];
}
void pushdown(int o, int L, int R)
{
	if (add[o])
	{
		int mid = L + (R - L) / 2;
		add[2 * o] += add[o];
		add[2 * o + 1] += add[o];
		sum[2 * o] += add[o] * (mid - L + 1);
		sum[2 * o + 1] += add[o] * (R - mid);
		add[o] = 0;
	}
}
void build(int o, int L, int R)
{
	add[o] = 0;
	if (L == R)scanf("%lld",&sum[o]);
	else
	{
		int mid = L + (R - L) / 2;
		build(2 * o, L, mid);
		build(2 * o + 1, mid + 1, R);
		pushup(o);
	}
}
void update(int o, int L, int R, int x, int y, int z)
{
	if (x <= L&&R <= y)
	{
		sum[o] += z*(R - L + 1);
		add[o] += z;
	}
	else
	{
		int mid = L + (R - L) / 2;
		pushdown(o, L, R);
		if (x <= mid)update(2 * o, L, mid, x, y, z);
		if (mid < y)update(2 * o + 1, mid + 1, R, x, y, z);
		pushup(o);
	}
}
LL query(int o, int L, int R, int x, int y)
{
	if (x <= L&&R <= y)return sum[o];
	else
	{
		int mid = L + (R - L) / 2;
		LL res = 0; 
		pushdown(o, L, R);
		if (x <= mid)res += query(2 * o, L, mid, x, y);
		if (mid < y)res += query(2 * o + 1, mid + 1, R, x, y);
		return res;
	}
}
int main() {
	int n, m, q;
	//freopen("D:\\oo.txt","r",stdin);
	while (~scanf("%d%d%d", &n, &m, &q))
	{
		build(1, 1, n);
		while (q--)
		{
			int x;
			scanf("%d", &x);
			printf("%lld\n", query(1, 1, n, x, x + m - 1));
			update(1, 1, n, x, x + m - 1, -1);
		}
	}
	return 0;
}


發佈了123 篇原創文章 · 獲贊 14 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章