hdu 4391 线段树

好神啊。解题报告说是哈希,我记得有个人跟我说过分块可以做线段树不能做的,只是复杂度高一点而已。。。。我用线段树过的。。。。。。刚开始把rt << 1写成rt >> 1了,调了一会。。。。。。,后来又T了,于是就开了一个mx和mn记录一个区间中颜色最大的。如果要查询的颜色不在这个区间里面,我们就直接不查了。

/*
Pro: 0

Sol:

date:
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <vector>
#define maxn   100100
#define havem int m = (l + r) >> 1
#define lson l,m,rt << 1
#define rson m+ 1, r, rt << 1|1
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
int n,m,col[maxn << 2],val[maxn << 2],mx[maxn << 2] ,mn[maxn << 2],op,L,R,c;
void push_up(int rt){
    if(col[ls] == -1 && col[rs] == -1 && val[ls] == val[rs])  { col[rt] = -1;val[rt] = val[ls]; }
    else {  col[rt] = 0; val[rt] = -1; }
    mx[rt] = max(mx[ls],mx[rs]);
    mn[rt] = min(mn[ls],mn[rs]);
}
void build(int l, int r, int rt){
    if(l == r){
        scanf("%d",&val[rt]);
        col[rt] = -1;//表示完全覆盖
        mn[rt] = mx[rt] = val[rt];
        return ;
    }havem; build(lson);    build(rson);
    push_up(rt);
}
void push_dn(int rt){
    if(col[rt] == -1){
        col[ls] = col[rs] = -1;
        val[ls] = val[rs] = val[rt];
        mx[ls] = mx[rs] = val[rt];
        mn[ls] = mn[rs] = val[rt];
        col[rt] = 0;
    }
}
void update(int L,int R,int c, int l, int r, int rt){
    if(L <= l && r <= R){
        col[rt] = -1; val[rt] = c; mx[rt] = c; mn[rt] = c; return ;
    }push_dn(rt);   havem;
    if(L <= m) update(L,R,c,lson);
    if(R > m) update(L,R,c,rson);
    push_up(rt);
}
//T了的话,那么是query的缘故
int query(int L,int R,int c, int l, int r, int rt){
    if(L <= l && r <= R){
        if(col[rt] == -1 && val[rt] == c) return r - l + 1;
        else if(c < mn[rt] || c > mx[rt]) return 0;
    }
//    if(l == r) {
//        if(val[rt] == c) return 1;
//        else return 0;
//    }
    push_dn(rt);   havem;
    int ans = 0;
    if(L <= m) ans = query(L,R,c,lson);
    if(R > m) ans += query(L,R,c,rson);
    push_up(rt);
    return ans;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        build(1,n,1);
        for(int i = 0; i < m; i ++){
            scanf("%d%d%d%d",&op,&L,&R,&c);
            L ++; R ++;
            if(op == 1)
                update(L,R,c,1,n,1);
            else
                printf("%d\n",query(L,R,c,1,n,1) );
        }
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值