• BZOJ_4378_[POI2015]Logistyka_树状数组


    BZOJ_4378_[POI2015]Logistyka_树状数组

    Description

    维护一个长度为n的序列,一开始都是0,支持以下两种操作:
    1.U k a 将序列中第k个数修改为a。
    2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作。
    每次询问独立,即每次询问不会对序列进行修改。

    Input

    第一行包含两个正整数n,m(1<=n,m<=1000000),分别表示序列长度和操作次数。
    接下来m行为m个操作,其中1<=k,c<=n,0<=a<=10^9,1<=s<=10^9。

    Output

    包含若干行,对于每个Z询问,若可行,输出TAK,否则输出NIE。

    Sample Input

    3 8
    U 1 5
    U 2 7
    Z 2 6
    U 3 1
    Z 2 6
    U 2 2
    Z 2 6
    Z 2 1

    Sample Output

    NIE
    TAK
    NIE
    TAK

    需要注意查询是在整个序列上的而不是给定区间。
    假设大于s的个数有k个,则剩下的那些权值和必须要大于等于(c-k)*s。
    于是我们把权值离散化然后用两个树状数组分别维护。
     
    代码:
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define N 1000050
    #define RR register
    typedef long long ll;
    int n,m,t[N],maxn=1000000000,h[N],p[N];
    ll c[N][2];
    char opt[10];
    inline int rd() {
        RR int x=0,f=1; RR char s=getchar();
        while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
        while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+s-'0';s=getchar();}
        return x*f;
    }
    struct A {
        int num,v,id,opt,pos;
    }a[N];
    inline bool cmp1(const A &x,const A &y){return x.num<y.num;}
    inline bool cmp2(const A &x,const A &y){return x.id<y.id;}
    void fix(int x,int v,int flg) {
        for(;x<=m;x+=x&(-x)) c[x][flg]+=v;
    }
    ll inq(int x,int flg) {
        ll re=0;
        for(;x;x-=x&(-x)) re+=c[x][flg];
        return re;
    }
    int main() {
        n=rd(); m=rd();
        int i,j;
        for(i=1;i<=m;i++) {
            scanf("%s",opt);
            if(opt[0]=='U') {
                a[i].opt=1; a[i].id=i; a[i].pos=rd(); a[i].num=rd();
            }else {
                a[i].opt=2; a[i].id=i; a[i].pos=rd(); a[i].num=rd();
            }
        }
        sort(a+1,a+m+1,cmp1); a[0].num=134234;
        for(j=0,i=1;i<=m;i++) {
            if(a[i].num!=a[i-1].num) j++;
            a[i].v=j;
            h[j]=a[i].num;
        }
        sort(a+1,a+m+1,cmp2);
        for(i=1;i<=m;i++) {
            if(a[i].opt==1) {
                int t=a[i].pos;
                if(p[t]) {
                    fix(p[t],-1,1);
                    fix(p[t],-h[p[t]],2);
                }
                p[t]=a[i].v;
                fix(p[t],1,1);
                fix(p[t],h[p[t]],2);
            }else {
                int k=inq(m,1)-inq(a[i].v-1,1);
                if(k>=a[i].pos) {
                    puts("TAK"); continue;
                }
                ll sum=inq(a[i].v-1,2);
                puts(sum>=1ll*a[i].num*(a[i].pos-k)?"TAK":"NIE");
            }
        }
    }
    
  • 相关阅读:
    C#
    C#
    ssh学习笔记
    (已解决)Could not open '/var/lib/nova/mnt/*/volume-*': Permission denied
    RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in major version and less than or equal in minor version as the specified version cap 4.11.
    如何在linux下安装idea
    The system has no LUN copy license
    调整mysql数据库最大连接数
    mysql数据库编码问题
    cinder支持nfs快照
  • 原文地址:https://www.cnblogs.com/suika/p/8967241.html
Copyright © 2020-2023  润新知