FZU Problem 2171 防守阵地 II (裸线段树 懒惰标记)

 Problem 2171 防守阵地 II

Accept: 259    Submit: 987

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 32 1 3 1 4123

Sample Output

635

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>

#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define lc   idx<<1
#define rc   idx<<1|1
#define N    100010

using namespace std;

int n,m,q;
int sum[N<<2],st[N<<2];

void push_up(int idx) {
    sum[idx]=sum[lc]+sum[rc];
}

void build(int idx,int l,int r) {
    st[idx]=0;
    if(l==r) {
        scanf("%d",&sum[idx]);
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(idx);
}

void push_down(int idx,int d) {
    if(st[idx]<0) {
        st[lc]+=st[idx];
        st[rc]+=st[idx];
        sum[lc]+=(d-d/2)*st[idx];
        sum[rc]+=d/2*st[idx];
    }
    st[idx]=0;
}

void update(int idx,int l,int r,int x,int y,int k) {
    if(x<=l&&y>=r) {
        sum[idx]+=(r-l+1)*k;
        st[idx]+=k;
        return;
    }
    push_down(idx,r-l+1);
    int mid=(l+r)>>1;
    if(x<=mid)update(lson,x,y,k);
    if(y>mid)update(rson,x,y,k);
    push_up(idx);
}

int query(int idx,int l,int r,int x,int y) {
    if(x<=l&&y>=r)return sum[idx];
    push_down(idx,r-l+1);
    int mid=(l+r)>>1;
    int res=0;
    if(x<=mid)res+=query(lson,x,y);
    if(y>mid)res+=query(rson,x,y);
    return res;
}

int main() {
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&q)) {
        build(1,1,n);
        int s,e;
        while(q--) {
            scanf("%d",&s);
            e=s+m-1;
            printf("%d\n",query(1,1,n,s,e));
            update(1,1,n,s,e,-1);
        }
    }
    return 0;
}
时间: 2024-08-06 16:03:34

FZU Problem 2171 防守阵地 II (裸线段树 懒惰标记)的相关文章

FZU Problem 2171 防守阵地 II (线段树,区间更新)

 Problem 2171 防守阵地 II Accept: 143    Submit: 565Time Limit: 3000 mSec    Memory Limit : 32768 KB  Problem Description 部队中总共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,指挥部将选择M个士兵依次进入指定地点进行防守任务,获得的参考指数即为M个士兵的能力之和.随着时间的推移,指挥部将下达Q个指令来替换M个进行防守的士兵们,每个参加完防守

FZU Problem 2171 防守阵地 II (线段树区间更新模板题)

http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <stri

fzu 2171 防守阵地 II

首先想说的是PPT设计,现在还是有很多人只把PPT当成一个存放文字和图片的软件,说的更直接点就是当是一个可以全屏放映内容的软件.但是我想说的是PPT已经走向了设计类型的软件,当Microsoft office Powerpoint2010正式版出来的时候这种感觉更盛了.绚丽的动画和更强大的设计功能,相信微软也是以设计软件这一理念来做更深层的突破了吧.技术的不断突破也给增加了PPT设计的范围,不再是单调的报告,而开始走向了企业宣传,产品宣传,动画制作--专业的设计也不断的接合平面设计,动画设计,甚

FZU_Problem 2171 防守阵地 II

http://acm.fzu.edu.cn/problem.php?pid=2171 线段树模板题,成段增减,区间求和. 思路:先查询,后更新. #include<cstdio> #define lson l , m ,rt << 1 #define rson m+1 , r , rt << 1 | 1 const int maxn = 100002; int add[maxn << 2]; int sum[maxn << 2]; void Pu

FOJ 2171 防守阵地 II 区间求和区间查询 线段树

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2171 题意: 给定n长序列,常数m,q个询问 对于每个询问x 1.求[x, x+m-1] 区间和 2.[x,x+m-1]区间的所有元素-1 线段树裸题,不知为何全用longlong会re,只能改成部分longlong #include<stdio.h> #include<string.h> #define ll long long #define LL int #define L(x) (x*

FZU Problem 2168 防守阵地 I

http://acm.fzu.edu.cn/problem.php?pid=2168 题目大意: 给定n个数和m,要求从n个数中选择连续的m个,使得a[i]*1+a[i+1]*2+--a[i+m]*m最大 思路: 常规思路是以每个数开始,枚举m个,但是这样会TLE. 可以有O(n)的算法. 例如样例的 n=5 m=3 五个数分别为 2 1 3 1 4 有三种连续的三个数 2 * 1 + 1 * 2 + 3* 3 = 13 1 * 1 + 3 * 2 + 1 * 3= 10 3 * 1 + 1 *

FZU2171 防守阵地 II (线段树)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2170 贴个baka爷的代码留念.. 数据出的有问题,输入的字符串长度不超过1000 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #incl

福州大学 Problem 2168 防守阵地 I

http://acm.fzu.edu.cn/problem.php?pid=2168 最重要的是 dp[k]=dp[k-1]-ans[k-1]+x[i]*m; ans[k-1]是m个数求和.  Problem 2168 防守阵地 I Accept: 14    Submit: 20 Time Limit: 3000 mSec    Memory Limit : 32768 KB Problem Description 部队中共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each