感觉题目简单,就看你的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;
}
本文介绍了一个使用bitset和线段树实现的高效区间更新与查询算法。该算法适用于处理大规模数据集上的区间修改及求和操作,通过实例演示了如何利用bitset的特性优化线段树的存储和更新效率。
732

被折叠的 条评论
为什么被折叠?



