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的更多相关文章

  1. CodeForces 547E Mike and Friends AC自动机 主席树

    题意: 给出\(n\)个字符串\(s_i\)和\(q\)个询问: \(l,r,k\):\(\sum\limits_{i=l}^{r}count(i, k)\),其中\(count(i,j)\)表示\( ...

  2. Codeforces 547E - Mike and Friends(AC 自动机+树状数组)

    题面传送门 好久每做过 AC 自动机的题了--做几个题回忆一下罢 AC 自动机能够解决多串匹配问题,注意是匹配,碰到前后缀的问题那多半不在 AC 自动机能解决的范围内. 在初学 AC 自动机的时候相信 ...

  3. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  4. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  5. CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)

    What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas ...

  6. Codeforces 548B Mike and Fun

    传送门 B. Mike and Fun time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  8. 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 ...

  9. Codeforces 798A - Mike and palindrome

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. [转]Pass a ViewBag instance to a HiddenFor field in Razor

    本文转自:https://stackoverflow.com/questions/27456983/pass-a-viewbag-instance-to-a-hiddenfor-field-in-ra ...

  2. 写一个有字符界面的ssh链接工具

    大概的样子 这是大致的样子- 写之前想说的 因为个人工作的的电脑是deepin系统的,系统本身的命令行非常好用,用第三方的ssh工具用不习惯,就想自己写一个. shell脚本是第一次写,写的不是很好, ...

  3. myBatis组件之缓存实现及使用

    一 .概述 先讲缓存实现,主要是mybatis一级缓存,二级缓存及缓存使用后续补充 Mybatis缓存的实现是基于Map的,从缓存里面读写数据是缓存模块的核心基础功能:除核心功能之外,有很多额外的附加 ...

  4. 采用DTO和DAO对JDBC程序进行进一步优化

    采用DTO和DAO对JDBC程序进行进一步优化 DTO:数据传输对象,主要用于远程调用等需要远程调用对象的地方DAO:数据访问对象,主要实现封装数据库的访问,通过它可以把数据库中的表转换成DTO类 引 ...

  5. importnew:Map大家族的那点事儿

    Map大家族的那点事儿(1) :Map Map大家族的那点事儿(2) :AbstractMap Map大家族的那点事儿(3) :TreeMap Map大家族的那点事儿(4) :HashMap Map ...

  6. PHP中文件操作(1)--打开/读取文件

    1.打开文件(fopen) 语法:resource  $fp=fopen(文件地址,模式),返回的是文件指针(file pointer) 模式 含义 r 只读 w 写(清空重写) a 追加 $fp = ...

  7. 转:PHP中的使用curl发送请求(GET请求和POST请求)

    原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始 ...

  8. p2p登录拦截

    1.编写拦截标签 package com.xmg.p2p.base.util; import java.lang.annotation.ElementType; import java.lang.an ...

  9. ERP 实施执行力提高的障碍,看看你中了几招?

    据统计,在国内 ERP 实施的成功率非常低,成功实施实现系统集成的只占10%-20%:没有实现系统集成或实现部分集成的只有30%-40%:而失败的却占50%,并且在实施成功的10%-20%中大多为外资 ...

  10. 网络基础 图解Windows系统下单网卡设置双IP访问不同网段的方法

    图解Windows系统下单网卡设置双IP访问不同网段的方法 by:授客 QQ:1033553122 在Windows系统下即使只有一块网卡,同样可以实现双IP访问不同网段. 例: 外网信息: IP:1 ...