codeforces 547E Mike and Friends
codeforces 547E Mike and Friends
题意
题解
代码
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---
const int N = 200005;
int n, q, tlen;
int len[N], plen[N];
string s[N];
namespace Doubling {
static const int N = ::N << 1;
int t[N], wa[N], wb[N], sa[N], h[N];
inline void sort(int *x, int *y, int n, int m) {
rep(i, 0, m) t[i] = 0;
rep(i, 0, n) t[x[y[i]]]++;
rep(i, 1, m) t[i] += t[i-1];
per(i, 0, n) sa[--t[x[y[i]]]] = y[i];
}
inline bool cmp(int *x, int a, int b, int d) {
return x[a] == x[b] && x[a+d] == x[b+d];
}
inline void da(int *s, int n, int m) {
int *x = wa, *y = wb;
rep(i, 0, n) x[i] = s[i], y[i] = i;
sort(x, y, n, m);
for(int j = 1, p = 1; p<n; m = p, j<<=1) {
p = 0;rep(i, n-j, n) y[p++] = i;
rep(i, 0, n) if(sa[i] >= j) y[p++] = sa[i] - j;
sort(x, y, n, m);
swap(x, y);
p = 1;
x[sa[0]] = 0;
rep(i, 1, n) x[sa[i]] = cmp(y, sa[i], sa[i-1], j)?p-1:p++;
}
}
inline void cal_h(int *s, int n, int *rk) {
int j, k = 0;
for(int i = 1; i<=n; ++i) rk[sa[i]] = i;
for(int i = 0; i<n; h[rk[i++]] = k) {
for(k && --k, j = sa[rk[i]-1];s[i+k]==s[j+k];++k);
}
}
}
struct Seg {
static const int N = Doubling::N * 22 + 7;
int cntn;
int ls[N], rs[N], cnt[N], rt[N];
inline void upd(int pre, int &now, int p, int l, int r) {
now = ++cntn;
ls[now] = ls[pre];
rs[now] = rs[pre];
cnt[now] = cnt[pre] + 1;
if(l == r) return ;
int mid = l + r >> 1;
if(p <= mid) upd(ls[pre], ls[now], p, l, mid);
else upd(rs[pre], rs[now], p, mid+1, r);
}
inline int qry(int tl, int tr, int L, int R, int l, int r) {
if(L<=l&&r<=R) {
return cnt[tr] - cnt[tl];
}
int mid = l+r>>1, ans=0;
if(L<=mid) ans += qry(ls[tl], ls[tr], L, R, l, mid);
if(R>=mid+1) ans += qry(rs[tl], rs[tr], L, R, mid+1, r);
return ans;
}
}seg;
struct DA {
static const int N = ::N << 1;
int p[22][N], rk[N], in[N], Log[N], n;
inline void Build() {
Doubling::da(in, n+1, 300);
Doubling::cal_h(in, n, rk);
Log[0] = -1;for(int i = 1; i<=n; ++i) Log[i] = Log[i-1] + (i==(i&(-i)));
for(int i = 1; i<=n; ++i) p[0][i] = Doubling::h[i];
for(int j = 1; 1<<j<=n; ++j) {
int lim = n+1-(1<<j);
for(int i = 1; i<=lim; ++i) p[j][i] = min(p[j-1][i], p[j-1][i+(1<<j>>1)]);
}
rep(i, 1, n+1) seg.upd(seg.rt[i-1], seg.rt[i], Doubling::sa[i], 0, n);
}
inline int lcp(int a, int b) {
if(a==b) return n;
if(a>b) swap(a, b);++a;
int t=Log[b-a+1];
return min(p[t][a], p[t][b-(1<<t)+1]);
}
}da;
inline int calcl(int l, int r, int k, int len) {
int res = r;
while(l<=r) {
int mid = l+r>>1;
if(da.lcp(mid, k) >= len) {
res = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return res;
}
inline int calcr(int l, int r, int k, int len) {
int res = l;
while(l<=r) {
int mid = l+r>>1;
if(da.lcp(k, mid) >= len) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res;
}
inline void solve() {
int l, r, k;
cin >> l >> r >> k;
int rk = da.rk[plen[k-1]+1];
int tl = calcl(1, rk, rk, len[k]);
int tr = calcr(rk, da.n, rk, len[k]);
int L = plen[l-1]+1;
int R = plen[r]-1;
cout << seg.qry(seg.rt[tl-1], seg.rt[tr], L, R, 0, da.n) << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> q;
rep(i, 1, n+1) {
cin >> s[i];
len[i] = sz(s[i]);
plen[i] = plen[i-1] + len[i] + 1;
da.in[tlen++] = 27;
rep(j, 0, sz(s[i])) da.in[tlen++] = s[i][j] - 'a' + 1;
}
da.in[tlen] = 0;
da.n = tlen;
da.Build();
while(q--) {
solve();
}
return 0;
}
codeforces 547E Mike and Friends的更多相关文章
- CodeForces 547E Mike and Friends AC自动机 主席树
题意: 给出\(n\)个字符串\(s_i\)和\(q\)个询问: \(l,r,k\):\(\sum\limits_{i=l}^{r}count(i, k)\),其中\(count(i,j)\)表示\( ...
- Codeforces 547E - Mike and Friends(AC 自动机+树状数组)
题面传送门 好久每做过 AC 自动机的题了--做几个题回忆一下罢 AC 自动机能够解决多串匹配问题,注意是匹配,碰到前后缀的问题那多半不在 AC 自动机能解决的范围内. 在初学 AC 自动机的时候相信 ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas ...
- Codeforces 548B Mike and Fun
传送门 B. Mike and Fun time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1
C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...
- Codeforces 798A - Mike and palindrome
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- [BZOJ 2655]calc
Description 题库链接 给出 \(A,n,p\) ,让你在模 \(p\) 意义下求所有序列 \(a\) 满足"长度为 \(n\) 且 \(a_i\in[1,A]\) ,并且对于 \ ...
- bzoj 5341: [Ctsc2018]暴力写挂
Description Solution 边分治+边分树合并 这个题很多做法都是启发式合并的复杂度的,都有点卡 以前有个套路叫做线段树合并优化启发式合并,消掉一个 \(log\) 这个题思路类似,建出 ...
- EMC,EMI,EMS,ESD分别是什么?有什么区别和联系?
一.EMC EMI EMS定义: EMC(ElectromagneticCompatibility) 电磁兼容,是指设备或系统在电磁环境中性能不降级的状态.电磁兼容,一方面要求系统内没有严重的干扰源, ...
- WPF进度条
ProgressBar控件与传统WinForm使用方法完全一样,我们只需关注: Minimum——最小值,默认为0: Maximum——最大值,默认为100. Value——当前值. 关键是它的控 ...
- 上传多张图片imgupload
<tr> <td class="listtitle-up">尿素箱</td> <td> <div class="co ...
- DOM-添加元素、节点
createElement()方法能够根据参数指定的标签名称创建一个新元素,并返回新建元素的引用,用法如下 var element=document.createElement("tagNa ...
- [IDE]快捷键整理
VS Resharper eclipse 备注 运行 Ctrl+F5 - Ctrl+F11 调试 F5 - F11 逐语句执行 F11 - F5 IE.FF: F11 逐过程执行 F10 ...
- Java基础教程(20)--数字和字符串
一.数字 在用到数字时,大多数情况下我们都会使用基本数据类型.例如: int i = 500; float gpa = 3.65f; byte mask = 0xff; 然而,有时候我们既需要 ...
- 在SecureCRT中给linux上传和下载文件
下载:sz中的s意为send(发送),告诉客户端,我(服务器)要发送文件 send to cilent,就等同于客户端在下载. 上传:rz中的r意为received(接收),告诉客户端,我(服务器)要 ...
- box-sizing 属性
box-sizing属性可以为三个值: content-box,border和padding不计算入width之内 padding-box,padding计算入width内 border-box,bo ...