BZOJ3179 [Coci2012]RASPORED

树状数组

题目传送门
学考爆炸飞天
%%%ZZP
YY一下就会发现最大肯定是按T从小到大做。因为每单位时间每个订单减少的贡献相同。
修改就是把T的位置改变一下,我们用两个树状数组来实现。

具体见代码:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200005
#define F inline
using namespace std;
typedef long long LL;
int n,m,L[N],T[N];
LL ans,s,t1[N],t2[N];
//t1维护T[i]所花费的时间,t2维护权值T[i]的个数
F char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    return l==r?EOF:*l++;
}
F int _read(){
    int x=0; char ch=readc();
    while (!isdigit(ch)) ch=readc();
    while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
    return x;
}
F void writec(LL x){
    if (x<0) putchar('-'),x=-x;
    if (x>9) writec(x/10); putchar(x%10+48);
}
F void _write(LL x){ writec(x),puts(""); }
#define lbt(x) ((x)&-(x))
F void mdfy(LL *t,int x,int y){ for (;x<=(N>>1);x+=lbt(x)) t[x]+=y; }
F LL srch(LL *t,int x){ for (s=0;x;x-=lbt(x)) s+=t[x]; return s; }
F void nsrt(int x){
    ans-=srch(t1,(N>>1)-x)*x,ans-=srch(t2,x)+x;
    mdfy(t1,(N>>1)-x,1),mdfy(t2,x+1,x);
}
F void dlt(int x){
    mdfy(t1,(N>>1)-x,-1),mdfy(t2,x+1,-x);
    ans+=srch(t1,(N>>1)-x)*x,ans+=srch(t2,x)+x;
}
int main(){
    n=_read(),m=_read();
    for (int i=1;i<=n;i++) ans+=(L[i]=_read()),nsrt(T[i]=_read());
    while (m--){
        _write(ans); int x=_read(),l=_read(),t=_read();
        ans-=L[x],ans+=(L[x]=l),dlt(T[x]),nsrt(T[x]=t);
    }
    return _write(ans),0;
}

猜你喜欢

转载自blog.csdn.net/a1799342217/article/details/80877216