2 条题解

  • 0
    @ 2025-10-8 17:00:39

    by hansang:

    #include <bits/stdc++.h>
    using namespace std;
    #define lp p<<1
    #define rp (p<<1)|1
    const int N=4e4+10;
    typedef long long LL;
    struct node{int l, r; LL f[2][2];} tr[N<<2];
    void pushup(int p){
    	tr[p].f[0][0]=max({tr[lp].f[0][1]+tr[rp].f[0][0], 
    	tr[lp].f[0][0]+tr[rp].f[1][0], tr[lp].f[0][0]+tr[rp].f[0][0]});
    	
    	tr[p].f[0][1]=max({tr[lp].f[0][1]+tr[rp].f[0][1], 
    	tr[lp].f[0][0]+tr[rp].f[1][1], tr[lp].f[0][0]+tr[rp].f[0][1]});
    	
    	tr[p].f[1][0]=max({tr[lp].f[1][0]+tr[rp].f[1][0], 
    	tr[lp].f[1][1]+tr[rp].f[0][0], tr[lp].f[1][0]+tr[rp].f[0][0]});
    	
    	tr[p].f[1][1]=max({tr[lp].f[1][0]+tr[rp].f[1][1], 
    	tr[lp].f[1][1]+tr[rp].f[0][1], tr[lp].f[1][0]+tr[rp].f[0][1]});
    }
    LL a[N];
    void bt(int p, int l, int r){
    	tr[p].l=l; tr[p].r=r;
    	memset(tr[p].f, 0, sizeof(tr[p].f));
    	if(l==r){
    		tr[p].f[1][1]=a[l]; 
    		return ;
    		
    	}
    	int mid=(l+r)>>1;
    	bt(lp, l, mid); bt(rp, mid+1, r);
    	pushup(p);
    }
    void change(int p, int x, LL c){
    	if(tr[p].l>x || tr[p].r<x) return ;
    	if(tr[p].l==tr[p].r) {tr[p].f[1][1]=c; return ;}
    	change(lp, x, c); change(rp, x, c);
    	pushup(p);
    }
    int main(){
    	int n, q; scanf("%d%d", &n, &q);
    	for(int i=1; i<=n; i++) scanf("%lld", &a[i]);
    	bt(1, 1, n); LL ans=0;
    	for(int i=1; i<=q; i++){
    		int t; LL d; scanf("%d%lld", &t, &d);
    		change(1, t, d);
    		ans+=max({tr[1].f[0][0], tr[1].f[0][1], 
    		tr[1].f[1][0], tr[1].f[1][1]});
    	}
    	printf("%lld\n", ans);
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:00:15

      by hansang:

      #include<bits/stdc++.h>
      using namespace std;
      #define lp p<<1
      #define rp (p<<1)|1
      const int N=4e4+10;
      typedef long long LL;
      struct node{int l, r; LL f[2][2];} tr[N<<2];
      void pushup(int p){
      	tr[p].f[0][0]=max({tr[lp].f[0][1]+tr[rp].f[0][0], 
      	tr[lp].f[0][0]+tr[rp].f[1][0], tr[lp].f[0][0]+tr[rp].f[0][0]});
      	
      	tr[p].f[0][1]=max({tr[lp].f[0][1]+tr[rp].f[0][1], 
      	tr[lp].f[0][0]+tr[rp].f[1][1], tr[lp].f[0][0]+tr[rp].f[0][1]});
      	
      	tr[p].f[1][0]=max({tr[lp].f[1][0]+tr[rp].f[1][0], 
      	tr[lp].f[1][1]+tr[rp].f[0][0], tr[lp].f[1][0]+tr[rp].f[0][0]});
      	
      	tr[p].f[1][1]=max({tr[lp].f[1][0]+tr[rp].f[1][1], 
      	tr[lp].f[1][1]+tr[rp].f[0][1], tr[lp].f[1][0]+tr[rp].f[0][1]});
      }
      LL a[N];
      void bt(int p, int l, int r){
      	tr[p].l=l; tr[p].r=r;
      	memset(tr[p].f, 0, sizeof(tr[p].f));
      	if(l==r){
      		tr[p].f[1][1]=a[l]; 
      		return ;
      	}
      	int mid=(l+r)>>1;
      	bt(lp, l, mid); bt(rp, mid+1, r);
      	pushup(p);
      }
      void change(int p, int x, LL c){
      	if(tr[p].l>x || tr[p].r<x) return ;
      	if(tr[p].l==tr[p].r) {tr[p].f[1][1]=c; return ;}
      	change(lp, x, c); change(rp, x, c);
      	pushup(p);
      }
      int main(){
      	int n, q; scanf("%d%d", &n, &q);
      	for(int i=1; i<=n; i++) scanf("%lld", &a[i]);
      	bt(1, 1, n); LL ans=0;
      	for(int i=1; i<=q; i++){
      		int t; LL d; scanf("%d%lld", &t, &d);
      		change(1, t, d);
      		ans+=max({tr[1].f[0][0], tr[1].f[0][1], 
      		tr[1].f[1][0], tr[1].f[1][1]});
      	}
      	printf("%lld\n", ans);
      	return 0;
      } 
      • 1

      USACO(55)线段树1:最优挤奶P3097 [USACO13DEC] Optimal Milking G

      信息

      ID
      2225
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      递交数
      22
      已通过
      8
      上传者