8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D635 - Factory Repairs 线段树

再一次比赛结束写完…sad
感觉这个题目描述很尴尬啊,一开始说机器每天能生产a个东西,但是有些零件坏了每天只能生产b个东西。
但是我并没有看到说只有1个机器,而且一开始是坏的,Pi是修理的日期,看了20~30分钟的题也是无语了….
猜了一下题意,感觉是线段树,等终测结束再交吧SAD

这里写图片描述
好吧 果然A了 sad

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
#define maxn 200005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
const double PAI=acos(-1.0);
int A[maxn];
int tree1[maxn*4];
int tree2[maxn*4];
int a,b,n;
void build(int l,int r,int index)
{
    tree1[index]=0;
    tree2[index]=0;
    int mid=(l+r)/2;
    if(l==r) return ;
    build(l,mid,index*2);
    build(mid+1,r,index*2+1);
}
void Push_up1(int index)
{
    tree1[index]=tree1[index*2]+tree1[index*2+1];
}
void Push_up2(int index)
{
    tree2[index]=tree2[index*2]+tree2[index*2+1];
}
void insert(int l,int r,int x,int val,int index)
{
    if(l==r&&l==x)
    {
        tree1[index]+=val;
        tree1[index]=min(tree1[index],a);
        tree2[index]+=val;
        tree2[index]=min(tree2[index],b);
        return ;
    }
    int mid=(l+r)/2;
    if(x>mid) insert(mid+1,r,x,val,index*2+1);
    else insert(l,mid,x,val,index*2);
    Push_up1(index);
    Push_up2(index);
}
int query(int l,int r,int left,int right,int t,int index)
{
    //printf("**%d %d %d %d %d %d\n",l,r,left,right,t,index);
    if(left>n||right==0) return 0;
    if(left<=l&&r<=right)
    {
        if(t==1) return tree1[index];
        else return tree2[index];
    }
    int mid=(l+r)/2;
    int ans=0;
    if(left<=mid) 
    ans+=query(l,mid,left,right,t,index*2);
    if(right>mid)
    ans+=query(mid+1,r,left,right,t,index*2+1);
    return ans;
}
int main()
{
    int k,q,i,x,y;
    scanf("%d%d%d%d%d",&n,&k,&a,&b,&q);
    build(1,n,1);
    rfor(i,1,q)
    {
        scanf("%d",&x);
        if(x==1)
        {
            scanf("%d%d",&x,&y);
            insert(1,n,x,y,1);
        }
        else
        {
            scanf("%d",&x);
            printf("%d\n",query(1,n,1,x-1,2,1)+query(1,n,x+k,n,1,1));
        }
    }
    return 0;
}

你可能感兴趣的:(codeforces)