码迷,mamicode.com
首页 > 其他好文 > 详细

模板合集

时间:2019-08-29 20:20:24      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:queue   size   next   read   hup   std   opera   for   date   

SPFA

#include<bits/stdc++.h>
using namespace std;
struct EE{
    int x,y,dis,next;
}edge[550000];
int len = 0,last[11000] = {0};
int n,m;
void ins(int xx,int yy,int dd){
    len++;
    edge[len].x = xx,edge[len].y = yy,edge[len].dis = dd;
    edge[len].next = last[xx]; 
    last[xx] = len;
}
int d[11000];
queue<int> q;
bool used[11000] = {0};
const int inf = 2147483647;
int s;
int main(){
    scanf("%d%d%d",&n,&m,&s);
    for(int i = 1;i<=m;i++){
        int ai,bi,ci;
        scanf("%d%d%d",&ai,&bi,&ci);
        ins(ai,bi,ci);
    }
    for(int i = 1;i<=n;i++)d[i] = inf;
    d[s] = 0;
    q.push(s);
    while(!q.empty()){
        int nw = q.front();
        int res = last[nw];
        while(res!=0){
            int rd = d[nw]+edge[res].dis;
            int i = edge[res].y;
            if(d[i]>rd){
                d[i] = rd;
                if(!used[i]){
                    q.push(i);
                    used[i] = 1;
                }
            }
            res = edge[res].next;
        }
        q.pop();
        used[nw] = 0;
    }
    for(int i = 1;i<=n;i++) printf("%d ",d[i]);
    return 0;
}

dijkstra + heap

#include<bits/stdc++.h>
using namespace std;
const long long inf = 9223372036854775807;
long long dis[2001000];
long long eh[200000] = {0};
bool used[200000] = {0};
struct EE{
    long long st,val,ne;
}edge[2001000];
long long n,m,s;
inline void Read(){
    scanf("%lld%lld%lld",&n,&m,&s);
    for(long long  i = 1;i<=m;i++){
        long long x;
        scanf("%lld%lld%lld",&x,&edge[i].st,&edge[i].val);
        edge[i].ne = eh[x];
        eh[x] = i;
    }
}
struct node{
    long long pos;
    long long dis;
    bool operator <( const node &x )const
    {
        return x.dis<dis;
    }
};
std::priority_queue<node> q;
node nw;
inline void Print(){
    for(long long i= 1;i<=n;i++)printf("%lld ",dis[i]);
}   
int main(){
    Read();
    for(long long i = 1;i<=n;i++)dis[i] = inf;
    dis[s] = 0;
    q.push((node){s,0});
    while(!q.empty()){
    nw = q.top();
    q.pop();
    long long d = nw.dis;
    long long p = nw.pos;
    if(used[p])continue;
    used[p] = 1;
    for(long long i = eh[p];i;i = edge[i].ne)
        if(dis[edge[i].st]>dis[p]+edge[i].val){
            dis[edge[i].st] = dis[p]+edge[i].val;
            if(!used[edge[i].st]) q.push((node){edge[i].st,dis[edge[i].st]});
        }
    }
    Print();
    return 0;
}

线段树

#include<bits/stdc++.h>
using namespace std;
struct node{
    long long l,r;
    long long c,add;
}tr[510000] = {0};
long long a[200007];
int n,m;
inline void bt(int cnt,int l,int r){
    tr[cnt].l = l;
    tr[cnt].r = r;
    tr[cnt].add = 0;
    if(l==r){
        tr[cnt].c = a[l];
        return;
    }
    int mid = (l+r)/2;
    bt(cnt*2,l,mid);
    bt(cnt*2+1,mid+1,r);
    tr[cnt].c = tr[cnt*2].c+tr[cnt*2+1].c;
    
}
inline void spread(int p){
    if(tr[p].add){
        tr[p*2].c += tr[p].add*(tr[p*2].r-tr[p*2].l+1);
        tr[p*2+1].c += tr[p].add*(tr[p*2+1].r-tr[p*2+1].l+1);
        tr[p*2].add += tr[p].add;
        tr[p*2+1].add += tr[p].add;
        tr[p].add = 0;
    }
}
inline void change(int nw,int l,int r,int k){
    if(l<=tr[nw].l&&r>=tr[nw].r){
        tr[nw].c += (long long)k*(tr[nw].r-tr[nw].l+1);
        tr[nw].add += k;
        return;
    }
    spread(nw);
    int mid = (tr[nw].l+tr[nw].r)/2;
    if(l<=mid) change(nw*2,l,r,k);
    if(r>mid) change(nw*2+1,l,r,k);
    tr[nw].c = tr[nw*2].c+tr[nw*2+1].c;
}
inline long long find(int nw,int l,int r){
    if(l==tr[nw].l&&r==tr[nw].r) return tr[nw].c;
    spread(nw);
    int mid = (tr[nw].l+tr[nw].r)/2;
    if(r<=mid) return find(nw*2,l,r);
    if(l>mid) return find(nw*2+1,l,r);
    return find(nw*2,l,mid)+find(nw*2+1,mid+1,r);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;i++) scanf("%lld",&a[i]);
    bt(1,1,n);
    int tp;
    for(int i = 1;i<=m;i++){
        scanf("%d",&tp);
        if(tp==1){
            int x,y,k;
            scanf("%d%d%d",&x,&y,&k);
            change(1,x,y,k);
        }else{
            int x,y;
            scanf("%d%d",&x,&y);
            printf("%lld\n",find(1,x,y));
        }
    }
    return 0;
}

