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. 从多核CPU Cache一致性的应用到分布式系统一致性的概念迁移

    概述 现代多核CPU的cache模型基本都跟下图1所示一样,L1 L2 cache是每个核独占的,只有L3是共享的,当多个cpu读.写同一个变量时,就需要在多个cpu的cache之间同步数据,跟分布式 ...

  2. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

  3. 中国剩余定理(CRT)及其拓展(ExCRT)

    中国剩余定理 CRT 推导 给定\(n\)个同余方程 \[ \left\{ \begin{aligned} x &\equiv a_1 \pmod{m_1} \\ x &\equiv ...

  4. PythonI/O进阶学习笔记_8.python的可迭代对象和迭代器、迭代设计模式

     content: 1.什么是迭代协议 2. 什么是迭代器(Iterator)和可迭代对象(Iterable) 3. 使用迭代器和可迭代对象 4. 创建迭代器和可迭代对象 5. 迭代器设计模式   一 ...

  5. JVM 学习笔记二 :JVM内存区域

    一.内存分配概述

  6. uni-app实现图片和视频上传功能

    使用uni-app实现点击上传,既可以上传视频,有可以上传图片,图片预览,删除图片和视频功能,最终效果如下.uni-app里面没有提供同时上传视频和图片这个插件,只能靠自己手写,  1.页面布局 通过 ...

  7. 12-Factor与云原生

    12-Factor与云原生 云原生应用 今天先到这儿,希望对技术领导力, 企业管理,系统架构设计与评估,团队管理, 项目管理, 产品管理,团队建设 有参考作用 , 您可能感兴趣的文章: 精益IT组织与 ...

  8. vue集成cesium,webgis平台第一步(附源码下载)

    vue-cesium-platform Vue结合Cesium的web端gis平台 初步效果 笔记本性能限制,运行Cesium温度飙到70度以上.所以平时开发时先开发界面,之后加载Cesium地球 当 ...

  9. HTTP认知(请求与响应)

    web的工作是:浏览器发送请求报文 + 服务端返回响应报文 通俗的说一下web工作的一个流程: 浏览器向服务端发送HTTP请求报文:这条请求报文组成由请求行.请求头.请求体三大部分组成: 1.请求行 ...

  10. git 常用操作汇总

    1. 如何查看当前分支是从哪个分支创建来的?  git reflog --date=local 当前分支名称 2. 查看当前分支 git branch 当前分支前面会显示 * 号  3.切换到某个分支 ...