思路:

暴力建图有n*m条边

考虑怎么优化

(那就只能加个线段树了呗)

然后我就不会写了.....

抄了一波题解

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=,M=N*,inf=0x3f3f3f3f;
vector<int>vec[N];
int n,m,first[M],next[M],v[M],w[M],tot,cnt=,S=,T=;
int lson[M],rson[M],root[N],jy,vis[M];
int read(){
char p=getchar();int x=;
while(p<''||p>'')p=getchar();
while(p>=''&&p<='')x=x*+p-'',p=getchar();
return x;
}
void Add(int x,int y,int z){v[tot]=y,w[tot]=z,next[tot]=first[x],first[x]=tot++;}
void add(int x,int y,int z){Add(x,y,z),Add(y,x,);}
int insert(int l,int r,int pos,int num){
pos=++cnt;
if(l==r){add(cnt,T,);return pos;}
int mid=(l+r)>>;
if(mid<num)add(pos,rson[pos]=insert(mid+,r,rson[pos],num),inf);
else add(pos,lson[pos]=insert(l,mid,lson[pos],num),inf);
return pos;
}
int merge(int l,int r,int pos,int last){
if(pos*last==)return pos+last;
int now=++cnt,mid=(l+r)>>;
if(l==r){add(now,pos,inf),add(now,last,inf);return now;}
add(now,lson[now]=merge(l,mid,lson[pos],lson[last]),inf);
add(now,rson[now]=merge(mid+,r,rson[pos],rson[last]),inf);
return now;
}
void dfs(int x){
for(int i=;i<vec[x].size();i++)
dfs(vec[x][i]),root[x]=merge(,n,root[x],root[vec[x][i]]);
}
void query(int l,int r,int pos,int L,int R){
if(!pos)return;
if(l>=L&&r<=R){add(cnt,pos,inf);return;}
int mid=(l+r)>>;
if(mid<L)query(mid+,r,rson[pos],L,R);
else if(mid>=R)query(l,mid,lson[pos],L,R);
else query(l,mid,lson[pos],L,R),query(mid+,r,rson[pos],L,R);
}
bool tell(){
memset(vis,-,sizeof(vis));
queue<int>q;q.push(S),vis[S]=;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=first[t];~i;i=next[i])
if(vis[v[i]]==-&&w[i])vis[v[i]]=vis[t]+,q.push(v[i]);
}return ~vis[T];
}
int zeng(int x,int y){
if(x==T)return y;
int r=;
for(int i=first[x];~i;i=next[i])if(vis[v[i]]==vis[x]+&&w[i]){
int t=zeng(v[i],min(w[i],y-r));
w[i]-=t,w[i^]+=t,r+=t;
}if(!r)vis[x]=-;
return r;
}
int flow(){int ans=;while(tell())while(jy=zeng(S,inf))ans+=jy;return ans;}
int main(){
memset(first,-,sizeof(first));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)vec[read()].push_back(i);
for(int i=;i<=n;i++)root[i]=insert(,n,root[i],read());
dfs();
for(int i=;i<=m;i++){
int l=read(),r=read(),x=read(),y=read();
cnt++,add(S,cnt,y),query(,n,root[x],l,r);
}
printf("%d\n",flow());
}

BZOJ 3681 线段树合并+网络流的更多相关文章

  1. BZOJ 4756 线段树合并(线段树)

    思路: 1.最裸的线段树合并 2. 我们可以观察到子树求一个东西 那我们直接DFS序好了 入队的时候统计一下有多少比他大的 出的时候统计一下 减一下 搞定~ 线段树合并代码: //By SiriusR ...

  2. 【BZOJ-3681】Arietta 网络流 + 线段树合并

    3681: Arietta Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 182  Solved: 70[Submit][Status][Discuss ...

  3. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  4. BZOJ.4399.魔法少女LJJ(线段树合并)

    BZOJ 注意\(c\leq7\)→_→ 然后就是裸的权值线段树+线段树合并了. 对于取\(\max/\min\)操作可以直接区间修改清空超出范围的值,然后更新到对应位置上就行了(比如对\(v\)取\ ...

  5. BZOJ.5461.[PKUWC2018]Minimax(DP 线段树合并)

    BZOJ LOJ 令\(f[i][j]\)表示以\(i\)为根的子树,权值\(j\)作为根节点的概率. 设\(i\)的两棵子树分别为\(x,y\),记\(p_a\)表示\(f[x][a]\),\(p_ ...

  6. BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)

    BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...

  7. BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)

    BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...

  8. BZOJ.5417.[NOI2018]你的名字(后缀自动机 线段树合并)

    LOJ 洛谷 BZOJ 考虑\(l=1,r=|S|\)的情况: 对\(S\)串建SAM,\(T\)在上面匹配,可以得到每个位置\(i\)的后缀的最长匹配长度\(mx[i]\). 因为要去重,对\(T\ ...

  9. BZOJ 3277 串 & BZOJ 3473 字符串 (广义后缀自动机、时间复杂度分析、启发式合并、线段树合并、主席树)

    标签那么长是因为做法太多了... 题目链接: (bzoj 3277) https://www.lydsy.com/JudgeOnline/problem.php?id=3277 (bzoj 3473) ...

随机推荐

  1. Map 键值对 set get delete

  2. C#学习笔记_11_方法的隐藏和重写

    11_方法的隐藏和重写 方法的隐藏 需要使用到关键字:new 方法的重写 虚函数: 使用关键字virtual修饰的函数 虚函数可以被子类隐藏,也可以被子类重写 非虚函数只能被子类隐藏 关键字:over ...

  3. BUPT2017 springtraining(16) #6 ——图论

    题目链接 A.容易发现最后字符的对应都是一对一的 或者说我们没办法出现最后多对一或者一对多的情况 所以只要算出 ‘a’ - 'z' 每个字符最后对应的字符即可 #include <cstdio& ...

  4. Hadoop2.2.0 注意事项

    1.启动前必须把防火墙关了,要不然会导致nodemanager启动不了. 关闭防火墙:service iptables stop 永久关闭(重启后默认关闭):chkconfig iptables of ...

  5. 51Nod——T 1113 矩阵快速幂

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 ...

  6. Spring MVC-集成(Integration)-Hibernate验证器示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_hibernate_validator.htm 说明:示例基于Spring MVC ...

  7. HDU 4532

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  8. 【oracle 11G Grid 】Crsctl start cluster 和 crsctl start crs 有差别么?

     [oracle 11G Grid ]Crsctl start cluster 和 crsctl start crs 有差别么? q:Crsctl start cluster 是 11.2新特性和 ...

  9. hdu3371 Connect the Cities (MST)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. 数据结构(C实现)------- 顺序栈

    栈是限定仅在表的一端进行插入或删除的纯属表,通常称同意插入.删除的一端为栈顶(Top),对应在的.则称还有一端为栈底(Bottom). 不含元素的栈则称为空栈. 所设栈S={a1,a2,a3,..., ...