TG-/TG 小技巧整理 代码实现 数据结构篇
BIT
单点取 \max 前缀 \max
void ins(int x,int v){//在位置x对v取max
while(x<=n) t[x]=max(t[x],v),x+=(x&-x);
}
int qry(int x){//查询[1...x]的前缀max
int res=0;
while(x) res=max(res,t[x]),x-=(x&-x);
return res;
}
倍增查询第 k 小
int kth(int k){//查询全局第k小
int pos=0,val=0;
for(int x,y,i=__lg(n);i>=0;--i){
x=pos+(1<<i);
if(x>n) continue;
y=val+t[x];//往后跳,并且累加跳的这段的权值
if(y<k) pos=x,val=y;
}
return pos+1;
}
P5094
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5;
int n,t[N],t2[N];//t维护值域和,t2维护值域出现次数
struct cow{
int v,x;
bool operator < (cow b){
return v<b.v;
}
}f[N];
void add(int *p,int x,int v){
while(x<N) p[x]+=v,x+=(x&-x);
}
int sum(int *p,int x){
int res=0;
while(x) res+=p[x],x-=(x&-x);
return res;
}
long long ans;
int main(){
cin>>n;
for(int i=1;i<=n;++i){
cin>>f[i].v>>f[i].x;
}
sort(f+1,f+n+1);
for(int x,y,tot=0,i=1;i<=n;++i){
x=f[i].x; y=f[i].v;
ans+=1ll*y*(x*sum(t2,x)-sum(t,x)+(tot-sum(t,x)-x*(i-1-sum(t2,x))));
add(t,x,x); add(t2,x,1); tot+=x;
}
cout<<ans;
return 0;
}
P2184
#include<bits/stdc++.h>
#define lowbit(x) (x&(-x))
using namespace std;
const int N=1e5+6;
int t1[N],t2[N],n,m;
void add(int *p,int x){
while(x<=n) p[x]++,x+=lowbit(x);
}
int sum(int *p,int x){
int res=0;
while(x) res+=t[x],x-=lowbit(x);
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1,q,L,R;i<=m;++i){
cin>>q>>L>>R;
if(q==1){
add(t1,R); add(t2,L);
}
else cout<<sum(t2,R)-sum(t1,L-1)<<'\n';//稍微推式子之后可以写成这个样子
}
return 0;
}
P4145修改
while(L<=R){
int t=sqrt(a[L]);
add(L,t-a[L]); a[L]=t;
if(a[L]<2){
pre[L]=L+1;
L=find(L);
}
else L++;
}
线段树
结构体线段树
struct node{
//sth here
}t[N<<2];
node pushup(node a,node b){
node res;
//do sth here
return res;
}
node qry(int pos,int L,int R,int x,int y){
if(x<=L&&R<=y) return t[pos];
int mid=(L+R>>1);
if(y<=mid) return qry(pos<<1,L,mid,x,y);
if(x>mid) return qry(pos<<1|1,mid+1,R,x,y);
return pushup(qry(pos<<1,L,mid,x,y),qry(pos<<1|1,mid+1,R,x,y));
P3071查询
int qry(int pos,int L,int R,int x){
if(L==R) return L;
pushdown(pos,L,R);
if(t[pos<<1].val>=x) return qry(pos<<1,L,mid,x);
if(t[pos<<1].r+t[pos<<1|1].l>=x) return mid-t[pos<<1].r+1;//t[pos<<1].r即左子树的后缀连续段长度
return qry(pos<<1|1,mid+1,R,x);
}
01串排序
代码不放了,太简单了。
势能线段树
太长了,放在了这个云剪。
可持久化线段树
区间Mex
这道题是P4137。
有点长,放在了这个云剪
链上第k小
这道题是P2633。
也有点长,放在了这个云剪
堆
对顶堆
下面这个是我封装过后的模板。
struct dheap{//一个维护第k小的对顶堆
priority_queue<int> q;//大根堆
priority_queue<int,vector<int>,greater<int> > q2;//小根堆
int k;
void fix(int x){//改变k,维护对顶堆
k=x;
while(q.size()<k&&!q2.empty())
q.push(q2.top()),q2.pop();
while(q.size()>k)
q2.push(q.top()),q.pop();
}
void ins(int x){//插入一个数x
if(q.empty()||x<=q.top()) q.push(x);
else q2.push(x);
fix(k);
}
int qry(int k){
fix(k);
return q.top();
}//维护好后,查询直接返回即可
};
栈和队列
单调栈
stack<int> s,t;
for(int i=1;i<=n;++i){
while(!s.empty()&&s.top()<a[i]) nxt[t.top()]=i,s.pop(),t.pop();
if(!s.empty()) pre[i]=t.top();
s.push(a[i]); t.push(i);
}
P3400代码:
很长且很板子,就不放了。
P6033求解:
long long ans=0;
for(int i=1;i<n;++i){
for(int j=0;j<=1;++j){
if(!q2.empty()&&q2.front()<q.front()||q.empty()) f[j]=q2.front(),q2.pop();
else f[j]=q.front(),q.pop();
}
ans+=f[0]+f[1],q2.push(f[0]+f[1]);
}
cout<<ans;