线段树

#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. (一)搭建自己的SpringBoot后台框架整合MyBatis

    一:通过idea工具构建基础框架 1.  打开idea,左上角File→New→Project, 2.  点击Next 3.  点击Next,配置如下图,这里我们选择数据库MySQL和持久层框架MyB ...

  2. Springboot的yml文件

    spring: datasource: # 数据库四大组件 schema-password: root data-username: root driver-class-name: com.mysql ...

  3. c++中的虚函数是什么东西?

    #include <iostream> #include<string> #include<vector> using namespace std; class A ...

  4. A Dangerous Maze LightOJ - 1027

    这题意真是... 题意:你在一个迷宫里,有一些门,每个门有一个参数x,如果为正表明你进入门后可以花x的时间出去,如果为负表明你进入门后可以花-x的时间回到出发的地方.每次回到出发的地方之后,不能记得之 ...

  5. bootstrap div 固定

    div固定在顶部样式: .navbar-fixed-top div固定在底部样式 .navbar-fixed-bottom

  6. AJPFX关于学习java遇到的问题:对算法和数据结构不熟悉

    为什么我先拿“数据结构和算法”说事捏?这玩意是写程序最最基本的东东.不管你使用 Java 还是其它的什么语言,都离不开它.而且这玩意是跨语言的,学好之后不管在哪门语言中都能用得上. 既然“数据结构和算 ...

  7. 【学习笔记】深入理解js原型和闭包(16)——完结

    之前一共用15篇文章,把javascript的原型和闭包讲解了一下. 首先,javascript本来就“不容易学”.不是说它有多难,而是学习它的人,往往都是在学会了其他语言之后,又学javascrip ...

  8. STM32 modbus CRC16校验

    typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef int int32_t; const uint16_t ...

  9. IOStableviewsectionSet

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {     if (t ...

  10. 第一次提交代码到github时经常遇到的问题

    最近两年在OpenStack方面做了一些工作,写了一些实验性的plugin. 本着Open Source的共享精神,想尝试提交到github,以便他人能下载使用. 当你注册完github帐号之后,点击 ...