CF上给的标签是数据结构。但给的题解里的方法是很巧的暴力,用vector<set>维护每个字母出现的下标,每次修改加下标,擦下标。每次询问对每个字母分别lower_bound查找区间内是否存在这样位置,实在太妙了!

先看题解的方法。

 #include <bits/stdc++.h>
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef long long ll;
const int MAXN=2e5+;
const int INF=0x3f3f3f3f; int main()
{
ios::sync_with_stdio(false);
cin.tie();
string s;
cin>>s;
vector<set<int> > alp();
for(int i=;i<s.size();++i)
{
alp[s[i] - 'a'].insert(i);
}
int q;
cin>>q;
while(q--)
{
int op;
cin>>op;
if(op==)
{
int pos;
char c;
cin>>pos>>c;
--pos;
alp[s[pos]-'a'].erase(pos);
s[pos]=c;
alp[s[pos]-'a'].insert(pos);
}
else
{
int l,r;
cin>>l>>r;
--l,--r;
int cnt=;
for(int i=;i<;++i)
{
auto k=alp[i].lower_bound(l);
if(k!=alp[i].end() && *k<=r) cnt++;
}
cout<<cnt<<endl;
}
}
return ;
}

然而我看别人线段树的代码没看懂。

但巧了我又去补了道 POJ - 2777

立马发现这两题有异曲同工之处。

POJ的题意是给线段染色,起始一种颜色,区间染色,最多30种颜色,询问区间中有几种颜色。应该是用了bitmask,用不同的位代表不同的颜色,每位上1代表存在这样的颜色,最后对所询问区间内按位或,再统计几位为1就为答案。

还要求你支持区间修改,这CF上这题只有要求单点更新的,很显然我们也可以用不同的位上的1代表不同的字母,最后将区间内的值按位或即可,最后统计有几位为1,就是这个区间几个不同的字母。

下见代码该题线段树解法。

 #include <iostream>
#define debug(x) cout << #x << ": " << x << endl
#define lson (rt<<1)
#define rson (rt<<1|1)
#define Lson l,m,lson
#define Rson m+1,r,rson
using namespace std;
typedef long long ll;
const int MAXN=1e5+;
const int INF=0x3f3f3f3f;
int seg[MAXN<<],lazy[MAXN<<];
string s; inline void pushup(int rt){seg[rt]=(seg[lson]|seg[rson]);} void pushdown(int rt)
{
if(lazy[rt])
{
seg[lson]=seg[rson]=lazy[rt];
lazy[lson]=lazy[rson]=lazy[rt];
lazy[rt]=;
}
} void build(int rt,int l,int r)
{
if(l==r) {seg[rt]=<<(s[l-]-'a');return;}
int mid=l+r>>;
build(lson,l,mid);
build(rson,mid+,r);
pushup(rt);
} void update(int rt,int l,int r,int L,int R,int item)
{
if(l>=L && r<=R) {seg[rt]=<<item;lazy[rt]=<<item;return;}
int mid=l+r>>;
pushdown(rt);
if(L<=mid) update(lson,l,mid,L,R,item);
if(R>mid) update(rson,mid+,r,L,R,item);
pushup(rt);
} int query(int rt,int l,int r,int L,int R)
{
if(l>=L && r<=R){return seg[rt];}
int mid=l+r>>;
pushdown(rt);
int ans1=,ans2=,ans=;
if(L<=mid) ans1=query(lson,l,mid,L,R);
if(R>mid) ans2=query(rson,mid+,r,L,R);
ans=ans1|ans2;
return ans; }
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int q;
cin>>s>>q;
int n=s.size();
build(,,n);
while(q--)
{
int op;
cin>>op;
if(op==)
{
int p;
string z;
cin>>p>>z;
int x=z[]-'a';
update(,,n,p,p,x);
}
else if(op==)
{
int L,R;
cin>>L>>R;
int cnt=,ans=query(,,n,L,R);
while(ans)
{
if(ans&) cnt++;
ans>>=;
}
cout<<cnt<<endl;
}
}
return ;
}

Codeforces Round #590 D. Distinct Characters Queries的更多相关文章

  1. Codeforces Round #590

    题目链接:Round #590 题目答案:官方Editorial.My Solution A. Equalize Prices Again 签到题还WA了一发,向上取整有点问题: //my wrong ...

  2. Codeforces Round #590 (Div. 3)

    A. Equalize Prices Again 题目链接:https://codeforces.com/contest/1234/problem/A 题意:给你 n 个数 , 你需要改变这些数使得这 ...

  3. Codeforces Round #590 (Div. 3)(e、f待补

    https://codeforces.com/contest/1234/problem/A A. Equalize Prices Again #include<bits/stdc++.h> ...

  4. Codeforces Round #590 (Div. 3) Editorial

    Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...

  5. Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

    链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...

  6. Codeforces Round #590 (Div. 3) C. Pipes

    链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists o ...

  7. Codeforces Round #590 (Div. 3) A. Equalize Prices Again

    链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...

  8. Codeforces Round #590 (Div. 3) E. Special Permutations

    链接: https://codeforces.com/contest/1234/problem/E 题意: Let's define pi(n) as the following permutatio ...

  9. Codeforces Round #590 (Div. 3) B2. Social Network (hard version)

    链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard ver ...

随机推荐

  1. 【CSS】340- 常用九宫格布局的几大方法汇总

    对,就是类似这样的布局~ 目录 1  margin负值实现 2  祖父和亲爹的里应外合 3  换个思路 - li生个儿子帮大忙 4 借助absolute方位值,实现自适应的网格布局 5 cloumn多 ...

  2. 【Eureka】服务端和客户端

    [Eureka]服务端和客户端 转载:https://www.cnblogs.com/yangchongxing/p/10778357.html Eureka服务端 1.添加依赖 <?xml v ...

  3. Java Swing JFrame实现全屏--无标题,无边框

    实现方式一: import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class T ...

  4. LCT能干啥???

    LCT能干啥       模板:   维护可加的树链信息:询问都是一条链上的信息:维护方式和线段树差不多: 增加一条边: 删除一条边: 修改一个点权: 修改一条路径上的所有点的点权:     整体来说 ...

  5. 小白的springboot之路(九)、集成MongoDB

    0.前言 MongoDB是一个高性能.开源的文档型数据库,是当前nosql数据库中最热门的一种,在企业中广泛应用:虽然前段时间更改了开源协议导致被很多企业舍弃,但主要是对云服务商影响较大,对我们来说其 ...

  6. Blazor(WebAssembly) + .NETCore 实现斗地主

    之前群里大神发了一个 html5+ .NETCore的斗地主,刚好在看Blazor WebAssembly 就尝试重写试试. 还有就是有些标题党了,因为文章里几乎没有斗地主的相关实现:),这里主要介绍 ...

  7. XAF Architecture XAF架构

    Applications built with the eXpressApp Framework are comprised of several functional blocks. The dia ...

  8. springcloud 微服务分布式 框架源码 activiti工作流 前后分离

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  9. Linux CPU占用率监控工具

    关键词:top.perf.sar.ksar.mpstat.uptime.vmstat.pidstat.time.cpustat.munin.htop.glances.atop.nmon.pcp-gui ...

  10. gorm操练记录

    这个数据库的定义蛮全的,先作个记录. package main import ( "time" "github.com/jinzhu/gorm" _ " ...