主席树(可持久化线段树)

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[200010],root[200010] = {0},b[200010],c[200010];
struct node{
    int l,r,sum;
}tr[8000100];
int mc = 0,cnt = 0;
int find(int x){
    return lower_bound(c+1,c+mc+1,x)-c;
}
void update(int l,int r,int &x,int y,int pos){
    tr[++cnt] = tr[y],tr[cnt].sum++,x = cnt;
    if(l==r) return;
    int mid = (l+r)/2;
    if(mid>=pos) update(l,mid,tr[x].l,tr[y].l,pos);
    else update(mid+1,r,tr[x].r,tr[y].r,pos);
}
int query(int l,int r,int x,int y,int k){
    if(l==r) return l;
    int mid = (l+r)/2;
    int sum = tr[tr[y].l].sum-tr[tr[x].l].sum;
    if(sum>=k) return query(l,mid,tr[x].l,tr[y].l,k);
    else return query(mid+1,r,tr[x].r,tr[y].r,k-sum);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;i++) scanf("%d",&a[i]),b[i] = a[i];
    sort(b+1,b+n+1);
    for(int i = 1;i<=n;i++)
        if(i==1||b[i]!=b[i-1]) c[++mc] = b[i];
    for(int i = 1;i<=n;i++)update(1,n,root[i],root[i-1],find(a[i]));
    for(int i = 1;i<=m;i++){
        int x,y,k;
        scanf("%d%d%d",&x,&y,&k);
        printf("%d\n",c[query(1,n,root[x-1],root[y],k)]);
    }
    return 0;
}

树状数组

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[660001];
int c[660001] = {0};
int lowbit(int x){
    return x&(-x);
}
void bt(){
    for(int i = 1;i<=n;i++){
        int lb = lowbit(i);
        for(int j = i;j>=(i-lb+1);j--)
            c[i]+=a[j];
    }
}
int query(int x){
    int res = 0;
    while(x!=0){
        res+= c[x];
        x-=lowbit(x);
    }
    return res;
}
void update(int x,int y){
    for(int i = x;i<=n;i+=lowbit(i))
        c[i]+=y;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;i++)
        scanf("%d",&a[i]);
    bt();
    for(int i = 1;i<=m;i++){
        int tp,x,y;
        scanf("%d%d%d",&tp,&x,&y);
        if(tp==1){
            update(x,y);
        }else printf("%d\n",query(y)-query(x-1));
    }
    return 0;
}

