Codeforces 903G 巧妙的线段树

思路:

巧妙的线段树

想方法将网络流往数据结构方向转化

http://www.cnblogs.com/yyf0309/p/8724558.html

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200050;
int n,m,q,X[N],y[N],tmp=1,xx,yy;
ll tree[N<<2],lazy[N<<2],x[N];
struct Node{int from,to,wei;}node[N];
bool operator<(Node a,Node b){return a.from<b.from;}
void insert(int l,int r,int pos,int L,int R,ll wei){
    if(l>=L&&r<=R){tree[pos]+=wei;lazy[pos]+=wei;return;}
    int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
    if(mid<L)insert(mid+1,r,rson,L,R,wei);
    else if(mid>=R)insert(l,mid,lson,L,R,wei);
    else insert(l,mid,lson,L,R,wei),insert(mid+1,r,rson,L,R,wei);
    tree[pos]=min(tree[lson],tree[rson])+lazy[pos];
}
int main(){
    scanf("%d%d%d",&n,&m,&q);
    for(int i=2;i<=n;i++)scanf("%lld%d",&x[i-1],&y[i]);
    for(int i=1;i<=m;i++)scanf("%d%d%d",&node[i].from,&node[i].to,&node[i].wei);
    for(int i=2;i<=n;i++)insert(1,n,1,i,i,y[i]);
    sort(node+1,node+1+m);
    for(int i=1;i<=n;i++){
        for(;tmp<=m&&node[tmp].from<=i;tmp++)
            insert(1,n,1,1,node[tmp].to,node[tmp].wei);
        X[i]=x[i],x[i]+=tree[1];
    }memset(tree,0,sizeof(tree)),memset(lazy,0,sizeof(lazy));
    for(int i=1;i<=n;i++)insert(1,n,1,i,i,x[i]);
    printf("%lld\n",tree[1]);
    while(q--){
        scanf("%d%d",&xx,&yy);
        insert(1,n,1,xx,xx,yy-X[xx]),X[xx]=yy;
        printf("%lld\n",tree[1]);
    }
}

猜你喜欢

转载自www.cnblogs.com/SiriusRen/p/9190975.html