线段树

#include<cstdio>
using namespace std;
int n,p,a,b,m,x,y,ans;
struct node
{
int l,r,w,f;
}tree[];
inline void build(int k,int ll,int rr)//建树
{
tree[k].l=ll,tree[k].r=rr;
if(tree[k].l==tree[k].r)
{
scanf("%d",&tree[k].w);
return;
}
int m=(ll+rr)/;
build(k*,ll,m);
build(k*+,m+,rr);
tree[k].w=tree[k*].w+tree[k*+].w;
}
inline void down(int k)//标记下传
{
tree[k*].f+=tree[k].f;
tree[k*+].f+=tree[k].f;
tree[k*].w+=tree[k].f*(tree[k*].r-tree[k*].l+);
tree[k*+].w+=tree[k].f*(tree[k*+].r-tree[k*+].l+);
tree[k].f=;
}
inline void ask_point(int k)//单点查询
{
if(tree[k].l==tree[k].r)
{
ans=tree[k].w;
return ;
}
if(tree[k].f) down(k);
int m=(tree[k].l+tree[k].r)/;
if(x<=m) ask_point(k*);
else ask_point(k*+);
}
inline void change_point(int k)//单点修改
{
if(tree[k].l==tree[k].r)
{
tree[k].w+=y;
return;
}
int m=(tree[k].l+tree[k].r)/;
if(x<=m) change_point(k*);
else change_point(k*+);
tree[k].w=tree[k*].w+tree[k*+].w;
}
inline void ask_interval(int k)//区间查询
{
if(tree[k].l>=a&&tree[k].r<=b)
{
ans+=tree[k].w;
return;
}
if(tree[k].f) down(k);
int m=(tree[k].l+tree[k].r)/;
if(a<=m) ask_interval(k*);
if(b>m) ask_interval(k*+);
}
inline void change_interval(int k)//区间修改
{
if(tree[k].l>=a&&tree[k].r<=b)
{
tree[k].w+=(tree[k].r-tree[k].l+)*y;
tree[k].f+=y;
return;
}
if(tree[k].f) down(k);
int m=(tree[k].l+tree[k].r)/;
if(a<=m) change_interval(k*);
if(b>m) change_interval(k*+);
tree[k].w=tree[k*].w+tree[k*+].w;
}
int main()
{
scanf("%d",&n);//n个节点
build(,,n);//建树
scanf("%d",&m);//m种操作
for(int i=;i<=m;i++)
{
scanf("%d",&p);
ans=;
if(p==)
{
scanf("%d",&x);
ask_point(x);//单点查询,输出第x个数
printf("%d",ans);
}
else if(p==)
{
scanf("%d%d",&x,&y);
change_point();//单点修改
}
else if(p==)
{
scanf("%d%d",&a,&b);//区间查询
ask_interval();
printf("%d\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&y);//区间修改
change_interval();
}
}
}

KMP

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
char a[];
char b[];
int nxt[];
int la,lb;
void makenext()
{
int k=-,j=;
nxt[j]=k;
while(j<lb)
{
if(k==-||b[j]==b[k])
{
k++,j++;
nxt[j]=k;
}
else
k=nxt[k];
}
}
void kmp()
{
makenext();
int i=,j=;
while(i<la)
{
if(j==-||a[i]==b[j])
j++,i++;
else
j=nxt[j];
if(j==lb)
{
cout<<i-lb+<<endl;
j=,i--;
}
}
}
int main()
{
cin>>a;
cin>>b;
la=strlen(a);
lb=strlen(b);
kmp();
for(int i=;i<=lb;i++)
cout<<nxt[i]<<" ";
return ;
}

HASH

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#define num 500003
using namespace std;
int hash[*num];
int star[];
bool vis[];
int main()
{
memset(hash,-,sizeof(hash));
int n;
cin>>n;
for(int i=;i<n;i++)
{
int x;
bool mark=;
cin>>x;
int y=abs(x)%num;
while(hash[y]>=-)
{
if(hash[y]==x)
{
mark=;
break;
}
y++;
}
hash[y]=x;
if(mark==)
cout<<;
else
cout<<;
}
return ;
}

模板来源

http://www.cnblogs.com/IUUUUUUUskyyy/

 

线段树、KMP、HASH模板的更多相关文章

  1. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  2. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

  3. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...

  4. hdu1698(线段树区间替换模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示 ...

  5. BZOJ 2124: 等差子序列 线段树维护hash

    2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...

  6. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题

    我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...

  7. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 141093 ...

  8. bzoj 2124 等差子序列 (线段树维护hash)

    2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 1922  Solved: 714[Submit][Status][Discuss ...

  9. 【luogu P3372 线段树1】 模板

    线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...

  10. 【线段树 集合hash】bzoj4373: 算术天才⑨与等差数列

    hash大法好(@ARZhu):大数相乘及时取模真的是件麻烦事情 Description 算术天才⑨非常喜欢和等差数列玩耍.有一天,他给了你一个长度为n的序列,其中第i个数为a[i].他想考考你,每次 ...

随机推荐

  1. qq教xixi写模拟加法【非常爆炸】

    #include<iostream> #include<cstdio> #include<math.h> #include<queue> #includ ...

  2. thinkphp5升级版开源框架tpframe v2.1发布

    免费开源框架tpframe是一款以thinkphp5为驱动,在此基础上进行进一步的完善与改进的框架,保持了ThinkPHP5原有的所有特性,优化核心,减少依赖,为个人或企业建站提供高效.快速解决的方案 ...

  3. [Swift]有用的Binary Heap Type类

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. 人品问题 树形dp

    题目 网上出现了一种高科技产品——人品测试器.只要你把你的真实姓名输入进去,系统将自动输出你的人品指数.把儿不相信自己的人品为0.经过了许多研究后,把儿得出了一个更为科学的人品计算方法.这种方法的理论 ...

  5. 492 Construct the Rectangle 构建矩形

    详见:https://leetcode.com/problems/construct-the-rectangle/description/ C++: class Solution { public: ...

  6. 浅析 Spark Shuffle 内存使用

    在使用 Spark 进行计算时,我们经常会碰到作业 (Job) Out Of Memory(OOM) 的情况,而且很大一部分情况是发生在 Shuffle 阶段.那么在 Spark Shuffle 中具 ...

  7. Java-学完一个月总结(javaSe学习路线)

    JavaSe的一个月 第一周 0410 基本数据类型:数据类型的转换:运算符:导入删除项目0411 分支结构if else:switch case ;while0412 do while ;for / ...

  8. logging模块基础

    很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...

  9. Git-merge & rebase(变基)

    在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. merge: merge有两种方法,fast-forward(快速合并)和three-way merge(三方合并 ...

  10. HTTP协议 处理流程

    我们平时在浏览网页的时候都是使用浏览器,输入你要的网址后回车,就会显示出我们所想要的内容,看似这个简单的用户操作行为的背后,Web的工作原理是怎样的呢?到底隐藏了些什么呢? 对于传统的上网流程,系统它 ...