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 ...
随机推荐
- 记laravel5.5项目php-fpm迁移到swoole4.2.9
事起说明 最近对上线半年多的laravel项目做了一次少大的改动,由php-fpm改为swoole,这里做个记录. 2019年过年前半个月,上阿里云后台查看前一天的访问请求日志,发现很多接口响应慢.翻 ...
- CUBA 7 新特性 (下篇)
上篇我们主要介绍了 CUBA 7 中前端的一些主要功能.这篇我们介绍一下中间件的一变化和新特性. 中间件功能 前面关于新的界面 API 的描述内容比我预期的要多许多,所以在这一节,我会尽量简单点说! ...
- Socket编程 - 网络基础知识
API编程部分:http://www.cnblogs.com/Jimmy1988/p/7895213.html 1. 协议简介 此处,我们主要介绍Linux编程常用的三种协议(TCP/UDP/IP), ...
- Git报错:error: cannot open .git/FETCH_HEAD: Read-only file system
Git:git pull时报错 error: cannot open .git/FETCH_HEAD: Read-only file system 查看该文件: 未在网上找到解决办法,重启服务器就好了 ...
- [疑难杂症]解决实际开发中各种问题bug
我有一个习惯就是遇到问题找到解决方案后收藏网页.后来遇到问题越来越多,收藏就多得有点离谱了.我反思了一下,其实有用的信息就那么点,那我干脆还是做成网页剪报好了. 关于VS的 Problem:未能正确加 ...
- Java:使用DOM4j来实现读写XML文件中的属性和元素
DOM4可以读取和添加XML文件的属性或者元素 读取属性: public static void ReadAttributes() throws DocumentException { File fi ...
- 关于DNS缓存
- Code Signal_10分钟挑战题_constructArray
完成时间3分06秒 Given an integer size, return an array containing each integer from 1 to size in the follo ...
- 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...
- oracel存储过程编写 以及plsql存储过程的debug
1.语法: create or replace procedure messagebackup_createTable //此处存储过程名称不能超过30个字符 as tableName ...