题目:http://codeforces.com/problemset/problem/610/E

如果存在c1,c2在原串相邻且在询问串中c1在c2前面的话,把它们在原串出现次数加起来记作sum,那么n-sum就是答案。

维护一棵线段树,线段树的每个节点存一个k^2的矩阵。

然后修改的话就在线段树上区间修改。。(标记下传的时候要注意不要越界TAT。。。

然后询问的话就k^2搞一下需要被减掉的字符对就好了。。

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define low(x) (x&(-x))
#define maxn 12
#define ll long long
using namespace std;
int n,m,K,ans;
char s[],s2[maxn];
int a[maxn][maxn];
struct data{int mat[maxn][maxn],l,r,tag;
}t[*];
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
while (isdigit(ch)){x=x*+ch-''; ch=getchar();}
return x*f;
}
void combine(data &a,data b,data c){
a.l=b.l; a.r=c.r;
rep(i,,K-) rep(j,,K-) a.mat[i][j]=b.mat[i][j]+c.mat[i][j];
a.mat[b.r][c.l]++;
a.tag=-;
}
void build(int i,int l,int r){
int mid=(l+r)/;
if (l==r){
t[i].tag=-; t[i].l=t[i].r=s[l]-'a';
clr(t[i].mat,);
return ;
}
build(i*,l,mid); build(i*+,mid+,r);
combine(t[i],t[i*],t[i*+]);
}
void ps(int i,int l,int r){
if (t[i].tag!=-){
t[i].l=t[i].r=t[i].tag;
clr(t[i].mat,);
t[i].mat[t[i].tag][t[i].tag]=r-l;
if (l!=r) t[i*].tag=t[i*+].tag=t[i].tag;
t[i].tag=-;
}
}
void change(int i,int l,int r,int tl,int tr,int val){
ps(i,l,r);
int mid=(l+r)/;
if (l==tl&&r==tr){
t[i].tag=val;
ps(i,l,r);
return ;
}
if (tr<=mid) {
change(i*,l,mid,tl,tr,val);
ps(i*+,mid+,r);
}
else if (tl>mid){
change(i*+,mid+,r,tl,tr,val);
ps(i*,l,mid);
}
else {
change(i*,l,mid,tl,mid,val);
change(i*+,mid+,r,mid+,tr,val);
}
combine(t[i],t[i*],t[i*+]);
}
int main(){
n=read(); m=read(); K=read();
scanf("%s",s);
build(,,n-);
rep(i,,m){
int op=read();
if (op==){
int l=read()-,r=read()-; scanf("%s",s2);
int now=s2[]-'a';
change(,,n-,l,r,now);
}
else {
ans=n;
scanf("%s",s2);
rep(j,,K-) rep(k,,K-) a[j][k]=;
rep(j,,K-) rep(k,j+,K-) a[s2[j]-'a'][s2[k]-'a']=;
rep(j,,K-) rep(k,,K-) ans-=t[].mat[j][k]*a[j][k];
printf("%d\n",ans);
}
}
return ;
}

CF 610E. Alphabet Permutations的更多相关文章

  1. Codeforces Round #337 Alphabet Permutations

    E. Alphabet Permutations time limit per test:  1 second memory limit per test:  512 megabytes input: ...

  2. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  3. CF 715 E. Complete the Permutations

    CF 715 E. Complete the Permutations 题目大意:给定两个排列\(p,q\)的一部分.定义两个排列\(p,q\)的距离为使用最少的交换次数使得\(p_i=q_i\).对 ...

  4. CF 1093 E. Intersection of Permutations

    E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...

  5. cf 251 B Playing with Permutations 暴力 分类讨论

    题链;http://codeforces.com/problemset/problem/251/B B. Playing with Permutations time limit per test 2 ...

  6. CF 463D Gargari and Permutations [dp]

    给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...

  7. CF 1093E Intersection of Permutations——CDQ分治

    题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...

  8. 【题解】CF#285 E-Positions in Permutations

    挺有收获的一道题ヾ(◍°∇°◍)ノ゙ 恰好为 m ,这个限制仿佛不是很好处理.一般而言,我所了解的恰好为 k 的条件,不是用组合数 / dp状态转移 / 斜率二分就只剩下容斥了.我们可以先处理出 nu ...

  9. CF D. Number Of Permutations 排列

    挺水的一道题~ 拿全排列随便乘一下就好了. #include <cstdio> #include <algorithm> #define N 300004 #define ll ...

随机推荐

  1. linux下PHP后台配置极光推送问题

    一.composer.json配置注意空格 按照极光推送官网所述,在composer.json下写入: "require": { "jpush/jpush": ...

  2. java.util.ConcurrentHashMap (JDK 1.8)

    1.1 java.util.ConcurrentHashMap继承结构 ConcurrentHashMap和HashMap的实现有很大的相似性,建议先看HashMap源码,再来理解Concurrent ...

  3. Windows as a Service(4)——使用Intune管理Windows10更新

    这是这个系列的最后一篇文章,我已经花了三篇的篇幅和大家分享有关于Windows as a Serivce的相关内容,链接如下: Windows as a Service(1)-- Windows 10 ...

  4. linux下后台运行MATLAB

    原帖:http://sypeterli1.blog.163.com/blog/static/2283740492013101745824207/ 后台运行matlab脚本文件的方法:nohup     ...

  5. mysql change master导致gtid丢失

    change master导致gtid丢失从innobackupex恢复导致binlog的拉取位置会导致主备gtid不一致.此类错误通过构造空事务方式无法修复.此时就需要change master 方 ...

  6. getComputedStyle与currentStyle获取样式(style/class)

    今天看jQuery源码CSS部分,里面用到了currentStyle和getComputedStyle来获取外部样式. 因为elem.style.width只能获取elem的style属性里的样式,无 ...

  7. requireJS对文件合并与压缩(二)

    requireJS对文件合并与压缩 RequireJS提供了一个打包与压缩工具r.js,r.js的压缩工具使用UglifyJS进行压缩的或Closure Compiler.r.js下载 require ...

  8. CSS开发规范

    虽然很久之前整理过一份简单的CSS规范,但是当时写的也不是很全面,有些细节也没有照顾到.记录一份较详细的版本,以备不时之需. 命名规范 [强制] class一律使用小写字母+下划线格式命名 例: cl ...

  9. 线程安全Dictionary

    public abstract class ReadFreeCache<TKey, TValue> { protected ReadFreeCache() : this(null) { } ...

  10. ASP.NET Core学习之四 在CentOS上部署.net core

    一.安装CentOs 以前在大学学过linux,但是对命令行总是有一种深深的排斥感,几年之后,还是又回来了. 1.下载 现在没法FQ,就算是FQ网速也是蜗牛一样慢,我使用阿里云的镜像站进行下载速度还是 ...