重新写一年前抄题解的那题,当时我啥都不会只是Ctrl+C,Ctrl+V写过的题,今天重新写一遍。

题解:

不会线段树分治,还是学一下这东西吧,这是我的第一道线段树分治。

首先对于特殊商品,可以直接可持久化Trie记录答案。首先考虑对每个线段树开一个vector,把询问的时间区间看成一段一段的塞到线段树里,修改实际上是相当于一个后缀。然后把修改按位置排序,然后仿照线段树的形式按时间分治,mid前的修改扔到左边数组里递归,反之扔到右边,这样能够排除这一维的限制。然后空间上用可持久化Trie,前缀和相减即可。最开始的修改,要按照商店顺序排序,这样可以把中间的商店忽略,一个修改挨着一个修改建主席树,复杂度得以保证。

#include<bits/stdc++.h>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int N=1e5+;
struct guest{int l,r,L,R,x;}p[N];
struct buy{int s,v,t;}q[N],t1[N],t2[N];
int n,m,n1,n2,tot,top,rt[N],ans[N],st[N],ch[N*][],sz[N*];
vector<int>a[N];
bool cmp(buy x,buy y){return x.s<y.s;}
void build(int&x,int u,int S)
{
x=++tot;
int now=x;
for(int i=;~i;i--)
{
bool d=S>>i&;
ch[now][d^]=ch[u][d^],ch[now][d]=++tot;
now=ch[now][d],u=ch[u][d],sz[now]=sz[u]+;
}
}
int query(int l,int r,int S)
{
int ret=;
for(int i=;~i;i--)
{
bool d=S>>i&;
if(sz[ch[r][d^]]-sz[ch[l][d^]]>)l=ch[l][d^],r=ch[r][d^],ret+=<<i;
else l=ch[l][d],r=ch[r][d];
}
return ret;
}
void update(int L,int R,int x,int l,int r,int rt)
{
if(L>R)return;
if(L<=l&&r<=R){a[rt].push_back(x);return;}
int mid=l+r>>;
if(L<=mid)update(L,R,x,lson);
if(R>mid)update(L,R,x,rson);
}
void calc(int x,int L,int R)
{
top=tot=;
for(int i=L;i<=R;i++)st[++top]=q[i].s,build(rt[top],rt[top-],q[i].v);
for(int i=,k,l,r;i<a[x].size();i++)
{
k=a[x][i];
l=upper_bound(st+,st+top+,p[k].l-)-st-,r=upper_bound(st+,st++top,p[k].r)-st-;
ans[k]=max(ans[k],query(rt[l],rt[r],p[k].x));
}
}
void divide(int l,int r,int rt,int L,int R)
{
if(L>R)return;
int mid=l+r>>,cnt1=,cnt2=;
calc(rt,L,R);
if(l==r)return;
for(int i=L;i<=R;i++)if(q[i].t<=mid)t1[++cnt1]=q[i];else t2[++cnt2]=q[i];
for(int i=;i<=cnt1;i++)q[i+L-]=t1[i];
for(int i=;i<=cnt2;i++)q[i+L-+cnt1]=t2[i];
divide(lson,L,L+cnt1-);
divide(rson,L+cnt1,R);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,x;i<=n;i++)scanf("%d",&x),build(rt[i],rt[i-],x);
for(int i=,op,l,r,x,d;i<=m;i++)
{
scanf("%d%d%d",&op,&l,&r);
if(!op)q[++n1]=(buy){l,r,n1};
else{
scanf("%d%d",&x,&d);
ans[++n2]=query(rt[l-],rt[r],x);
p[n2]=(guest){l,r,max(,n1-d+),n1,x};
}
}
for(int i=;i<=n2;i++)update(p[i].L,p[i].R,i,,n1,);
sort(q+,q+n1+,cmp);
divide(,n1,,,n1);
for(int i=;i<=n2;i++)printf("%d\n",ans[i]);
}

最新文章

  1. js图文讲解
  2. enmo_day_02
  3. 多校6-Key Set 2015-08-09 20:35 2人阅读 评论(0) 收藏
  4. Python练习题 004:判断某日期是该年的第几天
  5. Make AngularJS $http service behave like jQuery.ajax()(转)
  6. 如何不保存Word即可用Word打开想显示的内容!
  7. 点击datalist中Button按钮出现“回发或回调参数无效......”
  8. wscript:329: error: Could not autodetect OpenSSL support. Make sure OpenSSL development packages are
  9. 7月22日-奇舞团关于when.js与promise的分享
  10. Mycat 分片规则详解--数据迁移及节点扩容
  11. Docker入门之--基础知识
  12. layerX参数构建
  13. SSM-SpringMVC-31:SpringMVC中利用hibernate-validator做后台校验
  14. vimperator
  15. kubernetes1.13.5安装部署
  16. 创建数组必须指定数组数目之new运算符避免这种限制
  17. Git安装与配置——详细教程1
  18. openpyxl读取Excel数据
  19. tf.transpose函数的用法讲解
  20. F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解

热门文章

  1. local feature和global feature的理解
  2. LeetCode1217 玩筹码(贪心)
  3. Ican协议建立连接我的感悟
  4. HihoCoder第十周:后序遍历
  5. jsp采用ajax传递数组给后台controller并遍历
  6. pandas中na_values与keep_default_na
  7. Java多线程涉及的概念(3)
  8. Web前端开发CSS规范总结
  9. spring第9天(事务)
  10. js运用sort对json 数组进行排序