树链剖分

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct EE{
    int to,nxt;
}edge[320000] = {0};
int cnt = 0;
int h[160000] = {0};
void ins(int x,int y){
    edge[++cnt].to = y;
    edge[cnt].nxt = h[x];
    h[x] = cnt;
}
int n,m,r,mod;
int val[160000] = {0};
int dep[160000] = {0};
int fa[160000] = {0};
int son[160000] = {0};
int mxson[160000] = {0};
void dfs1(int u,int ff){
    dep[u] = dep[ff] + 1;
    fa[u] = ff;
    son[u] = 1;
    int res = 0;
    for(int i = h[u];i;i = edge[i].nxt){
        int v = edge[i].to;
        if(v==ff) continue;
        dfs1(v,u);
        son[u] += son[v];
        if(son[v]>res) res = son[v],mxson[u] = v;
    }
}
int cntd = 0;
int num[160000] = {0};
int seg[160000] = {0};
int top[160000] = {0};
void dfs2(int u,int st){
    num[u] = ++cntd;
    seg[cntd] = val[u];
    top[u] = st;
    if(son[u]==1)return;
    dfs2(mxson[u],st);
    for(int i = h[u];i;i = edge[i].nxt){
        int v = edge[i].to;
        if(v!=fa[u]&&v!=mxson[u]) dfs2(v,v);
    }
}
//---------Segment Tree
struct tt{
    int lazy,c,l,r;
}tr[640000] = {0};
void bt(int nw,int l,int r){
    tr[nw].l =  l;
    tr[nw].r =  r;
    if(l==r){
        tr[nw].c = seg[l];
        return;
    }
    int mid = (l+r)/2;
    bt(nw*2,l,mid);
    bt(nw*2+1,mid+1,r);
    tr[nw].c = (tr[nw*2].c+tr[nw*2+1].c) % mod;
}
void pushdown(int nw){
    if(tr[nw].lazy==0) return;
    tr[nw*2].lazy += tr[nw].lazy;
    tr[nw*2+1].lazy += tr[nw].lazy;
    tr[nw*2].c += (tr[nw*2].r-tr[nw*2].l+1) % mod * tr[nw].lazy % mod;
    tr[nw*2+1].c += (tr[nw*2+1].r-tr[nw*2+1].l+1) %mod * tr[nw].lazy % mod;
    tr[nw*2].lazy%=mod;    
    tr[nw*2+1].lazy%=mod;
    tr[nw*2].c%=mod;
    tr[nw*2+1].c%=mod;
    tr[nw].lazy = 0;
}
void update(int nw,int x,int y,int d){
    if(tr[nw].l==x&&tr[nw].r==y){
        tr[nw].lazy += d;
        tr[nw].lazy%=mod;
        tr[nw].c += d*(tr[nw].r-tr[nw].l+1)%mod;
        tr[nw].c%=mod;
        return;
    }
    pushdown(nw);
    int mid = (tr[nw].l+tr[nw].r)/2;
    if(y<=mid) update(nw*2,x,y,d);
    else if(x>mid) update(nw*2+1,x,y,d);
    else update(nw*2,x,mid,d),update(nw*2+1,mid+1,y,d);
    tr[nw].c = (tr[nw*2].c+tr[nw*2+1].c)%mod;
}
int query(int nw,int x,int y){
    if(tr[nw].l==x&&tr[nw].r==y){
        return tr[nw].c;
    }
    pushdown(nw);
    int mid = (tr[nw].l+tr[nw].r)/2;
    if(y<=mid) return query(nw*2,x,y);
    else if(x>mid) return query(nw*2+1,x,y);
    else return (query(nw*2,x,mid)+query(nw*2+1,mid+1,y))%mod;
}
//----------------------
int opt1(int x,int y){
    int res = 0;
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        int sc = num[x];
        int se = num[top[x]];
        res += query(1,se,sc);
        res %= mod;
        x = fa[top[x]];
    }
    int sp = num[x],sd = num[y];
    if(sp>sd) swap(sp,sd);
    res += query(1,sp,sd);
    res %= mod;
    return res;
}
void opt2(int x,int y,int d){
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        int sc = num[x];
        int se = num[top[x]];
        update(1,se,sc,d);
        x = fa[top[x]];
    }
    int sp = num[x],sd = num[y];
    if(sp>sd) swap(sp,sd);
    update(1,sp,sd,d);
}
signed main(){
    scanf("%lld%lld%lld%lld",&n,&m,&r,&mod);
    for(int i = 1;i<=n;i++){
        scanf("%lld",&val[i]);
        val[i] %= mod;
    }
    for(int i = 1;i<n;i++){
        int x,y;
        scanf("%lld%lld",&x,&y);
        ins(x,y);
        ins(y,x);
    } 
    dfs1(r,0);
    dfs2(r,r);
    bt(1,1,n);
    for(int i = 1;i<=m;i++){
        int opt,x,y,z;
        scanf("%lld",&opt);
        if(opt==1){
            scanf("%lld%lld%lld",&x,&y,&z);
            opt2(x,y,z);
        }else if(opt==2){
            scanf("%lld%lld",&x,&y);
            printf("%lld\n",opt1(x,y));
        }else if(opt==3){
            scanf("%lld%lld",&x,&y);
            update(1,num[x],num[x]+son[x]-1,y);
        }else{
            scanf("%lld",&x);
            printf("%lld\n",query(1,num[x],num[x]+son[x]-1));
        }
    }
    return 0;
}

