AtCoder ABC 157E Simple String Queries

题目链接:https://atcoder.jp/contests/abc157/tasks/abc157_e

题目大意

  给定一串全由小写英文字母组成的字符串,然后顺序给出$Q$个操作,一种为替换字符串中的某个字符;另一种为查询字符串某个区间里面有多少个不同的字符。要求顺序输出第二种操作的结果。

分析

  线段树单点更新,每个节点存一个32位整数,其中26位代表26个英文字母,更新的话只要节点求或就行了。

  其他方法1:用26个树状数组,这个原理是一样的,但总感觉没有只用一棵线段树简明。

  其他方法2:把每个字母出现的位置存到有序表里面,然后查询的时候利用二分看看字母在不在区间里面,在的话就加一。

  以上方法时间复杂度都为$O(QlogN)$,我选用的是一棵线段树的做法。

代码如下

  1 #pragma GCC optimize(2)
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4
  5 /*-------------------Define Start-------------------*/
  6 typedef bool BL;                        // 布尔类型
  7 typedef char SB;                        // 有符号1字节,8位
  8 typedef unsigned char UB;                // 无符号1字节,8位
  9 typedef short SW;                        // 有符号短整型,16位
 10 typedef unsigned short UW;                // 无符号短整型,16位
 11 typedef long SDW;                        // 有符号整型,32位
 12 typedef unsigned long UDW;               // 无符号整型,32位
 13 typedef long long SLL;                    // 有符号长整型,64位
 14 typedef unsigned long long ULL;            // 无符号长整型,64位
 15 typedef char CH;                        // 单个字符
 16 typedef float R32;                        // 单精度浮点数
 17 typedef double R64;                        // 双精度浮点数
 18
 19 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i)
 20 #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i)
 21 #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i)
 22 #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 23 #define ms0(a) memset(a,0,sizeof(a))
 24 #define msI(a) memset(a,0x7f,sizeof(a))
 25 #define LOWBIT(x) ((x)&(-x))
 26
 27 #define MP make_pair
 28 #define PB push_back
 29 #define ft first
 30 #define sd second
 31 #define ALL(x) x.begin(),x.end()
 32
 33 #define pr(x) cout << #x << " = " << x << "  "
 34 #define prln(x) cout << #x << " = " << x << endl
 35
 36 #define lson l , mid , rt << 1
 37 #define rson mid + 1 , r , rt << 1 | 1
 38
 39 const ULL mod = 1e9 + 7;                //常用模数(可根据题目需要修改)
 40 const ULL inf = 0x7fffffff;                //用来表示无限大
 41 const ULL infLL = 0x7fffffffffffffffLL;    //用来表示无限大
 42 /*-------------------Define End-------------------*/
 43
 44 const UDW maxN = 5e5 + 7;
 45 SDW N, Q;
 46 string S;
 47 SDW ans;
 48
 49 void output();
 50
 51 SDW st[maxN << 2];
 52
 53 inline void pushUp(SDW rt) {
 54     st[rt] = st[rt << 1] | st[rt << 1 | 1];
 55 }
 56
 57 inline void build(SDW l, SDW r, SDW rt) {
 58     if(l >= r) {
 59         st[rt] = (1 << (S[r] - ‘a‘));
 60         return;
 61     }
 62     SDW mid = (l + r) >> 1;
 63     build(lson);
 64     build(rson);
 65     pushUp(rt);
 66 }
 67
 68 // 单点更新
 69 inline void update(SDW x, SDW y, SDW l, SDW r, SDW rt) {
 70     if(l >= r) {
 71         st[rt] = y;
 72         return;
 73     }
 74     SDW mid = (l + r) >> 1;
 75     if(x <= mid) update(x, y, lson);
 76     else update(x, y, rson);
 77     pushUp(rt);
 78 }
 79
 80 // 区间查询
 81 inline SDW query(SDW L, SDW R, SDW l, SDW r, SDW rt) {
 82     if(L <= l && r <= R) return st[rt];
 83     SDW ret = 0;
 84     SDW mid = (l + r) >> 1;
 85
 86     if(L <= mid) ret |= query(L, R, lson);
 87     if(R > mid) ret |= query(L, R, rson);
 88     return ret;
 89 }
 90
 91 void input(){
 92     cin >> N >> S >> Q;
 93     S = "#" + S;
 94     build(1, N, 1);
 95 }
 96
 97 void solve(){
 98     SDW mode;
 99
100     Rep(i, Q) {
101         cin >> mode;
102         if(mode == 1) {
103             SDW x;
104             CH y;
105             cin >> x >> y;
106             update(x, (1 << (y - ‘a‘)), 1, N, 1);
107         }
108         else if(mode == 2) {
109             SDW x, y;
110             cin >> x >> y;
111             ans = query(x, y, 1, N, 1);
112             ans = __builtin_popcount(ans);
113             output();
114         }
115     }
116 }
117
118 void output(){
119     cout << ans << endl;
120 }
121
122 int main() {
123     input();
124     solve();
125     return 0;
126 }

