Codeforces Round #590 D. Distinct Characters Queries
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的更多相关文章
- Codeforces Round #590
题目链接:Round #590 题目答案:官方Editorial.My Solution A. Equalize Prices Again 签到题还WA了一发,向上取整有点问题: //my wrong ...
- Codeforces Round #590 (Div. 3)
A. Equalize Prices Again 题目链接:https://codeforces.com/contest/1234/problem/A 题意:给你 n 个数 , 你需要改变这些数使得这 ...
- Codeforces Round #590 (Div. 3)(e、f待补
https://codeforces.com/contest/1234/problem/A A. Equalize Prices Again #include<bits/stdc++.h> ...
- Codeforces Round #590 (Div. 3) Editorial
Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...
- 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 ...
- Codeforces Round #590 (Div. 3) C. Pipes
链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists o ...
- 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 ...
- Codeforces Round #590 (Div. 3) E. Special Permutations
链接: https://codeforces.com/contest/1234/problem/E 题意: Let's define pi(n) as the following permutatio ...
- 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 ...
随机推荐
- Spring-security-oAuth2分享
Spring-security-oAuth2分享 oAuth2简介 OAuth 2.0是用于授权的行业标准协议.OAuth 2.0致力于简化客户端开发人员的工作,同时为Web应用程序,桌面应用程序,移 ...
- Vmware Workstation虚拟机
目录 一.虚拟机是什么? 二.虚拟机的作用: 三.虚拟机创建流程 四.新的虚拟机上安装系统 五.虚拟机里添加硬盘 六.磁盘分区 一.虚拟机是什么? 虚拟机指通过软件模拟的具有完整硬件系统功能的.运行在 ...
- python学习-logging
"""#设置输出的日志内容格式fmt = '%(asctime)s %(filename)s %(funcName)s [line:%(lineno)d] %(level ...
- 表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理
有两张表,结构如下: t_item: t_bid: id int id int n ...
- 学习Python第一天 ---Hello World
引言 人生苦短,请用 Python(3.+) 越来越多的情况下使用Python语言进行"代码粘合"和"数据分析"变得非常方便,而且Python 在"爬 ...
- uploadify没反应
由于业务问题,需要用到uploadify这个插件,结果官方的case怎么弄都没问题,弄到自己的页面上就有问题了. 后来发现,这个插件是要用到swf的,但是页面加载的过程中并没有加载swf文件,所以将问 ...
- python之encode和decode编码
u = '中文' str3 = u.encode('utf-8') # 以utf-8编码对u进行编码,获得bytes类型对象 print(str3) u2 = str3.decode('utf-8') ...
- Java入门(一)——类、抽象类和接口
Java是一门面向对象语言,可以看出"对象"在Java有着举足轻重的位置.那么,"对象"从何而来呢?那必须是丈母娘造出来的,下面我们就先来说说这个丈母娘--类. ...
- 初学Swoole:PHP7安装Swoole的步骤
本篇文章给大家带来的内容是关于初学Swoole:PHP7安装Swoole的步骤,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 环境 这里不在使用apache做为web server. ...
- wx-icon和progress
基本内容 index.wxml <!--index.wxml--> <view class="container"> <!--icon text pr ...