treap

#include<bits/stdc++.h>
using namespace std;
const int inf = 1e7+1;
int root = 1;
struct treap{
    int fa,son[2],c,cnt,cnts,lv;
}tr[220000] = {0};
int cnte = 1;
int used[220000] = {0};
void pushup(int nw){
    tr[nw].cnt = tr[nw].cnts + tr[tr[nw].son[0]].cnt + tr[tr[nw].son[1]].cnt;
}
void rotate(int &nw,int tp){
    int k = tr[nw].son[tp^1];
    tr[nw].son[tp^1] = tr[k].son[tp];
    tr[k].son[tp] = nw;
    pushup(nw);
    pushup(k);
    nw = k;
}
void ins(int &p,int c){
    if(!used[p]){
        used[p] = 1;
        tr[p].c = c;
        tr[p].cnt = 1;
        tr[p].cnts = 1;
        tr[p].lv = rand();
        if(!tr[p].son[0])tr[p].son[0] = ++cnte;
        if(!tr[p].son[1])tr[p].son[1] = ++cnte;
        return;
    }
    if(tr[p].c == c){
        tr[p].cnts++;
        tr[p].cnt++;
        return;
    }
    int tp = c>tr[p].c;
    ins(tr[p].son[tp],c);
    if(tr[p].lv < tr[tr[p].son[tp]].lv) rotate(p,tp^1);
    pushup(p);
}
void del(int &p,int c){
    if(!used[p]) return;
    if(c!=tr[p].c) del(tr[p].son[c>tr[p].c],c);
    else{
        if(!used[tr[p].son[0]]&&!used[tr[p].son[1]]){
            tr[p].cnt--;
            tr[p].cnts--;
            if(tr[p].cnt==0) used[p] = 0; 
        }else if(used[tr[p].son[0]]&&!used[tr[p].son[1]]){
            rotate(p,1);
            del(tr[p].son[1],c);
        }else if(!used[tr[p].son[0]]&&used[tr[p].son[1]]){
            rotate(p,0);
            del(tr[p].son[0],c);
        }else if(used[tr[p].son[0]]&&used[tr[p].son[1]]){
            int tp = (tr[tr[p].son[0]].lv>tr[tr[p].son[1]].lv);
            rotate(p,tp);
            del(tr[p].son[tp],c);
        }
    }
    pushup(p);
}
int opt3(int p,int c){
    if(!used[p]) return 0;
    if(tr[p].c==c) return tr[tr[p].son[0]].cnt+1;
    if(tr[p].c<c) return tr[tr[p].son[0]].cnt + tr[p].cnts + opt3(tr[p].son[1],c);
    if(tr[p].c>c) return opt3(tr[p].son[0],c);
}
int opt4(int p,int rk){
    if(!used[p]) return 0;
    if(tr[tr[p].son[0]].cnt>=rk) return opt4(tr[p].son[0],rk);
    if(tr[tr[p].son[0]].cnt+tr[p].cnts>=rk) return tr[p].c;
    return opt4(tr[p].son[1],rk-tr[tr[p].son[0]].cnt-tr[p].cnts);
}
int opt5(int p,int c){
    if(!used[p]) return -inf;
    if(tr[p].c>=c) return opt5(tr[p].son[0],c);
    else return max(tr[p].c,opt5(tr[p].son[1],c));
}
int opt6(int p,int c){
    if(!used[p]) return inf;
    if(tr[p].c<=c) return opt6(tr[p].son[1],c);
    else return min(tr[p].c,opt6(tr[p].son[0],c));
}
int n;
int main(){
    srand(time(0));
    scanf("%d",&n);
    for(int i = 1;i<=n;i++){
        int opt,x;
        scanf("%d%d",&opt,&x);
        if(opt==1){
            ins(root,x);
        }else if(opt==2){
            del(root,x);
        }else if(opt==3){
            printf("%d\n",opt3(root,x));
        }else if(opt==4){
            printf("%d\n",opt4(root,x));
        }else if(opt==5){
            printf("%d\n",opt5(root,x));
        }else if(opt==6){
            printf("%d\n",opt6(root,x));
        }
    }
    return 0;
}

