P5117 [USACO18DEC] The Bucket List B

 

 只需要O(n)就可以完成 单点查询+区间修改

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;
const int maxn=1e5+10;

int n,a[1111],ans=0,tmp;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        int s,t,v;
        cin>>s>>t>>v;
    //    l=min(l,s),r=max(r,t);
        
        a[s]+=v,a[t+1]-=v;
    }
    
    for(int i=1;i<=1000;i++) ans=max(ans,a[i]+tmp),tmp+=a[i];
    cout<<ans<<'\n';
    return 0;
}
sol2 差分

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;
const int maxn=1e5+10;

int n,a[1111],ans=0,l,r;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        int s,t,v;
        cin>>s>>t>>v;
        l=min(l,s),r=max(r,t);
        for(int j=s;j<=t;j++) a[j]+=v;
    }
    
    for(int i=l;i<=r;i++) ans=max(ans,a[i]);
    cout<<ans<<'\n';
    return 0;
}
sol 1

 

 
 
 
posted @ 2023-07-29 09:37  JMXZ  阅读(34)  评论(0)    收藏  举报