HDU 5306 Gorgeous Sequence(线段树)

    xiaoxiao2025-11-06  5

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5306 三种操作: 0 x y t: 使这个区间的值变成min(t,a[i])。 1 x y: 区间最大值 2 x y: 区间和

    1和2都是常见操作,就是这个0操作很难搞定。参考了吉如一论文。感觉非常的强。

    区间的max可以看做是区间标记,我们可以维护3个值,区间最大值,区间次大值,区间最大值的个数。然后我们每次做0操作的时候,就会有3种情况。 把t作为要更新的值。 1:t>=区间最大值,那么就是无效操作。 2:区间次大值 < t <区间最大值,因为我们已经求得了最大值的个数,所以我们可以直接更新这段的sum。 3:其他情况。继续搜下去,直到搜到前两种情况为止。

    然后我们可以把区间的最大值看做是这个区间的标记,因为每次更新的时候只会更新到(区间次大值 < t < 区间最大值)的情况,而且因为没有加法操作,所以max从上到下一定是非递增的,那么我们在更新的时候就可以从每次pushdown的时候判断一下子节点和当前节点的最大值的情况,然后更新一下子节点的sum和max值。复杂度证明并不会,但是论文里说是O(mlogn)的。

    2000+ms AC

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <iostream> #include <vector> #include <map> #include <set> #include <stack> #include <queue> #include <ctime> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define pb push_back #define mp make_pair #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define calm (l+r)>>1 const int INF = 2139062143; const int maxn=1000010; int n,m; struct Seg{ ll sum[maxn<<2]; int mx[maxn<<2],mxnum[maxn<<2],sc[maxn<<2]; inline void pushup(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; mx[rt]=max(mx[rt<<1],mx[rt<<1|1]); mxnum[rt]=0; sc[rt]=max(sc[rt<<1],sc[rt<<1|1]); if(mx[rt]==mx[rt<<1])mxnum[rt]+=mxnum[rt<<1]; else sc[rt]=max(sc[rt],mx[rt<<1]); if(mx[rt]==mx[rt<<1|1])mxnum[rt]+=mxnum[rt<<1|1]; else sc[rt]=max(sc[rt],mx[rt<<1|1]); } inline void pushdown(int rt){ if(mx[rt]<mx[rt<<1]){ sum[rt<<1]-=(ll)mxnum[rt<<1]*(mx[rt<<1]-mx[rt]); mx[rt<<1]=mx[rt]; } if(mx[rt]<mx[rt<<1|1]){ sum[rt<<1|1]-=(ll)mxnum[rt<<1|1]*(mx[rt<<1|1]-mx[rt]); mx[rt<<1|1]=mx[rt]; } } void build(int l,int r,int rt){ if(l==r){ scanf("%I64d",&sum[rt]); mx[rt]=sum[rt]; mxnum[rt]=1; sc[rt]=-1; return; } int m=calm; build(lson);build(rson); pushup(rt); } void update(int L,int R,int v,int l,int r,int rt){ if(v>=mx[rt])return; if(L<=l&&r<=R&&sc[rt]<v){ sum[rt]-=(ll)mxnum[rt]*(mx[rt]-v); mx[rt]=v; return; } pushdown(rt); int m=calm; if(L<=m)update(L,R,v,lson); if(R>m)update(L,R,v,rson); pushup(rt); } int getMax(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){ return mx[rt]; } pushdown(rt); int m=calm,ans=0; if(L<=m)ans=getMax(L,R,lson); if(R>m)ans=max(ans,getMax(L,R,rson)); return ans; } ll query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){ return sum[rt]; } pushdown(rt); int m=calm; ll ans=0; if(L<=m)ans+=query(L,R,lson); if(R>m)ans+=query(L,R,rson); return ans; } }tree; int main(){ //freopen("D://input.txt","r",stdin); int T;scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); tree.build(1,n,1); while(m--){ int op,l,r; scanf("%d%d%d",&op,&l,&r); if(op==0){ int x;scanf("%d",&x); tree.update(l,r,x,1,n,1); } else if(op==1){ printf("%d\n",tree.getMax(l,r,1,n,1)); } else{ printf("%I64d\n",tree.query(l,r,1,n,1)); } } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1303900.html
    最新回复(0)