原文地址:https://www.cnblogs.com/zaq19970105/p/12436066.html

时间: 2024-08-04 09:05:58

AtCoder ABC 157E Simple String Queries的相关文章

AtCoder ABC 129F Takahashi&#39;s Basics in Education and Learning

题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f 题目大意 给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起来,记作 N,求 N % M. 分析 设 bit(i) 为第 i 项所需要进行的十进制位移. 则 $N = S_0 * 10^{bit(0)} + S_1 * 10^{bit(1)} + \dots + S_{L - 1} * 10^{bit(L - 1)}$. 一项一项地算是肯定要超时的,不过注意

csu 1550: Simple String (字符串)

1550: Simple String Time Limit: 1 Sec  Memory Limit: 256 MB Submit: 249  Solved: 112 [Submit][Status][Web Board] Description Welcome,this is the 2015 3th Multiple Universities Programming Contest ,Changsha ,Hunan Province. In order to let you feel fu

Water --- CSU 1550: Simple String

Simple String Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1550 Mean: 略. analyse: 水题. Time complexity: O(n) Source code:  // Memory Time // 1347K 0MS // by : crazyacking // 2015-03-29-12.08 #include<map> #include<queue> #

(比赛)A - Simple String Problem

A - Simple String Problem Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Practice POJ 2236 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network wit

Atcoder ABC 141

Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; char ch[50]; int main() { scanf("%s",ch+1); if(ch[1] == 'S') puts("Cloudy

题解 [Atcoder ABC 161] A,B,C

题解 [Atcoder ABC 161] A,B,C A: 水题,按题意模拟即可. code: #include<bits/stdc++.h> #define ft(i,l,r) for(register int i=l;i<=r;i++) #define fd(i,r,l) for(register int i=r;i>=l;i--) using namespace std; int a,b,c; int main() { cin>>a>>b>>

AtCoder ABC 127F Absolute Minima

题目链接:https://atcoder.jp/contests/abc127/tasks/abc127_f 题目大意 初始状态下$f(x) = 0$,现在有 2 种模式的询问,第一种以“1 a b”的形式,需要进行操作$f(x) = f(x) + |x - a| + b$:第二种以“2”的形式,求使得 f(x) 取得最小值的 x 取值和 f(x) 值,如果有多个 x,输出任意一个即可. 分析 考虑第一种询问已经出现了 k 次,现在遇到第二种询问.此时$f(x) = \sum_{i = 1}^k

AtCoder ABC 130F Minimum Bounding Box

题目链接:https://atcoder.jp/contests/abc130/tasks/abc130_f 题目大意 给定地图上 N 个点的坐标和移动方向,它们会以每秒 1 个单位的速度移动,设 Ans(t) 为在 t 时刻,$(x_{max} - x_{min}) * (y_{max} - y_{min})$的值,求 Ans(t) 的最小值.(最小值可能不是一个整数) 分析 稍加思考可以发现,不是所有点的所有坐标都对答案有影响,很多点完全可以忽略不计,下面以 Y 坐标为例,讨论影响$(y_{

AtCoder ABC 154E Almost Everywhere Zero

题目链接:https://atcoder.jp/contests/abc154/tasks/abc154_e 题目大意 给定一个整数N($1 \leq N \leq 10^{100}$)和K($1 \leq K \leq 3$),求[1, N]区间内数位上只有K个非零数的整数个数. 分析 找一下规律即可,详情见代码注释. 代码如下 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 /*-------------------Define