传送门

文章目录

  • 题意:
  • 思路:

题意:

思路:

猫树是一种可以O(nlogn)O(nlogn)O(nlogn)预处理,O(1)O(1)O(1)查询的数据结构。预处理的信息应该满足可合并的性质,与线段树pushuppushuppushup的原理相同,道理上线段树能维护的猫树应该都能维护。
猫树需要将原序列补满为222的幂次,维护信息的时候只需要buildbuildbuild一下,维护出来[l,mid],[mid+1,r][l,mid],[mid+1,r][l,mid],[mid+1,r]的信息,让后查询[l,r][l,r][l,r]的时候,只需要找到midmidmid的位置,让后用预处理出来的信息直接合并即可。
由于猫树满足堆式存储,所以要找两个点[l,l],[r,r][l,l],[r,r][l,l],[r,r]的lcalcalca的话只需要找到最长公共前缀即可,我们预处理一个logloglog数组,将两个位置的二进制异或一下,可以发现相同前缀都变为了000,让后再用原来的减去异或后的二进制位数即可。
但是猫树不能支持修改,除非保证修改很少,每次可以重建猫树?没什么意义。。

参考资料

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=100010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int a[N];
int p[30][N],s[30][N],pos[N];
int lg[N<<4];void build(int u,int l,int r,int d) {if(l==r) {pos[l]=u;return;}int pre=0,sum=0;int mid=(l+r)>>1;p[d][mid]=s[d][mid]=pre=sum=a[mid]; sum=max(sum,0);for(int i=mid-1;i>=l;i--) {pre+=a[i]; sum+=a[i];p[d][i]=max(p[d][i+1],sum); s[d][i]=max(s[d][i+1],pre);sum=max(sum,0);}p[d][mid+1]=s[d][mid+1]=pre=sum=a[mid+1]; sum=max(sum,0);for(int i=mid+2;i<=r;i++) {pre+=a[i]; sum+=a[i];p[d][i]=max(p[d][i-1],sum); s[d][i]=max(s[d][i-1],pre);sum=max(sum,0);}build(L,l,mid,d+1); build(R,mid+1,r,d+1);
}int query(int l,int r) {int d=lg[pos[l]]-lg[pos[l]^pos[r]];return max(max(p[d][l],p[d][r]),s[d][l]+s[d][r]);
}int main()
{//  ios::sync_with_stdio(false);
//  cin.tie(0);cin>>n; int len=2;while(len<n) len<<=1;for(int i=1;i<=n;i++) scanf("%d",&a[i]);// for(int i=1;i<=n<<4;i++) lg[i]=lg[i>>1]+1;for(int i=2,l=len<<1;i<=l;++i)lg[i]=lg[i>>1]+1;build(1,1,len,1);int m; scanf("%d",&m);while(m--) {int l,r; scanf("%d%d",&l,&r);printf("%d\n",query(l,r));}return 0;
}
/**/

SP1043 GSS1 - Can you answer these queries I 猫树相关推荐

  1. SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))

    题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...

  2. SP1043 GSS1 - Can you answer these queries I(猫树)

    给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y}. 给定M ...

  3. SPOJ GSS2 Can you answer these queries II (线段树离线) - xgtao -

    Can you answer these queries II 这是一道线段树的题目,维护历史版本,给出N(<=100000)个数字(-100000<=x<=100000),要求求出 ...

  4. HDU 4027 Can you answer these queries?(线段树/区间不等更新)

    传送门 Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/6576 ...

  5. 解题报告:SP1043 GSS4 - Can you answer these queries III(GSS线段树八部曲之三)(区间最大连续子段和)

    要注意输入的数据有坑,x可能大于y- 我们可以模块化编程,使得整个代码井井有条 函数可以重名,只要参数不一样就行. 来源 yxc老师的上课板书 然后就是简单的代码了 #include<iostr ...

  6. 另一个画风的GSS1 - Can you answer these queries I(猫树)

    前言 其实我觉得你看猫锟的解释也看不懂(主要是还有一些不良心的讲解者不讲清楚,当然这里不是针对了qwq) 猫锟链接 Solution 考虑我们的线段树是个啥玩意? 每一层都是一堆区间叠在一起. 我们在 ...

  7. Can you answer these queries III (线段树维护最大子段和)

    题意: 求一个区间的最大连续和. 0:表示把A[x]改成y 1:表示求[x,y]这个区间的最大连续和. 题解: 线段树维护四个变量. 倒着讲,先来看如何维护这四个变量. summax代表这个区间连续最 ...

  8. SPOJ - GSS3 Can you answer these queries III(线段树+区间合并)

    题目链接:点击查看 题目大意:给出一个长度为n的序列,进行m次操作: 1 x y  查询区间[l,r]中的最大连续子段和 0 x y  将第x个数修改为y 题目分析:因为涉及到单点修改和区间查询等操作 ...

  9. [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和.昨晚思前想后找有没有一个数学上的 ...

最新文章

  1. keras,在 fit 和 evaluate 中 都有 verbose 这个参数标记是否打印进度条
  2. 正则表达式(2.实例)
  3. Linux wc命令统计文件大小
  4. Hive 之 分析窗口函数
  5. mysql排序区分大小写吗_MySQL的order by时区分大小写
  6. 银行数据仓库体系实践(13)--数据应用之监管报送
  7. 转载“用USBOOT制作DOS启动盘”
  8. 数据分析案例-电影数据可视化分析
  9. 投资理财之基金篇(一) - 认识基金
  10. 自建微信公众号文章搜索舆情系统
  11. 修改图片exif信息
  12. 企业代码提交和发布流程
  13. Spark SQL的selectExpr用法
  14. unsw计算机专业排名,新南威尔士大学UNSW计算机科学Computer Science专业排名第54位(2021年THE世界大学商科排名)...
  15. 金蝶cloud后台数据库表说明
  16. Windows10 22H2 19045.2130推送了!
  17. Apache安装部署
  18. 干货!12个程序员证书​,含金量超高
  19. ORB-SLAM2 特征点法SLAM 单目 双目 rgbd相机SLAM 单应/本质矩阵恢复运动 小图大图地图优化
  20. 浅谈Asterisk的呼叫转接功能

热门文章

  1. 听说麦当劳,买一个雪糕就送一个男友!
  2. 雨中的蚊子为啥不会被雨滴砸死?
  3. 这相册一出手,哪个长辈搞不定?
  4. π!到底蕴藏了多少不为人知的秘密?|今日最佳
  5. 把握人工智能命脉的有效方法
  6. 计算机游戏50关,YELLOW游戏全50关攻略
  7. 传递集合对象_面试必备——Java集合框架
  8. python气象数据处理与绘图_Python气象数据处理与绘图:纬高图的另一种思路
  9. ros发布节点信息python_vscode开发ROS1(13)-python实现话题通信(msg)
  10. idea shell 中的函数 跳转_SpringBoot项目打包+shell脚本部署实践,太有用了