HDU 6096 树套树
思路:
网上的题解有AC自动机的,有trie树的,还有(乱搞?)的
首先把输入的那n个串按照字典序排序,
把n个串翻转以后再按照字典序排序
这样我们发现, 查的前缀在字典序排序后是一段区间,
查的后缀翻转一下在翻转后的字典序排序以后也是一段区间
这样如果不考虑重叠的问题,就是一个简单的二维数点问题,一维排序,一维线段树即可解决
如果有重叠的问题,我们需要搞出来每个字符串的长度,使给出的前缀长+后缀长>=原字符串长度
此时题目变成了三维偏序,排序后树套树即可。
//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=;
int cases,n,q,root[N<<],cnt,tree[N*],lson[N*],rson[N*];
struct Node{string str;int id,len;}X[N],Y[N],tmp;
bool operator<(Node a,Node b){return a.str<b.str;}
struct Pnt{int x,y,z;}p[N];
struct Ask{int xl,xr,yl,yr,z,id,ans;}ask[N];
bool cmp1(Pnt a,Pnt b){return a.z>b.z;}
bool cmp2(Ask a,Ask b){return a.z>b.z;}
bool cmp3(Ask a,Ask b){return a.id<b.id;}
void Insert(int l,int r,int &pos,int wei){
if(!pos)pos=++cnt;tree[pos]++;
if(l==r)return;
int mid=(l+r)>>;
if(mid>=wei)Insert(l,mid,lson[pos],wei);
else Insert(mid+,r,rson[pos],wei);
}
void insert(int l,int r,int pos,int num,int wei){
Insert(,n,root[pos],wei);
if(l==r)return;
int mid=(l+r)>>,lson=pos<<,rson=pos<<|;
if(mid<num)insert(mid+,r,rson,num,wei);
else insert(l,mid,lson,num,wei);
}
int Query(int l,int r,int pos,int L,int R){
if(l>=L&&r<=R)return tree[pos];
int mid=(l+r)>>;
if(mid<L)return Query(mid+,r,rson[pos],L,R);
else if(mid>=R)return Query(l,mid,lson[pos],L,R);
else return Query(l,mid,lson[pos],L,R)+Query(mid+,r,rson[pos],L,R);
}
int query(int l,int r,int pos,int xl,int xr,int yl,int yr){
if(l>=xl&&r<=xr)return Query(,n,root[pos],yl,yr);
int mid=(l+r)>>,lson=pos<<,rson=pos<<|;
if(mid<xl)return query(mid+,r,rson,xl,xr,yl,yr);
else if(mid>=xr)return query(l,mid,lson,xl,xr,yl,yr);
else return query(l,mid,lson,xl,xr,yl,yr)+query(mid+,r,rson,xl,xr,yl,yr);
}
int main(){
ios::sync_with_stdio(false);
cin>>cases;
while(cases--){
memset(tree,,sizeof(int)*(cnt+));
memset(lson,,sizeof(int)*(cnt+));
memset(rson,,sizeof(int)*(cnt+));
memset(root,,sizeof(root));cnt=;
cin>>n>>q;
for(int i=;i<=n;i++)cin>>X[i].str;
sort(X+,X++n);
for(int i=;i<=n;i++)X[i].id=i,X[i].len=X[i].str.length();
for(int i=;i<=n;i++)Y[i]=X[i];
for(int i=;i<=n;i++)reverse(Y[i].str.begin(),Y[i].str.end());
sort(Y+,Y++n);
for(int i=;i<=n;i++)p[i].x=Y[i].id,p[i].y=i,p[i].z=Y[i].len;
for(int i=;i<=q;i++){
string pre,suf;cin>>pre>>suf;
ask[i].id=i;tmp.str=pre;
ask[i].xl=lower_bound(X+,X++n,tmp)-X;
tmp.str=pre,tmp.str.push_back('z'+);
ask[i].xr=lower_bound(X+,X++n,tmp)-X-;
tmp.str=suf,reverse(tmp.str.begin(),tmp.str.end());
ask[i].yl=lower_bound(Y+,Y++n,tmp)-Y;
tmp.str.push_back('z'+);
ask[i].yr=lower_bound(Y+,Y++n,tmp)-Y-;
ask[i].z=pre.size()+suf.size();
}
sort(p+,p++n,cmp1);sort(ask+,ask++q,cmp2);
int tt=;
for(int i=;i<=q;i++){
while(p[tt].z>=ask[i].z&&tt<=n)insert(,n,,p[tt].x,p[tt].y),tt++;
if(ask[i].xl>ask[i].xr||ask[i].yl>ask[i].yr){ask[i].ans=;continue;}
ask[i].ans=query(,n,,ask[i].xl,ask[i].xr,ask[i].yl,ask[i].yr);
}sort(ask+,ask++q,cmp3);
for(int i=;i<=q;i++)cout<<ask[i].ans<<endl;
}
}
HDU 6096 树套树的更多相关文章
- hdu 4417 Super Mario/树套树
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...
- hdu 4819 Mosaic 树套树 模板
The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). He ...
- BZOJ 3110: [Zjoi2013]K大数查询 [树套树]
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6050 Solved: 2007[Submit][Sta ...
- BZOJ4170 极光(CDQ分治 或 树套树)
传送门 BZOJ上的题目没有题面-- [样例输入] 3 5 2 4 3 Query 2 2 Modify 1 3 Query 2 2 Modify 1 2 Query 1 1 [样例输出] 2 3 3 ...
- bzoj3262: 陌上花开(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- bzoj3295: [Cqoi2011]动态逆序对(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- BZOJ 3110 k大数查询 & 树套树
题意: 有n个位置,每个位置可以看做一个集合,现在要求你实现一个数据结构支持以下功能: 1:在a-b的集合中插入一个数 2:询问a-b集合中所有元素的第k大. SOL: 调得火大! 李建说数据结构题能 ...
- BZOJ 3110 树套树 && 永久化标记
感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...
随机推荐
- Excel小tips - 设置指定可选填充内容
数据——数据验证——设置——允许(A)——序列——来源 图1 数据验证界面 图2 选取序列数据字典 图3 效果展示 可以在同一个工作薄的同一个或者另一个工作表中设置指定内容(充当数据字典),然后点 ...
- OPENWRT安装Python到U盘
http://www.zcilxl.com/tech/23.html 研究了一下如何将软件安装在OPENWRT挂载的U盘上,以Python为例,把过程记录一下. 安装的前提是你的USB设备已经成功挂载 ...
- 【转】c++ 如何批量初始化数组 fill和fill_n函数的应用
http://blog.csdn.net/sunquana/article/details/9153213 一. fill和fill_n函数的应用: fill函数的作用是:将一个区间的元素都赋予val ...
- [转]图解eclipse 查看原始类出现The jar file rt.jar has no source attachment
原文:http://blog.csdn.net/u011514810/article/details/53196371 ---------------------------------------- ...
- IntelliJ 中类似于Eclipse ctrl+q的是Ctrl+Shift+Backspace
IntelliJ 中类似于Eclipse ctrl+q的是Ctrl+Shift+Backspace 回到刚刚编辑的地方: ctrl+alt+Left 是回到刚刚浏览的地方,不一定是编辑的地方,可能已经 ...
- VM虚拟机ping不通局域网其他主机的解决办法
1 我的笔记本的无线网卡是自动获取IP,并且是通过无线网卡上网. 2 我的有线网卡是通过自己设定IP跟局域网的其他机器连通.当前设定的IP为172.16.17.2 3我需要连接的局域网另一个主 ...
- 怎样用Google APIs和Google的应用系统进行集成(8)----怎样把Google Blogger(博客)的JSON Schema转换成XML的Schema(XSD)?
在Google RESTFul API中,Google Blogger API(Google博客API)应该和我们的生活离得近期:由于差点儿非常多人每天都在看博客,都在写博客,都听说过博客.在前面的G ...
- VTMagic的使用
// VTMagic的使用 // CFOrderViewController.m // qifuyuniOS //// /** * @author 李洪强, 16-08-30 10:08:50 ...
- sublime text 3 乱码
sublime text 是一款很好用的文字编辑软件,可谓是程序员必备,但是最近发现在mac端使用的时候,中文乱码, 网上一些解决方案,抄袭严重,没有解决实际问题,所以记录下自己解决问题的过程. 1. ...
- jquery源码学习笔记一:总体结构
练武不练功,到老一场空.计算机也一样. 计算机的功,就是原理.如果程序员只会使用各种函数,各种框架,而不知其原理,顶多熟练工人而已.知其然,更要知其所以然. jquery我们用得很爽,但它究竟咋实现的 ...