链接:

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(线段树, 位运算)的更多相关文章

  1. 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) & ...

  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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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. ...

  7. 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 ...

  8. 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 ...

  9. 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 问题描述 ...

随机推荐

  1. 模型预测控制 MPC

    使用MPC的原因:

  2. centos7yum的配置

    首先呢,本人的centos的版本是7.5 这里我主要介绍的是通过挂在系统镜像文件和指定网络epel镜像源. 首先指定本地的光盘当做yum的仓库 1.删除/etc/yum.repos.d的所有文件 2. ...

  3. Thinkphp命令行快速生成模型类方法

    进去cmd,切换到项目根目录,也就是think文件所在目录,执行下面的指令可以生成index模块的blog模型类文件: >php think make:model index/Blog 生成的模 ...

  4. luogu P4762 [CERC2014]Virus synthesis (回文自动机)

    大意: 初始有一个空串, 操作(1)在开头或末尾添加一个字符. 操作(2)在开头或末尾添加该串的逆串. 求得到串$S$所需最少操作数. 显然最后一定是由某个偶回文通过添加字符得到的, 那么只需要求出所 ...

  5. javascript中用&&跟||来简化if{}else{}的写法

    原文:javascript中用&&跟||来简化if{}else{}的写法 目录 javascript中用&&跟||来简化if{}else{}的写法 1. if else ...

  6. Angular 变更检测

    angular 的钩子函数有 content 和 view , Docheck 子控件中有属性变化的时候,父组件的 Docheck  content   view  这3个会依次执行,即使这个属性不在 ...

  7. 前端vue项目执行npm install 报错cd() never called()

    前端我刚开始接触Vue,从GitHub上下载了代码程序,但缺少一些插件,用vscode打开并下载插件执行报错cd() never called! 解决的方式 1.执行cmd命令行不要再vscode里执 ...

  8. JS数组去掉重复元素

    JS数组去掉重复元素,这里提供3中写法. var arr =[1,2,3,4,5,6,3,4,7,2,4,1,8]; 输出:[1,2,3,4,5,6,7,8]; 1.使用indexOf() arr.i ...

  9. vue+element-ui 项目中实现复制文字链接功能

    需求: 点击复制按钮,复制一个链接 在GitHub上找到一个clipboard组件,功能比较齐全 使用方法:   安装 npm i clipboard --save HTML <template ...

  10. mac上配置apidoc环境

    1. 安装node.js 和npm 前往 https://nodejs.org/en/ 下载node.js的最新版本,双击.pkg进行安装 在终端输入 node -v ,如正确输出版本号即安装成功 ( ...