: 寻宝
时间限制: Sec 内存限制: MB
提交: 解决:
[提交] [状态] [讨论版] [命题人:admin]
题目描述
采蘑菇的小西佬找到了一张上古年间的藏宝图,上面画着m座连绵不断的山,他决定去地图上记载的地点探险,可当他到达时,他发现当地其实有n座山,并且由于年代久远有些山也改变了形状,他不能准确的找到地图上藏宝的地点,于是他决定从一些连续的山中随意选择一座山作为地图上的第一个点,当他选定了起点以后,若当前的山与地图上记载山的高度相同他就会继续前往下一座山,否则就停下来,直到他到达地图上的最后一座山位置。我们假设如果他经过了大于k座山他就能找到宝藏(如果第一座山高度就不相同,视为经过0座山),现在的问题就是他有多大的可能性找到宝藏。
题目共有q次询问,每次询问有两个整数l,r代表采蘑菇的小西佬选择的区间,并且会给出第一次询问的k,之后的每个询问的k按照下面的式子算出
k=(ans*10086+666)%x; ans代表上一次询问的答案,x=min(m,r-l+1); 输入
第一行输入三个数n,m,k(<=n,m,k<=1e5); 第二行输入n个数代表n座山的高度(<=a[i]<=1e9) 第二行输入m个数代表m座山的高度(<=b[i]<=1e9) 第四行输入一个数q;(<=q<=1e5) 接下来每行输入两个整数l,r;(<=l<=r<=n) 输出
对于每个询问请输出对应的概率,以逆元的形式输出(mod=1e9+7);

题面

用exkmp预处理出每个点可以向后面跑的长度,由于k是变化的,所以需要主席树来回答。

#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
/**********showtime************/ const int N = 1e5 + ;
int nxt[N], ex[N];
int S[N], P[N];
int a[N];
int n,m,k;
void GETNEXT() {
int i = , j, po, len=m;
nxt[] = len;
while(P[i] == P[i+] && i+ < len) i++;
nxt[] = i;
po = ;
for(i = ; i < len; i++) {
if(nxt[i-po] + i < nxt[po] + po)
nxt[i] = nxt[i-po];
else {
j=nxt[po] + po - i;
if(j < ) j = ;
while(i + j < len && P[j] == P[j+i])
j++;
nxt[i] = j;
po = i;
}
}
}
void EXKMP()
{
int i = , j, po, len = n, l2 = m;
while(S[i] == P[i] && i < l2 && i < len) i++;
ex[] = i;
po = ;
for(i = ; i < len; i++)
{
if(nxt[i-po] + i < ex[po] + po) ex[i]=nxt[i-po];
else {
j = ex[po] + po - i;
if(j < ) j = ;
while(i + j < len && j < l2 && S[j+i] == P[j]) j++;
ex[i] = j;
po = i;
}
}
} struct node{
int le, ri;
int cnt;
} tree[N * ];
int tot = ;
int root[N];
int build(int le, int ri) {
int p = ++tot;
if(le == ri) {
tree[p].cnt = ;
return p;
}
int mid = (le + ri) >> ;
tree[p].le = build(le, mid);
tree[p].ri = build(mid+, ri);
tree[p].cnt = tree[tree[p].le].cnt + tree[tree[p].ri].cnt;
return p;
}
int ins(int now, int le, int ri, int x, int val) {
int p = ++tot;
tree[p] = tree[now];
if(le == ri) {
tree[p].cnt += val;
return p;
}
int mid = (le + ri) >> ;
if(x <= mid) {
tree[p].le = ins(tree[now].le, le, mid, x, val);
}
else {
tree[p].ri = ins(tree[now].ri, mid+, ri, x, val);
}
tree[p].cnt = tree[tree[p].le].cnt + tree[tree[p].ri].cnt;
return p;
}
int query(int L, int R, int le, int ri, int rt1, int rt2) {
if(L > R) return ;
if(le >= L && ri <= R) {
return tree[rt2].cnt - tree[rt1].cnt;
} int res = ;
int mid = (le + ri) >> ;
if(mid >= L) res += query(L, R, le, mid, tree[rt1].le, tree[rt2].le);
if(mid < R) res += query(L, R, mid+, ri, tree[rt1].ri, tree[rt2].ri);
return res;
}
ll ksm(ll a, ll n) {
ll res = ;
while(n > ) {
if(n & ) res = res * a % mod;
a = a * a % mod;
n = n>>;
}
return res;
}
int main(){
scanf("%d%d%d", &n, &m, &k);
for(int i=; i<n; i++) scanf("%d", &S[i]);
for(int i=; i<m; i++) scanf("%d", &P[i]); GETNEXT();
EXKMP();
root[] = build(, n); for(int i=; i < n; i++){
root[i+] = ins(root[i], , n, ex[i]+, );
} int q; scanf("%d", &q);
int ans = ;
for(int i=; i<=q; i++) { int l, r;
scanf("%d%d", &l, &r);
if(i > ) {
int x = min(m, r - l + );
k = (1ll*ans * + ) % x;
}
int c = query(k+, n, , n, root[l-], root[r]);
// cout<<c << " " << k << endl;
ans = 1ll*c * ksm(r - l + , mod-) % mod;
printf("%d\n", ans);
} return ;
}

