链接:

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;
}

最新文章

  1. js正则表达式中test,exec,match方法的区别
  2. Impossible to load an image in xcassets on bundle
  3. Linux关于添加硬盘的那些事儿:笔记
  4. 使用CSS/JS代码修改博客模板plus
  5. TI CC254x BLE教程 3
  6. WEB/HTTP 调试利器 Fiddler 的一些技巧分享
  7. vc不用IDE编译方法
  8. hadoop中遇到的问题。
  9. 关于Linux内核学习的误区以及相关书籍介绍
  10. FileUpload上传文件无法获取文件名
  11. HttpClient使用cookie
  12. oracle-TNS是什么?
  13. Android实现程序前后台切换效果
  14. POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
  15. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
  16. StudyJams学习历程总结
  17. virtualenv与virtualenvwrapper的配置
  18. Laravel5笔记--路由
  19. OAuth2.0 协议的理解
  20. vuex中strict严格模式

热门文章

  1. Django使用DataTables插件总结
  2. System memory 259522560 must be at least 4.718592
  3. Eureka【支持Remote Region】
  4. 请写一段 PHP 代码 ,确保多个进程同时写入同一个文件成功
  5. php源码安装执行configure报错error: off_t undefined; check your library configuration
  6. Ubuntu使用Shadow socks-qt5
  7. Spring 自定义Bean 实例获取
  8. 微信小程序在组件中获取界面上的节点信息wx.createSelectorQuery
  9. linux - 卸载python
  10. Git忽略已追踪文件或文件夹