Fzu 2171 line segment tree Interval Update

Source: Internet
Author: User

Http://acm.fzu.edu.cn/problem.php? PID = 1, 2171

 

Problem 2171 defensive position IIAccept: 105 submit: 415
Time Limit: 3000 msec memory limit: 32768 kb Problem 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 reference index obtained is the sum of the capabilities of M soldiers. Over time, the headquarters will issue Q commands to replace m of soldiers defending. The performance 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 ).

In the next Q row, each row has an integer x, indicating that the previous soldiers are replaced with M soldiers starting with X in the original queue. (1 <= x <= N-M + 1)

For 30% of data 1 <= m, n, q <= 1000.

Output

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

Sample input5 3 32 1 3 1 4123 sample output635 ============================ ========================================== classic line segment tree interval updates, only one code is posted here. You still need to understand it.
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#define Maxx 100010using namespace std;int str[Maxx];long long ans;struct Node{    int left,right,add,maxnode,sum;};Node tree[Maxx<<2];void build(int l,int r,int rt){    tree[rt].left=l;    tree[rt].right=r;    tree[rt].add=0;    if(l == r)    {        tree[rt].sum=str[l];        return ;    }    int mid=(l+r)/2;    build(l,mid,rt*2);    build(mid+1,r,rt*2+1);    //return 0;    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;}void update(int l,int r,int rt,int add){    if(tree[rt].left>r || tree[rt].right<l)    {        return ;    }    if(tree[rt].left >=l&&tree[rt].right <=r)    {        tree[rt].sum+=(tree[rt].right-tree[rt].left+1)*add;        tree[rt].add+=add;        return ;    }    if(tree[rt].add)    {        tree[rt*2].sum+=(tree[rt*2].right-tree[rt*2].left+1)*tree[rt].add;        tree[rt*2].add+=tree[rt].add;        tree[rt*2+1].sum+=(tree[rt*2+1].right-tree[rt*2+1].left+1)*tree[rt].add;        tree[rt*2+1].add+=tree[rt].add;        tree[rt].add=0;    }    update(l,r,rt*2,add);    update(l,r,rt*2+1,add);    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;}void query(int l,int r,int rt){    if(tree[rt].left>r || tree[rt].right<l)    {        return ;    }    if(tree[rt].left>=l && tree[rt].right<=r)    {        ans+=tree[rt].sum;        return ;    }    if(tree[rt].add)    {        tree[rt*2].sum+=(tree[rt*2].right-tree[rt*2].left+1)*tree[rt].add;        tree[rt*2].add+=tree[rt].add;        tree[rt*2+1].sum+=(tree[rt*2+1].right-tree[rt*2+1].left+1)*tree[rt].add;        tree[rt*2+1].add+=tree[rt].add;        tree[rt].add=0;    }    query(l,r,rt*2);    query(l,r,rt*2+1);    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;    //return ans;}int main(){    int n,m,q;    int x,i,j;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        for(i=1;i<=n;i++)        {            scanf("%d",&str[i]);        }        build(1,n,1);        for(i=0;i<q;i++)        {            scanf("%d",&x);            ans=0;            query(x,x+m-1,1);            printf("%lld\n",ans);            update(x,x+m-1,1,-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.