跳到正文

题解

题解:P7707 「Wdsr-2.7」百花齐放的太阳花田

ChthollyTree__约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年10月1日
收录
2026年10月4日
更新
2026年11月9日
篇幅
约 7 分钟
获赞
17
题目
P7707

版本

  1. v1当前

不难发现一个区间中,有意义的高度最多只有 O(len)\mathcal O(len) 种,其中 lenlen 为区间长度。

于是建立一颗线段树,每个节点维护其区间内所有出现过的 hh 所对应的答案的最左端类型 ll,最右端类型 rr,以及段数 sumsum。不难发现这样只会有 O(nlog⁡n)\mathcal O(n\log n) 个元素,原因就是一个叶节点最多只会出现在其 log⁡n\log n 个祖先节点中。

这样的话向上合并是简单的,但是时间复杂度是 O(len)\mathcal O(len) 的,如果每次后插都把整条到根的路径 push_up,那么时间复杂度为 O(n2)\mathcal O(n^2),无法接受。

但是我们可以后插时插满一个节点的整个区间时才更新这个节点,因为如果没插满的话查询时一定用不到这个节点,一定会向下递归。这样做就把插入部分的时间降到了 O(nlog⁡n)\mathcal O(n\log n)。

然后查询就是简单的,一直递归到完全覆盖的区间后二分出小于等于 xx 的最大的 hih_i 查询这个高度的答案即可。

最终时间复杂度 O(nlog⁡n+qlog⁡2n)\mathcal O(n\log n+q\log^2n)。

要注意的细节在代码注释中。

C++
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <utility>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <list>
#include <map>
#include <set>

#define c1(x) __builtin_popcount(x)
#define Code ios::sync_with_stdio(0);
#define by cin.tie(0);
#define ChthollyTree__ cout.tie(0);
#define rep(i,l,r) for(ri int i=l;i<=r;i++)
#define per(i,l,r) for(ri int i=r;i>=l;i--)
#define pb push_back
#define ri register
#define il inline
#define db double

using namespace std;

const int N=1e6+10;
int n,m,k,len,h[N],t[N];

struct node
{
    int l,r,sum,h;
    il bool operator<(const node &x) const
    {
        return h<x.h;
    }
};

il node operator+(node a,node b)
{
    if(!a.sum) return b;
    if(!b.sum) return a;
    return {a.l,b.r,a.sum+b.sum-(a.r==b.l),max(a.h,b.h)};
}

vector<node> tr[N<<2];

il void push_up(int u)
{
    int i=0,j=0;
    while(i<tr[u<<1].size()||j<tr[u<<1|1].size())
    {
        if(j>=tr[u<<1|1].size()||(i<tr[u<<1].size()&&tr[u<<1][i]<tr[u<<1|1][j]))//这一行要先判i<tr[u<<1].size()否则会RE
        {
            tr[u].pb(tr[u<<1][i]+(j>0?tr[u<<1|1][j-1]:node{0,0,0,0}));
            i++;
        }
        else
        {
            tr[u].pb((i>0?tr[u<<1][i-1]:node{0,0,0,0})+tr[u<<1|1][j]);
            j++;
        }
    }
}

il void update(int u,int l,int r,int p,int h,int t)
{
    if(l==r)
    {
        tr[u].pb({t,t,1,h});
        return;
    }

    int mid=l+r>>1;
    if(p<=mid) update(u<<1,l,mid,p,h,t);
    else update(u<<1|1,mid+1,r,p,h,t);
    if(p==r) push_up(u);
}

il node query(int u,int l,int r,int L,int R,int h)
{
    if(L<=l&&r<=R)
    {
        if(tr[u][0].h>h) return node{0,0,0,0};//一定不要忘记这行特判
        else return tr[u][upper_bound(tr[u].begin(),tr[u].end(),node{0,0,0,h})-tr[u].begin()-1];
    }
    int mid=l+r>>1;
    if(L<=mid&&R>mid) return query(u<<1,l,mid,L,R,h)+query(u<<1|1,mid+1,r,L,R,h);
    else if(L<=mid) return query(u<<1,l,mid,L,R,h);
    else return query(u<<1|1,mid+1,r,L,R,h);
}

int main()
{
    // freopen("sunflower.in","r",stdin);
    // freopen("sunflower.out","w",stdout);
    Code by ChthollyTree__
    cin>>n>>m>>k;
    len=n+m;
    rep(i,1,n) cin>>h[i];
    rep(i,1,n) cin>>t[i];
    rep(i,1,n) update(1,1,len,i,h[i],t[i]);
    int last=0,idx=n;
    while(m--)
    {
        int opt,l,r,h;
        cin>>opt>>l>>r;
        l^=k*last;
        r^=k*last;
        if(opt==1)
        {
            cin>>h;
            h^=k*last;
            cout<<(last=query(1,1,len,l,r,h).sum)<<"\n";
        }
        else
        {
            update(1,1,len,++idx,l,r);
        }
    }
    return 0;
}
/*4 3 0
2 5 1 4
1 2 1 2
1 1 4 3
2 3 2
1 1 5 5
*/
原文
存档评论6 条抓取于