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 ...
随机推荐
- jmeter微信公众号接口测试实例
线程组 HTTP Cookie 管理器 HTTP 请求默认值 用户定义的变量 察看结果树 HTTP请求 响应断言 正则表达式提取器 线程组 HTTP Cookie 管理器 HTTP 请求默认值 用户定 ...
- H5 app在真机调试的时候正常,打包成app后报错
在自己的一个用h5开发的项目中, 环境 IDE HBuilderX 打包工具 Hbuilder线上打包 开发语言 JS 现象 从一个列表进入详细页之后一直转圈圈.因为是调用系统原生的等待组件,界面无法 ...
- drf源码分析系列---权限
权限的使用 全局使用 from rest_framework.permissions import BasePermission from rest_framework import exceptio ...
- 简单学习【1】——打包JS
webpack entry <entry> output webpack --config webpack.conf.js Step1:新建一个文件,里面有一个app.js 一个sum.j ...
- hdu 6299 Balanced Sequence (贪心)
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- java基础-谈谈你对面向对象的理解
一 前言 本篇文章的核心知识如下,主要是帮助大家更好的理解面向对象编程: 二面向对象VS面向过程 2.1 面向过程编程 面向过程编程(Process Oriented Programming )其意指 ...
- 小程序 wx.request请求
1.wx.request相当于发送ajax请求 微信官方解释 参数 属性 类型 默认值 必填 说明 url string 是 开发者服务器接口地址 data string/object/ArrayBu ...
- 在CentOS 7 上使用Docker 运行.NetCore项目
安装Docker CentOS 7 安装 Docker 编写Dockerfile 右键项目->添加->Docker 支持 选择Linux 修改为如下: FROM mcr.microsoft ...
- 利用Azure虚拟机安装Dynamics 365 Customer Engagement之十二:新增SQL Server可用性副本
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 数据库学习笔记day01+day02
--表示系统时间select sysdate from dual --表是关系型数据库的基本结构--表是二维的,由行和列组成--行称为记录,列称为字段 --创建第一张表create table hw( ...