发转载只能输入一个链接就发原创了,转载链接代码下面有
树状数组
动态查询前缀和
vector<int>bits; //放树状数组
int N; //原数组的大小+1表示树状数组的大小,为什么要+1呢,因为树状数组多了一个前0个数的和
int query(int x) { //查询前缀和
int res = 0;
for (; x; x -= x & -x) res += bit[x];
return res;
}
void add(int i,int x) { //i位置加x
while(i < N){
bits[i] + = x;
i + = i & - i;
}
}
线段树
功能比树状数组多一点
const int e=100006;
struct qq
{
int maxx;
}tree[4*e];//线段树要开4倍的点的个数
int n,t,a,b;
void updata(int l,int r,int root)
{
if(r<a || l>a) return;
if(r==l)
{
tree[root].maxx=b;
return;
}
int mid=(l+r)/2;
updata(l,mid,root*2);
updata(mid+1,r,root*2+1);
tree[root].maxx=max(tree[root*2].maxx,tree[root*2+1].maxx);
}
int search(int l,int r,int root)
{
if(l>b || r<a) return(-999999999);
if(l>=a && r<=b) return(tree[root].maxx);
int mid=(l+r)/2;
return(max( search(l,mid,root*2), search(mid+1,r,root*2+1)));
}
————————————————
版权声明:本文为CSDN博主「_Tham」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/txl199106/article/details/45374505
字典树
快速查单词用的
class TrieTree{
public:
TrieTree() : root_(make_shared<TrieNode>()){}
void insert(string word_){
auto res = root_;
for(auto c : word_){ // 范围for语句,auto类型推导
if (res->children_[c-'a'] == nullptr){
res->children_[c-'a'] = make_shared<TrieNode>(); // 智能指针分配内存
}
res = res->children_[c-'a'];
}
res->isWord_ = true;
}
bool search(string word){
shared_ptr<TrieNode> res = find(word);
return res != nullptr && res->isWord_ == true;
}
bool startsWith(string prefix){//是否有包含该前缀的单词
return find(prefix) != nullptr;
}
private:
struct TrieNode
{
bool isWord_;
vector<shared_ptr<TrieNode>> children_; // 孩子节点的集合
TrieNode() : isWord_(false), children_(26, nullptr){} // 初始化列表
};
shared_ptr<TrieNode> find(string& prefix){
auto res = root_;
for(int i = 0;i < prefix.size() && res != nullptr; ++i){
res = res->children_[prefix[i] - 'a'];
}
return res;
}
shared_ptr<TrieNode> root_; // 智能指针
};
————————————————
版权声明:本文为CSDN博主「TeddyZhangOvO」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41576121/article/details/95328117