首页 > 其他分享 >hdu:LCIS(线段树+区间合并)

hdu:LCIS(线段树+区间合并)

时间:2023-05-15 20:45:33浏览次数:48  
标签:hdu LCIS int max 线段 rmax len lmax maxn

Problem Description
Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].

Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=10^5).
The next line has n integers(0<=val<=10^5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10^5)
OR
Q A B(0<=A<=B< n).

Output
For each Q, output the answer.

Sample input
1
10 10
7 7 3 3 5 9 9 8 1 8
Q 6 6
U 3 4
Q 0 1
Q 0 5
Q 4 7
Q 3 5
Q 0 2
Q 4 6
U 6 10
Q 0 9

Sample output
1
1
4
2
3
1
2
5

数据结构-区间合并线段树

区间合并,将最长连续序列由其左右子区间的关系得出

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Segtree{
    int l,r,len;
    int maxn;
    int lmax;
    int rmax;
}tr[N<<2];

int w[N];

void pushup(int rt)
{
    auto &s=tr[rt];
    auto &a=tr[rt<<1];
    auto &b=tr[rt<<1|1];
    s.l=a.l;
    s.r=b.r;
    s.len=a.len+b.len;
    
    if(w[a.r]>=w[b.l]) 
    {
        s.lmax=a.lmax;
        s.rmax=b.rmax;
        s.maxn=max(a.maxn,b.maxn);//注意此处应该为左右子树LCIS的max值
    }
    else
    {
        s.lmax=(a.lmax==a.len)?a.len+b.lmax:a.lmax;
        s.rmax=(b.rmax==b.len)?b.len+a.rmax:b.rmax;
        s.maxn=max(a.maxn,max(a.rmax+b.lmax,b.maxn));
    }
}

void build(int l,int r,int rt)
{
    auto &s=tr[rt];
    if(l==r) 
    {
        s.l=s.r=l;
        s.lmax=s.rmax=s.maxn=s.len=1;
        return ;
    }
    
    int m=(l+r)>>1;
    
    build(l,m,rt<<1);build(m+1,r,rt<<1|1);
    
    pushup(rt);
}

void update(int t,int l,int r,int rt)
{
    auto &s=tr[rt];
    if(l==r) return ;
    int m=(l+r)>>1;
    if(t<=m) update(t,l,m,rt<<1);
    else update(t,m+1,r,rt<<1|1);
    pushup(rt);
}

int query(int L,int R,int l,int r,int rt)
{
    auto &s=tr[rt];
    auto &a=tr[rt<<1];
    auto &b=tr[rt<<1|1];
    int ans=0;
    if(L>r||R<l) return 0;
    if(L<=l&&R>=r) return s.maxn;
    int m=(l+r)>>1;
    if(L<=m) ans=max(ans,query(L,R,l,m,rt<<1));
    if(R>m) ans=max(ans,query(L,R,m+1,r,rt<<1|1));
    if(L<=m&&R>m&&w[a.r]<w[b.l])
    ans=max(ans,min(a.rmax,m-L+1)+min(b.lmax,R-m));
    return ans;
}
void solve()
{
    int n,m;
    cin>>n>>m;
    
    for(int i=1;i<=n;++i)
    cin>>w[i];
    
    build(1,n,1);
    
    for(int i=0;i<m;++i)
    {
        char op;
        cin>>op;
        int p,q;
        cin>>p>>q;
        if(op=='Q') 
        {
          cout<<query(p+1,q+1,1,n,1)<<'\n';
        }
        else 
        {
            w[p+1]=q;
            update(p+1,1,n,1);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
}

标签:hdu,LCIS,int,max,线段,rmax,len,lmax,maxn
From: https://www.cnblogs.com/ruoye123456/p/17403036.html

相关文章

  • hdu:这是真正的水题(RMQ)
    ProblemDescription在缺水的地方,水是非常有限的资源,所以人们常常为争夺最大的水源而战。给定一系列水源,用a1,a2,a3,…,an代表水源的大小。给定一组查询,每个查询包含2整数L和R,请找出L和R之间最大的水源。Input输入数据首先给定一个整数T(T≤10)表示测试用例的数量。......
  • hdu:Party(2-SAT)
    ProblemDescription有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n个人同时列席?Inputn:表示有n对夫妻被邀请(n<=1000)m:表......
  • hdu:Let's go home(2-SAT)
    ProblemDescription小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。——余光中集训是辛苦的,道路是坎坷的,休息还是必须的。经过一段时间的训练,lcy决定让大家回家放松一下,但是训练还是得照常进行,lcy想出了如下回家规定,每一个队(三人一队)或者队长留下或者其余两名队员同时留下;每......
  • hdu:How far away ?(树链剖分)
    ProblemDescriptionTherearenhousesinthevillageandsomebidirectionalroadsconnectingthem.Everydaypeolealwaysliketoasklikethis“HowfarisitifIwanttogofromhouseAtohouseB”?Usuallyithardtoanswer.Butluckilyintthisvilla......
  • hdu:Arbitrage(最短路变形)
    ProblemDescriptionArbitrageistheuseofdiscrepanciesincurrencyexchangeratestotransformoneunitofacurrencyintomorethanoneunitofthesamecurrency.Forexample,supposethat1USDollarbuys0.5Britishpound,1Britishpoundbuys10.0......
  • hdu:六度分离(最短路)
    ProblemDescription1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(smallworldphenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为“六度分离”理论(sixdegreesofsepa......
  • hdu:求和(逆元)
    ProblemDescriptionApex实验室里培养了很多种类的细菌。细菌的繁殖遵循如下规则:第k种细菌在第t个单位时间内新增的数量为k^t。例如,k=2,t=4时,第种细菌的总数为2+4+8+16。现在,实验室里一共有n种细菌,分别为1,2,3,…,n。在第1单位时间结束后,第k种细菌的个数为k个。求第m个单位时......
  • 线段树合并 学习笔记
    算法两棵动态开点线段树,直接把一棵线段树上的信息合并到另一棵树上。递归合并:如果某个节点两棵都有,合并,然后递归下去。否则直接返回节点。复杂度证明记权值线段树值域范围为\(m\),\(n\)次插入操作。因为动态开点,一次插入会新增\(\log_2m\)个节点,总节点数\(n\ti......
  • 线段树
    线段树--解决区间问题的数据结构,相比于树状数组,更具有普适性;完全二叉树的性质:根节下标为1,,节点为i的节点,左子节点为2*i,右子节点为2*i+1;代表nums中单个元素的节点tree[x]应当在树的最底层,即叶子节点;更大的区间从叶子节点开始向上构成;代表区间【L,R】的节点tree【i】,左子节点tre......
  • hdu:XOR(线性基)
    XOR数学-线性基设原集为S,线性基为P,若|S|>|P|,则异或会出现0。若|P|==n,则会产生2^n个不同数(包括0)而线性基不包含0元素,故若|S|中元素可以异或出0,k需要自减点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e4+10;lla[N......