LCA

#include<bits/stdc++.h>
using namespace std;
struct EE{
    int to,nxt;
}edge[1010000];
int h[660000] = {0};
int cnt = 0;
void ins(int x,int y){
    edge[++cnt].to = y;
    edge[cnt].nxt = h[x];
    h[x] = cnt;
}
int n,m,s;
int f[660000][77] = {0};
int dep[660000] = {0};
int lg[660000] = {0};
void dfs(int u,int fa){
    f[u][0] = fa;
    for(int i = 1;(1<<i)<=dep[u];i++){
        f[u][i] = f[f[u][i-1]][i-1];
    }
    for(int i = h[u];i;i = edge[i].nxt){
        int v = edge[i].to;
        if(v==fa) continue;
        dep[v] = dep[u]+1;
        dfs(edge[i].to,u);
    }
}
void calclg(){
    for(int i = 1;i<=n;i++){
        lg[i] = lg[i-1]+((1<<lg[i-1])==i);
    }
}
int main(){
    scanf("%d%d%d",&n,&m,&s);
    for(int i = 1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        ins(x,y);
        ins(y,x);
    }
    dfs(s,-1);
    calclg();
    for(int i = 1;i<=m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        if(dep[x]<dep[y]) swap(x,y);
        while(dep[x]>dep[y])
            x = f[x][lg[dep[x]-dep[y]]-1];
        if(x==y){
            printf("%d\n",x);
            continue;
        }
        for(int k = lg[dep[x]]-1;k>=0;k--)
            if(f[x][k]!=f[y][k]){
                x = f[x][k],y = f[y][k];
            }
        printf("%d\n",f[x][0]);
    }
    return 0;
}

Dinic

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y,c,next,other;
}a[2110000];
int len,last[11000],st,ed;
int n,f,d;
void ins(int x,int y,int c){
    len++;int k1 = len;
    a[len].x = x,a[len].y = y,a[len].c = c;
    a[len].next = last[x];
    last[x] = len;
    len++; int k2 = len;
    a[len].x = y,a[len].y = x,a[len].c = 0;
    a[len].next = last[y];
    last[y] = len;
    a[k1].other = k2;
    a[k2].other = k1;
}
queue<int> q;
int h[11000],m;
bool build_h(){ // BFS
    memset(h,0,sizeof(h));
    h[st] = 1;
    q.push(st);
    while(!q.empty()){
        int x = q.front();
        for(int k = last[x];k;k=a[k].next){
            int y = a[k].y;
            if(a[k].c>0&&h[y]==0){
                h[y] = h[x]+1;
                q.push(y);
            }
        }
        q.pop();
    }
    if(h[ed]>0)return 1;
    else return 0;
}
int findflow(int x,int f){ //DFS
    if(x==ed) return f;
    int s = 0,t;
    for(int i = last[x];i;i=a[i].next){
        int y = a[i].y;
        if(a[i].c>0&&h[y]==(h[x]+1)&&s<f){
            s+=(t = findflow(y,min(a[i].c,f-s)));
            a[i].c -= t;a[a[i].other].c+=t;
        }
    }
    if(s==0)h[x] = 0;
    return s;
}
int main(){
    scanf("%d%d%d%d",&n,&m,&st,&ed);
    len = 0;
    memset(last,0,sizeof(last));
    int s = 0,t;
    
    int ai,bi,ci;//(qwq)
    for(int i = 1;i<=m;i++){
        scanf("%d%d%d",&ai,&bi,&ci);
        ins(ai,bi,ci);
    }
    while(build_h()){
        s+=findflow(st,99999999);
    }
    printf("%d\n",s);
    return 0;
}

exgcd

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,b;
int a1,b1;
int exgcd(int a,int b,int &x,int &y){
    if(b==0){
        x = 1,y = 0;
        return a;
    }
    int res = exgcd(b,a%b,x,y);
    int tmp = x;
    x = y,y = tmp-y*(a/b);
    return res;
}
signed main(){
    scanf("%lld%lld",&a,&b);
    exgcd(a,b,a1,b1);
    printf("%lld",(a1%b+b)%b);
    return 0;
}

模板合集

标签:queue   size   next   read   hup   std   opera   for   date   

原文地址:https://www.cnblogs.com/LJA001100/p/11431644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!