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 问题描述 ...
随机推荐
- P5441 【XR-2】伤痕
Luogu5441 有 \(n\) 个点 ( \(n\) 为奇数 , \(n \le 99\) ) 的完全图 , 其中可以有最多 \(n\) 条无向边 , 其他都是有向边 . 如果对于某四个点不经过这 ...
- MapReduce 框架原理
1. Hadoop 序列化 1.1 自定义Bean对象实现序列化接口 必须实现 Writable 接口: 反序列化时,需要反射调用空参构造函数,所以必须有空参构造: 重写序列化方法: 重写反序列化方法 ...
- GitHub克隆下载代码速度慢解决办法
这几天克隆下载GitHub代码奇慢无比,网上搜索了一下解决方案有些不太完整,自己试验出了比较完整的解决方式: 1.在hosts文件里追加以下内容(IP需要替换掉),以下几个域名一个都不要少,有些文章只 ...
- js轮播图和bootstrap中的轮播图
js中的轮播图案例: <!DOCTYPE html><html lang="en"> <head> <meta charset=" ...
- cook,session
//发送session request.getSession().setAttribute("user", "zs1"); //发送cookie //需要自动登 ...
- C++反汇编第四讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.
目录: 1.多重继承在内存中的表现形式 多重继承在汇编中的表现形式 2.菱形继承 普通的菱形继承 虚继承 汇编中的表现形式 一丶多重继承在内存中的表现形式 高级代码: class Father1 { ...
- 10-MySQlL DBA笔记-基础知识
第四部分 运维篇 首先来了解一下数据库的定义,数据库是高效的.可靠的.易用的.安全的多用户存储引擎,我们可以通过它访问大量的持久化数据.我们管理和维护数据库,本质上也是要确保如上的特性,尽可能地保证数 ...
- 偏移动画(TranslateTransform)
用户界面组件.图像元素和多媒体功能可以让我们的界面生动活泼,除此之外,Silverlight还具备动画功能,它可以让应用程序“动起来”.实际上,英文中Animation这个单词的意思是给某物带来生命. ...
- 基于MFC对话框的2048游戏
在之前一篇<简单数字拼板游戏学习>基础上修改,地址:http://www.cnblogs.com/fwst/p/3706483.html 开发环境:Windows 7/ Visual St ...
- 初学java3 条件判断
三目运算符 条件? 正确结果:错误结果 if判断 单一条件判断 if(条件){ }else{ } 多种条件判断 if(){ }else if(){ } ... else{ } switch判断 swi ...