标签:math for print bsp tree update shu std %s
线段树染色问题简化版
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1e5+10;
char s[maxn];
int a[maxn];
struct note
{
int left,right,sum,lazy;
void up(int val)
{
sum=1<<(val-1);
lazy=val;
}
} tree[maxn*4];
void pushup(int id)
{
tree[id].sum=tree[id<<1].sum|tree[id<<1|1].sum;
}
void pushdown(int id)
{
if(tree[id].lazy)
{
tree[id<<1].up(tree[id].lazy);
tree[id<<1|1].up(tree[id].lazy);
tree[id].lazy=0;
}
}
void build(int id,int l,int r)
{
tree[id].left=l;
tree[id].right=r;
if(l==r)
tree[id].sum=1<<(a[l]-1);
else
{
int mid=(l+r)/2;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
pushup(id);
}
}
int query(int id,int l,int r)
{
if(l<=tree[id].left&&tree[id].right<=r)
return tree[id].sum;
pushdown(id);
int mid=(tree[id].left+tree[id].right)/2;
int ans=0;
if(l<=mid) ans|=query(id<<1,l,r);
if(r>mid) ans|=query(id<<1|1,l,r);
return ans;
}
void update(int id,int l,int r,int val)
{
if(l<=tree[id].left&&tree[id].right<=r)
{
tree[id].up(val);
return;
}
pushdown(id);
int mid=(tree[id].left+tree[id].right)/2;
if(l<=mid) update(id<<1,l,r,val);
if(r>mid) update(id<<1|1,l,r,val);
pushup(id);
}
int main()
{
scanf("%s",s+1);
int n;
scanf("%d",&n);
int len=strlen(s+1);
for(int i=1; i<=len; i++)
a[i]=s[i]-‘a‘+1;
build(1,1,len);
for(int i=1; i<=n; i++)
{
int op;
scanf("%d",&op);
if(op==1)
{
int pos;
char val;
scanf("%d %c",&pos,&val);
update(1,pos,pos,val-‘a‘+1);
}
else
{
int l,r;
scanf("%d%d",&l,&r);
int sum=query(1,l,r);
int ans=0;
while(sum>0)
{
if(sum&1) ans++;
sum=sum>>1;
}
printf("%d\n",ans);
}
}
}
Codeforces Round #590 (Div. 3)
标签:math for print bsp tree update shu std %s
原文地址:https://www.cnblogs.com/dongdong25800/p/11620417.html