Nowcoder 第十四届华中科技大学程序设计竞赛决赛同步赛 M

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
In 8102, Alisa took fishing as her business. When she was walking along the bank and forgot the distance gradually, she came upon a peach forest and found an interesting thing. These trees lining up in one row can be numbered from 1 to N, and each one had a color. Alisa sometimes likes color X, and she will color the trees in [L,R] to the same color X, or sometimes wants to query the maximum elegant values of [L,R] after being cut into at most k+1 subintervals. The elegant value of an interval is defined as the sum of the number of different consecutive digits in each subinterval.
For example:

The elegant value of 2 2 2 3 3 is 2 and it becomes 4 after cutting twice. (You can cut it into {2} {2} {2 3 3} or {2} {2 2 3} {3} or {2 2} {2 3} {3})

The first line contains two integers N and M(1≤N,M≤2×105), the number of trees and the number of operations.
The second line contains N integers ai (1≤ai≤109), the color of the i-th tree.
Then m lines follow.
1. Format of the query 1 L R X. You need to color the tree in [L,R] to the same color X.
2. Format of the query 2 L R k. You should output the elegant value of [L,R] cutting k times.
It is guaranteed that 1≤L≤R≤n,1≤X≤109,0≤k≤R-L.
For each query of the second type, print the maximum elegant value in a single line.
3 3
1 2 3
2 1 3 1
1 1 3 1
2 1 3 1
3
2

被题解一句话点破智商。知道智商的不够后就是 裸线段树了,没事好好看看。

/*
线段树维护一个区间里有几段,维护区间左端点和右端点(用来合并)
切分可以就是把有连续的地方都切,有多少地方连续可以通过长度-段数算出来。
*/
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long int ll;
const int maxn = 200000;
int num[maxn*4+5],lx[maxn*4+5],rx[maxn*4+5],sv[maxn*4+5];
int n,m,a[maxn+5];
void maintain(int o)
{
    int lch = o*2, rch = o*2+1;
    num[o] = num[lch]+num[rch];
    if(rx[lch]==lx[rch]) num[o]--;
    lx[o] = lx[lch],  rx[o] = rx[rch];
}
void pushdown(int o)
{
    int ls = o*2, rs = o*2+1;
    if(sv[o]!=-1)
    {
        num[ls] = num[rs] = 1;
        lx[ls] = rx[ls] = sv[o];
        lx[rs] = rx[rs] = sv[o];
        sv[ls] = sv[rs] = sv[o];
        sv[o] = -1;
    }
}
void build(int o,int l,int r)
{
    sv[o] = -1;
    if(l==r)
    {
        num[o] = 1, lx[o] = rx[o] = a[l];
        return;
    }
    int mid = (l+r)>>1;
    build(o*2,l,mid);
    build(o*2+1,mid+1,r);
    maintain(o);
}
void updata(int o,int l,int r,int L,int R,int x)
{
    if(L<=l&&r<=R)
    {
        sv[o] = x;
        num[o] = 1;
        lx[o] = rx[o] = x;
    }
    else
    {
        pushdown(o);
        int mid = (l+r)>>1;
        if(R<=mid) updata(o*2,l,mid,L,R,x);
        else
        {
            if(L>mid) updata(o*2+1,mid+1,r,L,R,x);
            else
            {
                updata(o*2,l,mid,L,R,x);
                updata(o*2+1,mid+1,r,L,R,x);
            }
        }
        maintain(o);
    }
}
int query(int o,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return num[o];
    }
    else
    {
        pushdown(o);
        int mid = (l+r)>>1;
        if(R<=mid) return query(o*2,l,mid,L,R);
        else if(L>mid) return query(o*2+1,mid+1,r,L,R);
        else
        {
            int na,nb,nc;
            na = query(o*2,l,mid,L,R);
            nb = query(o*2+1,mid+1,r,L,R);
            nc = na+nb;
            if(rx[o*2] == lx[o*2+1]) nc--;
            return nc;
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    build(1,1,n);
    for(int i=1; i<=m; i++)
    {
        int opt,l,r,x;
        scanf("%d %d %d %d",&opt,&l,&r,&x);
        if(opt==1)
        {
            updata(1,1,n,l,r,x);
        }
        else
        {
            int nk = query(1,1,n,l,r), mk = r-l+1-nk,ans = 0;
            if(mk<=x) ans = nk+mk;
            else ans = nk+x;
            printf("%d\n",ans);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值