HUST 1559 ACM(树状数组)
来源:互联网 发布:现在淘宝做什么产品好 编辑:程序博客网 时间:2024/06/05 14:11
ACM
Time Limit: 2 Sec Memory Limit: 128 MBSubmissions: 166 Solved: 16
Description
Jianhe25 is a ACMer, he loves "ACM" very much, so he wants to know how many times "ACM" appear in the string.
Jianhe25 thinks it's so naive, so he wants to make it more difficult. He defines two types of operation:
C i x
It means to change the ith character into x , and x is a character which will always be capital letters 'A', 'C' or 'M'.
Q i j ( 1 <=i <= j<=n )
It means to count the times the substring "ACM" appear within the range of [i,j] of the string and print it on the screen.
Jianhe25 thinks it's so naive, so he wants to make it more difficult. He defines two types of operation:
C i x
It means to change the ith character into x , and x is a character which will always be capital letters 'A', 'C' or 'M'.
Q i j ( 1 <=i <= j<=n )
It means to count the times the substring "ACM" appear within the range of [i,j] of the string and print it on the screen.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has two integers n m , 1 <= n,m <= 10000 , that represents the length of the string and the number of operations, and the second line contains n character which always be the Capital letters 'A','C'or'M'. Followed by m lines, each line represents the operation.
Output
The output should contain the times "ACM" appear in the string, one per line.
Sample Input
16 8ACACMMQ 1 6Q 1 2C 3 MC 4 AC 5 CC 6 MQ 1 6Q 2 4
Sample Output
1020
HINT
Huge Input/Output , scanf/printf is recommended.
Source
The 6th ACM Programming Contest of HUST
题目比较简单,直接比对,结果用树状数组维护
#include <string.h>#include <algorithm>#include <iostream>#include <stdio.h>using namespace std;const int maxn = 15000;int n,m;char str[maxn];int tree[maxn];bool rec[maxn];int lowbit(int x){ return x&(-x);}int update(int x,int num){ while(x<=n){ tree[x]+=num; x+=lowbit(x); } return 0;}int sum(int pos){ int sum=0; while(pos>0){ sum+=tree[pos]; pos-=lowbit(pos); } return sum;}int main(){ int i,j,k,t,a,b; char s[10]; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); memset(tree,0,sizeof(tree)); memset(rec,false,sizeof(rec)); scanf("%s",str+1); for(i=3;i<=n;i++){ if(str[i]=='M' && str[i-1]=='C' && str[i-2]=='A'){ update(i,1); rec[i]=1; } } for(i=0;i<m;i++){ scanf("%s",s); if(s[0]=='Q'){ scanf("%d%d",&a,&b); if(a+2>b){ printf("0\n"); }else{ printf("%d\n",sum(b)-sum(a+2)+rec[a+2]); } }else{ scanf("%d%s",&a,s); if(s[0]==str[a]) continue; str[a]=s[0]; if(a>=3){ if(rec[a]){ update(a,-1); rec[a]=false; }else{ if(str[a]=='M' && str[a-1]=='C' && str[a-2]=='A'){ update(a,1); rec[a]=true; } } if(rec[a+1]){ update(a+1,-1); rec[a+1]=false; }else{ if(str[a+1]=='M' && str[a-1+1]=='C' && str[a-2+1]=='A'){ update(a+1,1); rec[a+1]=true; } } if(rec[a+2]){ update(a+2,-1); rec[a+2]=false; }else{ if(str[a+2]=='M' && str[a-1+2]=='C' && str[a-2+2]=='A'){ update(a+2,1); rec[a+2]=true; } } } if(a==2){ if(rec[a+1]){ update(a+1,-1); rec[a+1]=false; }else{ if(str[a+1]=='M' && str[a-1+1]=='C' && str[a-2+1]=='A'){ update(a+1,1); rec[a+1]=true; } } if(rec[a+2]){ update(a+2,-1); rec[a+2]=false; }else{ if(str[a+2]=='M' && str[a-1+2]=='C' && str[a-2+2]=='A'){ update(a+2,1); rec[a+2]=true; } } } if(a==1){ if(rec[a+2]){ update(a+2,-1); rec[a+2]=false; }else{ if(str[a+2]=='M' && str[a-1+2]=='C' && str[a-2+2]=='A'){ update(a+2,1); rec[a+2]=true; } } } } } } return 0;}
0 0
- HUST 1559 ACM(树状数组)
- Hust oj 1400 汽车比赛(树状数组)
- ACM斩草除根系列(一) 树状数组
- [ACM] hdu 2352 Stars (树状数组)
- [ACM] hrbustoj 1161 Leyni (树状数组)
- HDU 5096 ACM Rank(树状数组)
- 树状数组(acm hdu 5792)
- ACM PKU 2155 Matrix(树状数组)
- ACM算法:树状数组(详细)
- acm算法之树状数组
- ACM-数据结构-树状数组I
- HUST——1106xor的难题之二(异或树状数组单点修改和区间查询)
- Hdu acm 3486 step5.3.6(树状数组)
- [ACM] hdu 1166 敌兵布阵(树状数组)
- [ACM] hrbustoj 1400 汽车比赛 (树状数组)
- 杭电 HDU ACM 1166 敌兵布阵(树状数组)
- nyoj ACM:士兵杀敌(二)(树状数组)
- nyoj ACM:士兵杀敌(四)(树状数组)
- 转换路径的几种方式
- 利用百度地图API 画多边形时 有时坐标点会跑到地图窗口左上角
- linux grep命令详解
- 为甚么说不得
- Android 优化布局层次结构
- HUST 1559 ACM(树状数组)
- 移动App带来的好处
- mysql 实现oracle start with connect by递归
- Python中的staticmethod和classmethod
- 7 buffer pool的组织结构
- think in java interview-高级开发人员面试宝典(一)
- solr TermsComponent数据统计
- 浅谈数据库事务管理
- Android view 的cache