Fzu 2171 defensive position II

Source: Internet
Author: User

E-defensive position II
Time Limit: 3000 Ms memory limit: 32768kb 64bit Io format: % i64d & % i64u
Submit

Status
Description
There are a total of N soldiers in the army, each of which has its own capacity index XI,
During a drill, the headquarters determined m locations to be defended,
The headquarters will select M soldiers to go to the designated location for defensive tasks in turn,
The obtained reference index is the sum of the capabilities of M soldiers.

Over time, the headquarters will issue Q commands to replace m of soldiers defending,
The capability index of each soldier who completes the defensive task will drop by 1 due to fatigue and other reasons.

Now the soldiers are in a row. Please calculate the reference index of each defender.

Input
The input contains multiple groups of data.

The first line contains two integers, n, m, and Q (1 <=n <= 100000,1 <= m <= 100000 ,1 <= q <= ),
The N integers in the second row indicate the capability index XI (1 <= xi <= 1000) of each soldier ).

Next, row Q contains an integer x,
In the original queue, M soldiers starting with X are replaced with previous soldiers for defense. (1 <= x <= N-M + 1)

Output
Output Q rows, each row has an integer, which is the reference index of soldiers who defend each command execution.

Sample Input
5 3 3
2 1 3 1 4
1
2
3
Sample output
6
3
5

 

 

At the beginning, I saw a little bit of excitement. The result array was not opened four times. =

 

I have asked Wang daemon before. It is time-out to use the line segment tree to modify the interval.

At present, it seems that only templates can be set and cannot be used smoothly.

 

#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<stack>#define mem(a,b) memset(a,b,sizeof(a))#define ll __int64#define MAXN 1000#define INF 0x7ffffff#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int sum[400000+10];// n<<2!!!!!int add[1000000];void pushup(int rt){    //if(sum[rt<<1]<0) sum[rt<<1]=0;    //if(sum[rt<<1|1]<0) sum[rt<<1|1]=0;    sum[rt]=sum[rt<<1] +sum[rt<<1|1];}void pushdown(int rt,int m){    if(add[rt])    {        add[rt<<1]+=add[rt];        add[rt<<1|1]+=add[rt];        sum[rt<<1]+=add[rt]*(m-(m>>1));        sum[rt<<1|1]+=add[rt]*(m>>1);        //if(sum[rt<<1]<0) sum[rt<<1]=0;        //if(sum[rt<<1|1]<0) sum[rt<<1|1]=0;        add[rt]=0;    }}void build(int l,int r,int rt){    if(l==r)    {        scanf("%d",&sum[rt]);        return ;    }    int m=(l+r)>>1;    build(lson); build(rson); pushup(rt);}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R)    {        add[rt]+=c;        sum[rt]+=c*(r-l+1);        return ;    }    pushdown(rt,r-l+1);    int m=(l+r)>>1;    if(R<=m) update(L,R,c,lson);    else if(L>m) update(L,R,c,rson);    else    {        update(L,R,c,lson);        update(L,R,c,rson);    }    pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return sum[rt];    }    pushdown(rt,r-l+1);    int m=(l+r)>>1;    int ret=0;    if(R<=m) ret+=query(L,R,lson);    else if(L>m) ret+=query(L,R,rson);    else    {        ret+=query(L,R,lson);        ret+=query(L,R,rson);    }    return ret;}int main(){    int i,j,n,m,q,wen,jian;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        mem(add,0);        build(1,n,1);        for(i=1;i<=q;i++)        {                scanf("%d",&wen);            printf("%d\n",query(wen,wen+m-1,1,n,1));            update(wen,wen+m-1,-1,1,n,1);        }    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.