存板子专用

news/2024/8/26 18:26:15

某谷树剖模板

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define ull unsigend long long
#define rep(k,i,j) for(int k = i;k <= j; ++k)
#define FOR(k,i,j) for(int k = i;k >= j; --k)
inline int read(){
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
    return x*f;
}
const int mxn = 2e5+10;
int n,m,root,p;
int tp[mxn],fa[mxn],id[mxn],val[mxn],dep[mxn],sz[mxn],link[mxn],son[mxn];
struct edge{int nxt,y;}e[mxn<<1];
struct Tree{int l,r,tag,sum;}tree[mxn<<2];
int len,to[mxn];
int res = 0,cnt;
inline void add(int xx,int yy){
    e[++len].nxt = to[xx];
    to[xx] = len;
    e[len].y = yy;
}
inline void pushdown(int pos,int lth){
    int tag_ = tree[pos].tag;
    if(tag_){
        tree[pos<<1].tag += tag_;
        tree[pos<<1|1].tag += tag_;
        tree[pos<<1].sum += tag_*(lth-(lth>>1));
        tree[pos<<1|1].sum += tag_*(lth>>1);
        tree[pos<<1].sum %= p;
        tree[pos<<1|1].sum %= p;
        tree[pos].tag = 0;
    }
}
inline void build(int pos,int l,int r){
    if(l==r){
        tree[pos].sum = link[l];
        if(tree[pos].sum > p) tree[pos].sum %= p;
        return;
    }
    int m = l+r >>1;
    build(pos<<1,l,m),build(pos<<1|1,m+1,r);
    tree[pos].sum = (tree[pos<<1].sum + tree[pos<<1|1].sum)%p;
}
inline void query(int pos,int lp,int rp,int l,int r){
    if(l<=lp&&rp<=r){
        res += tree[pos].sum;
        res %= p;
        return;
    }else{
    pushdown(pos,rp-lp+1);
    int m = lp+rp >>1;
    if(l<=m) query(pos<<1,lp,m,l,r);
    if(r>m) query(pos<<1|1,m+1,rp,l,r);
    }
}
inline void update(int pos,int lp,int rp,int l,int r,int k){
    if(l<=lp&&rp<=r){
        tree[pos].tag += k;
        tree[pos].sum += k*(rp-lp+1);
        return;
    }
    pushdown(pos,rp-lp+1);
    int m = lp+rp >>1;
    if(l<=m) update(pos<<1,lp,m,l,r,k);
    if(r>m) update(pos<<1|1,m+1,rp,l,r,k);
    tree[pos].sum = (tree[pos<<1].sum+tree[pos<<1|1].sum)%p;
}
inline int query_2(int x,int y){
    int ans = 0;
    while(tp[x]!=tp[y]){
        if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
        res = 0;
        query(1,1,n,id[tp[x]],id[x]);
        ans += res;
        ans %= p;
        x = fa[tp[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    res = 0;
    query(1,1,n,id[x],id[y]);
    ans += res;
    return ans%p;
}
inline void update_1(int x,int y,int k){
    k %= p;
    while(tp[x]!=tp[y]){
        if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
        update(1,1,n,id[tp[x]],id[x],k);
        x = fa[tp[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    update(1,1,n,id[x],id[y],k);
}
inline int query_4(int x){
    res = 0;
    query(1,1,n,id[x],id[x]+sz[x]-1);
    return res;
}
inline void update_3(int x,int k){
    update(1,1,n,id[x],id[x]+sz[x]-1,k);
}
inline void dfs1(int x,int f,int d){
    dep[x] = d,fa[x] = f;
    sz[x] = 1;
    int maxson = -1;
    for(int i = to[x]; i;i = e[i].nxt){
        int y = e[i].y;
        if(y==f) continue;
        dfs1(y,x,d+1);
        sz[x] += sz[y];
        if(sz[y]>maxson) son[x] = y,maxson = sz[y];
    }
}
inline void dfs2(int x,int topf){
    id[x] = ++cnt;
    link[cnt] = val[x];
    tp[x] = topf;
    if(!son[x]) return;
    dfs2(son[x],topf);
    for(int i = to[x]; i;i = e[i].nxt){
        int y = e[i].y;
        if(y==fa[x]||y==son[x]) continue;
        dfs2(y,y);
    }
}
int main(){
    n = read(),m = read(),root = read(),p = read();
    rep(i,1,n) val[i] = read();
    rep(i,1,n-1){
        int x = read(),y = read();
        add(x,y),add(y,x);
    }    
    dfs1(root,0,1);
    dfs2(root,root);
    build(1,1,n);
    while(m--){
        int opt,x,y,z;
        opt = read();
        if(opt==1){
            x = read(),y = read(),z = read();
            update_1(x,y,z);
        }else if(opt==2){
            x = read(),y = read();
            printf("%d\n",query_2(x,y));
        }else if(opt==3){
            x = read(),y = read();
            update_3(x,y);
        }else{
            x = read();
            printf("%d\n",query_4(x));
        }
    }
}
View Code

 某谷最大流模板

#include<bits/stdc++.h>
using namespace std;
#define File ""
#define inf 1<<29
#define ll long long
#define ull unsigned long long
#define rep(k,i,j) for(int k = i;k <= j; ++k)
#define FOR(k,i,j) for(int k = i;k >= j; --k)
inline void file(){
    freopen(File".in","r",stdin);
    freopen(File".out","w",stdout);
}
inline int read(){
    int x=0,f=1;   char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
    return x*f;
}
const int mxn = 1e4+5;
const int mxm = 1e5+5;
struct edge{int nxt,y,v;}e[mxm<<1];
int to[mxn],len = 1;
inline void add(int xx,int yy,int zz){
    e[++len].nxt = to[xx];
    to[xx] = len;
    e[len].y = yy;
    e[len].v = zz;
    
    e[++len].nxt = to[yy];
    to[yy] = len;
    e[len].y = xx;
    e[len].v = 0;
}
int n,m,s,t,maxflow;
int incf[mxn],pre[mxn],v[mxn];
bool bfs(){
    memset(v,0,sizeof v);
    queue<int> q;
    q.push(s); v[s] = 1;
    incf[s] = inf;
    while(q.size()){
        int x = q.front(); q.pop();
        for(int i = to[x]; i;i = e[i].nxt){
            int y = e[i].y,z = e[i].v;
            if(z){
                if(v[y]) continue;
                incf[y] = min(incf[x],z);
                pre[y] = i;
                q.push(y),v[y] = 1;
                if(y==t) return 1;
            }
        }
    }
    return 0;
}
inline void update(){
    int x = t;
    while(x != s){
        int i = pre[x];
        e[i].v -= incf[t];
        e[i^1].v += incf[t];
        x = e[i^1].y;
    }
    maxflow += incf[t];
}
int main(){
//    file();
    n = read(),m = read(),s = read(),t = read();
    maxflow = 0;
    while(m--){
        int x = read(),y = read(),z = read();
        add(x,y,z);
    }
    while(bfs()) update();
    printf("%d\n",maxflow);
    return 0;
}
View Code

 某谷的线段树模板(1)

#include<bits/stdc++.h>
using namespace std;
#define File ""
#define ll long long
inline void file(){
    freopen(File".in","r",stdin);
    freopen(File".out","w",stdout);
}
inline ll read(){
    ll x=0,f=1;   char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
    return x*f;
}
const int mxn = 1e5+5;
int n,m;
struct T{
    int l,r;
    ll dat,tg;
}tr[mxn<<3];
#define ls p<<1
#define rs p<<1|1
ll a[mxn];
inline void build(int p,int l,int r){
    tr[p].l = l,tr[p].r = r;
    if(l==r){
        tr[p].dat = a[l];
        return;
    }
    int mid = l+r >>1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    tr[p].dat = tr[ls].dat + tr[rs].dat;
}
inline void push(int p){
    ll tg = tr[p].tg;
    if(tg){
        tr[ls].tg += tg;
        tr[rs].tg += tg;
        tr[ls].dat += tg*(tr[ls].r-tr[ls].l+1);
        tr[rs].dat += tg*(tr[rs].r-tr[rs].l+1);
        tr[p].tg = 0;
        return;
    }
}
inline void update(int p,int l,int r,int k){
    if(l <= tr[p].l && tr[p].r <= r){
        tr[p].tg += k;
        tr[p].dat += 1ll*(tr[p].r-tr[p].l +1)*k;
        return;
    }
    push(p);
    int mid = tr[p].l + tr[p].r >>1;
    if(l <= mid) update(ls,l,r,k);
    if(r > mid) update(rs,l,r,k);
    tr[p].dat = tr[ls].dat + tr[rs].dat;
}
inline ll ask(int p,int l,int r){
    ll ans(0);
    if(l <= tr[p].l && tr[p].r <= r) return tr[p].dat;
    push(p);
    int mid = tr[p].l + tr[p].r >>1;
    if(l <= mid) ans += ask(ls,l,r);
    if(r > mid) ans += ask(rs,l,r);
    return ans;
}
int main(){
//    file();
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n; ++i) a[i] = read();
    build(1,1,n);
    while(m--){
        int opt = read();
        if(opt==1){
            int x = read(),y = read(),k = read();
            update(1,x,y,k);
        }else{
            int x = read(),y = read();
            printf("%lld\n",ask(1,x,y));
        }
    }
    return 0;
}
/*
5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4
*/
View Code

某谷的线段树模板(2)

某谷的树状数组模板(1)

#include<bits/stdc++.h>
using namespace std;
#define File ""
#define ll long long
inline void file(){
    freopen(File".in","r",stdin);
    freopen(File".out","w",stdout);
}
inline ll read(){
    ll x=0,f=1;   char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
    return x*f;
}
const int mxn = 5e5+5;
int n,m;
ll c[mxn];
inline ll lowbit(int x){
    return x&-x;
}
inline void update(int x,ll d){
    while(x <= n)
        c[x] += d,x += lowbit(x);
}
inline ll ask(int x){
    ll ans(0);
    while(x)
        ans += c[x],x -= lowbit(x);
    return ans;
}
int main(){
//    file();
    n = read(),m = read();
    for(int i = 1;i <= n; ++i) update(i,read());
    while(m--){
        int opt = read();
        if(opt==1){
            int x = read(),k = read();
            update(x,k);
        }else{
            int x = read(),y = read();
            printf("%lld\n",ask(y)-ask(x-1));
        }
    }
    return 0;
}
/*
5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4
*/
View Code

某谷的树状数组模板(2)

 

转载于:https://www.cnblogs.com/ve-2021/p/10392362.html


http://www.niftyadmin.cn/n/681367.html

相关文章

ThinkPHP3.1在PHP7下页面空白的解决方案

ThinkPHP3.1在PHP7下页面空白的解决方案 浏览&#xff1a;2057 发布日期&#xff1a;2016/06/28 分类&#xff1a;技术分享先把BUG原因扔出来&#xff1a;模板解析出了问题。之前一直用PHP5.6做开发&#xff0c;听说过PHP出7了&#xff0c;不过一直没尝试。直到前两天&#xff…

编程随笔-ElasticSearch知识导图(3):映射

1. 啥是映射 ES中的映射(Mapping)实质上就是对文档对象结构的定义&#xff0c;也即对文档中各元素的描述。在ES中定义映射&#xff0c;就如同定义XML文档的XML Schema。  ES中的映射定义了文档模式&#xff08;就如同在关系数据库中定义了关系模式&#xff09;&#xff0c;文…

oneinstack一键包Nginx php多版本共存配置全过程

oneinstack一键包Nginx php多版本共存配置全过程 2016-01-17 12:39 3285人阅读 评论(0) 收藏 举报 分类&#xff1a; 服务器操作相关&#xff08;10&#xff09; 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 oneinstack一键包地址&#xff1a…

thinkphp3.2 I('get.id') 在 ningx代理apache下面错误 GET POST参数都变成_URL_ 解决方法 I函数

thinkphp3.2 I(get.id) 在 ningx代理apache下面错误 GET POST参数都变成_URL_ 解决方法 I函数/*** 获取输入参数 支持过滤和默认值* 使用方法:* <code>* I(id,0); 获取id参数 自动判断get或者post* I(post.name,,htmlspecialchars); 获取$_POST[name]* I(get.); 获取$_G…

UML--面向对象还是面向过程

为什么使用面向对象&#xff0c;面向对象和面向过程有差别在哪里呢&#xff1f;面向过程和面向对象有什么小故事呢&#xff1f; 面向过程&#xff1a; 面向过程&#xff0c;就像他的名字一样针对过程&#xff0c;它认为世界是一个个互相联系的小系统组成的&#xff0c;有着因果…

UML-面向对象

面向对象的特征&#xff0c;说一哈我知道的&#xff0c;哈哈哈 抽象 相似本质 抽象层次 什么是抽象层次呢&#xff0c;其实很简单&#xff0c;就相当于你在不同的层次上面进行分析 封装 啥是封装&#xff1f;举个例子&#xff0c;我们将对象看作一个盒子&#xff0c;除了与外…

http强制升级为https http头文件 Content Security Policy: 升级不安全的请求

http强制升级为https <meta http-equiv"Content-Security-Policy" content"upgrade-insecure-requests" />Content Security Policy: 升级不安全的请求“http://talk.f1host.cn/ApiEfl/getjsinfo?course_id325304”至使用“https”--------------…