五月月赛 寻宝 exkmp + 主席树的更多相关文章

  1. BZOJ5361[Lydsy1805月赛]对称数——主席树+随机化

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5361 好神的一道题啊! 容易看出来是要用维护权值的数据结构,因此树链剖分首先pass掉. ...

  2. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  3. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  4. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  5. BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3522  Solved: 1041[Submi ...

  6. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  7. BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 7143  Solved: 2968[Su ...

  8. [bzoj3932][CQOI2015][任务查询系统] (主席树)

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...

  9. [bzoj2588][count on a tree] (主席树+lca)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

随机推荐

  1. Prometheus 整合 AlertManager

    简介 Alertmanager 主要用于接收 Prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由,是一款前卫的告警通知系统.它支持丰富的告警通知渠道,可以将告警信息 ...

  2. Python版:Selenium2.0之WebDriver学习总结_实例1

    Python版:Selenium2.0之WebDriver学习总结_实例1  快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 实属转载:本人看的原文地址 :http:/ ...

  3. 【iOS】UIImageView 点击事件

    UIImageView 并不像 UIButton 那样点击鼠标就可以关联点击事件,也不像 Android 里有 onClickListener,这个时候就需要借助 UITapGestureRecogn ...

  4. 【iOS】tableView:viewForHeaderInSection: 方法未调用

    今天遇到这个问题,即重写的方法 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sec ...

  5. 【Android】error: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.NoActionBar'.

    问题: res 文件夹下的 values 下的 styles.xml <style name="Sherlock.Light.NoActionBar" parent=&quo ...

  6. 搭建谷歌浏览器无头模式抓取页面服务,laravel->php->python->docker !!!

    背景: 公司管理系统需要获取企业微信页面的配置参数如企业名.logo.人数等信息并操作,来隐藏相关敏感信息并自定义简化企业号配置流程 第一版已经实现了扫码登录获取cookie,使用该cookie就能获 ...

  7. Linux系统下减少LV(逻辑卷)容量

    查看文件系统现有 lv_test 容量,总计9.9G,已使用2% 命令 df -h 2 查看系统中的 PV 情况 命令:pvdisplay vg_test 下有两个 PV,分别为  /dev/sdb1 ...

  8. Windows的 IIS 部署django项目

    Windows的 IIS 部署django项目 1.安装Windows的IIS 功能(win10为例): (1)进入控制面板  :选择大图标    进入程序和功能 (2)启用或者关闭Windows功能 ...

  9. Spring系列(二):Spring IoC应用

    一.Spring IoC的核心概念 IoC(Inversion of Control  控制反转),详细的概念见Spring系列(一):Spring核心概念 二.Spring IoC的应用 1.定义B ...

  10. android——实现跨程序访问数据

    使用之前的SQLite存储的应用程序.首先需要在这个应用程序中创建内容提供器,右击com.example.administrator.exp7包→New→Other→Content Provider, ...