Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接:
https://codeforces.com/contest/1234/problem/D
题意:
You are given a string s consisting of lowercase Latin letters and q queries for this string.
Recall that the substring s[l;r] of the string s is the string slsl+1…sr. For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top".
There are two types of queries:
1 pos c (1≤pos≤|s|, c is lowercase Latin letter): replace spos with c (set spos:=c);
2 l r (1≤l≤r≤|s|): calculate the number of distinct characters in the substring s[l;r].
思路:
线段树维护二进制,二进制的每一位维护对应的字母在区间是否使用过, 合并区间就是与一下.
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
char s[MAXN];
int tree[MAXN*4];
int n, q;
void PushUp(int root)
{
tree[root] = tree[root<<1] | tree[root<<1|1];
}
void Build(int root, int l, int r)
{
if (l == r)
{
tree[root] = 1<<(s[l]-'a');
return;
}
int mid = (l+r)/2;
Build(root<<1, l, mid);
Build(root<<1|1, mid+1, r);
PushUp(root);
}
void Update(int root, int l, int r, int p, int c)
{
if (l == r)
{
tree[root] = 1<<c;
return;
}
int mid = (l+r)/2;
if (p <= mid)
Update(root<<1, l, mid, p, c);
else
Update(root<<1|1, mid+1, r, p, c);
PushUp(root);
}
int Query(int root, int l, int r, int ql, int qr)
{
if (qr < l || ql > r)
return 0;
if (ql <= l && r <= qr)
return tree[root];
int mid = (l+r)/2;
int res = 0;
res |= Query(root<<1, l, mid, ql, qr);
res |= Query(root<<1|1, mid+1, r, ql, qr);
return res;
}
int main()
{
scanf("%s", s+1);
n = strlen(s+1);
Build(1, 1, n);
scanf("%d", &q);
int op, l, r;
char val;
while (q--)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%d %c", &l, &val);
Update(1, 1, n, l, val-'a');
}
else
{
scanf("%d %d", &l, &r);
int res = Query(1, 1, n, l, r);
int cnt = 0;
while (res)
{
if (res&1)
cnt++;
res >>= 1;
}
printf("%d\n", cnt);
}
}
return 0;
}
Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)的更多相关文章
- Codeforces Round #721 (Div. 2)A. And Then There Were K(位运算,二进制) B1. Palindrome Game (easy version)(博弈论)
半个月没看cf 手生了很多(手动大哭) Problem - A - Codeforces 题意 给定数字n, 求出最大数字k, 使得 n & (n−1) & (n−2) & ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
题目链接:http://codeforces.com/contest/381/problem/E E. Sereja and Brackets time limit per test 1 secon ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
题目链接: 题目 E. George and Cards time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 ...
随机推荐
- 《C和指针》读书笔记
1. 三字母词 三字母词即用三个字符合起来表示另一个字符,它可以使C环境在某些缺少一些必需字符的字符集上实现. ??( [ ??< { ??= # ??) ] ??> } ??/ \ ?? ...
- js中的三目运算符详解
判断 javascript中的三目运算符用作判断时,基本语法为: expression ? sentence1 : sentence2 当expression的值为真时执行sentence1,否则执行 ...
- 更新neo4j节点信息
将多个属性的内容更新到节点上 def update_by_id(id,graph,**kwargs): """ 更新节点的属性 根据节点的ID来更新节点的属性,如果存在该 ...
- multipart/form-data(二进制流) 两种传输方式
一.传统表单提交传输方式 <form id= "uploadForm" action= "url" method= "post" en ...
- analysis_tools
- 如何给Swagger加注释
在Startup.cs文件中的ConfigureServices()方法中添加如下代码即可 services.AddSwaggerGen(options => { options.Swagger ...
- PS笔记
PS 笔记 PS调色人像步骤-by李涛: 色阶- 追饱和度- 色彩平衡- 中间调不动,阴影偏青,偏绿,偏蓝 , 高光偏红,偏一点蓝 锐化 1. 冷暖对比-by狼族摄影: 高光和阴影设置为偏青色 ...
- Javascript简单教程汇总
什么是函数 一段定义好的代码,并可以反复使用的代码块 函数的作用 提升代码的可复用性,将一段代码进行预定义,需要使用的时候才触发 代码块 形成了一个相对独立的作用域 语法: function 函数名 ...
- asp.net ListView控件的简单实用和配置
1 web窗体界面代码 ItemType:控件要绑定的实体模型 SelectMethod:控件获取实体集合的后台方法 DataKeyNames:实体的主键 UpdateProduct:设置跟新的方法 ...
- 在vue中引用echarts导致Cannot read property getAttribute of null ?
报错信息如下: 之前一直用echarts没有出现过这个问题,所以当这个问题出现时我就开始了各种查,试了几种方法依旧报错,比如: 1.在mounted() {},写成如下形式:(依旧报错) this.$ ...