cdoj1259 线段树+bitset

感觉题目简单,就看你的bitset怎么用了。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <stdio.h>
#include <bitset>
#define maxn 160005
using namespace std;
struct ee
{
    int l,r,lazy;
    bitset<120> s;
}tree[maxn*4];
int n,m;

void build_tree(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].lazy=0;
    tree[root].s.reset();
    if(l==r)
        return;
    int mid=(l+r)/2;
    build_tree(2*root,l,mid);
    build_tree(2*root+1,mid+1,r);
}

void pushdown(int a)
{

    tree[a].s.reset();
    tree[a].s|=tree[a*2].s;
    tree[a].s|=tree[a*2+1].s;
}

void pushup(int a,int l)
{
    tree[a*2].lazy=l;
    tree[a*2].s.reset();
    tree[a*2].s[l]=1;
    tree[a*2+1].lazy=l;
    tree[a*2+1].s.reset();
    tree[a*2+1].s[l]=1;
}

bitset<120> query(int root,int l,int r)
{
    if(tree[root].lazy!=0)
    {
        pushup(root,tree[root].lazy);
        tree[root].lazy=0;
    }
    if(l<=tree[root].l&&tree[root].r<=r)
        return tree[root].s;
    bitset<120> bit;
    bit.reset();
    int mid;
    mid=(tree[root].l+tree[root].r)/2;
    if(l<=mid)
    bit|=query(root*2,l,r);
    if(mid<r)
    bit|=query(root*2+1,l,r);
    return bit;
}

void update(int root,int l,int r,int v)
{
    if(l<=tree[root].l&&tree[root].r<=r)
    {
        tree[root].lazy=v;
        tree[root].s.reset();
        tree[root].s[v]=1;
        return;
    }
    if(tree[root].lazy!=0)
    {
        pushup(root,tree[root].lazy);
        tree[root].lazy=0;
    }
    int mid=(tree[root].l+tree[root].r)/2;
    if(l<=mid)
    {
        update(root*2,l,r,v);
    }
    if(mid<r)
    {
        update(root*2+1,l,r,v);
    }
    pushdown(root);
    //printf("tree[%d]=%d l=%d,r=%d\n",root,tree[root].s.count(),tree[root].l,tree[root].r);
}

int main()
{
    //freopen("d:\\in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int i;
    build_tree(1,1,n);
    for(i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        update(1,i,i,x);
    }
    int Q;
    scanf("%d",&Q);
    for(i=1;i<=Q;i++)
    {
        char ch[2];
        scanf("%s",ch);
        int a,b;
        scanf("%d%d",&a,&b);
        if(ch[0]=='M')
        {
            int c;
            scanf("%d",&c);
            update(1,a,b,c);
        }
        else
        {
           printf("%d\n",query(1,a,b).count());
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值