题解:

题解居然是LCT……受教了

把所有区间按照端点排序,动态维护目前有重叠的区间,用LCT维护即可。

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
int son[],fa,sz1,sz2,rev;
}t[];
struct task{
int op,u,v,x;
task(int _op=,int _u=,int _v=,int _x=){
op=_op,u=_u,v=_v,x=_x;
}
friend bool operator <(task a,task b){
return a.x==b.x?a.op<b.op:a.x<b.x;
}
}q[];
int n,u,v,l,r,cnt=,top,s[];
ll ans;
int lr(int u){
return t[t[u].fa].son[]==u;
}
int ntrt(int u){
return t[t[u].fa].son[]==u||t[t[u].fa].son[]==u;
}
void pushup(int u){
t[u].sz1=t[t[u].son[]].sz1+t[t[u].son[]].sz1+t[u].sz2+;
}
void getrev(int u){
swap(t[u].son[],t[u].son[]);
t[u].rev^=;
}
void pd(int u){
if(t[u].rev){
if(t[u].son[])getrev(t[u].son[]);
if(t[u].son[])getrev(t[u].son[]);
t[u].rev=;
}
}
void rotate(int u){
int f=t[u].fa,ff=t[f].fa,ch=lr(u);
if(ntrt(f))t[ff].son[t[ff].son[]==f]=u;
t[f].son[ch]=t[u].son[ch^];
t[t[f].son[ch]].fa=f;
t[u].son[ch^]=f;
t[f].fa=u;
t[u].fa=ff;
pushup(f);
pushup(u);
}
void splay(int u){
int now=u;
s[top=]=now;
while(ntrt(now))s[++top]=now=t[now].fa;
while(top)pd(s[top--]);
while(ntrt(u)){
int f=t[u].fa,ff=t[f].fa;
if(ntrt(f)){
rotate(lr(u)==lr(f)?f:u);
}
rotate(u);
}
}
void access(int u){
for(int now=;u;now=u,u=t[u].fa){
splay(u);
t[u].sz2+=t[t[u].son[]].sz1-t[now].sz1;
t[u].son[]=now;
pushup(u);
}
}
void makert(int u){
access(u);
splay(u);
getrev(u);
}
void link(int x,int y){
makert(x);
makert(y);
ans+=(ll)t[x].sz1*t[y].sz1;
t[y].fa=x;
t[x].sz2+=t[y].sz1;
pushup(x);
}
void cut(int x,int y){
makert(x);
access(y);
splay(y);
t[x].fa=t[y].son[]=;
pushup(y);
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d%d%d",&u,&v,&l,&r);
q[++cnt]=(task){,u,v,l};
q[++cnt]=(task){,u,v,r};
}
sort(q+,q+cnt+);
for(int i=;i<=cnt;i++){
if(!q[i].op)link(q[i].u,q[i].v);
else cut(q[i].u,q[i].v);
}
printf("%lld",ans);
return ;
}

最新文章

  1. Angular中的Ajax
  2. mybatis报invalue types()错误
  3. 洛谷P3371 【模板】单源最短路径
  4. slidingMenu有时候需要关闭侧边栏
  5. php一些常用函数的理解
  6. JS学习笔记(一) 概述
  7. 如何使用WCF调试器WcfTestClient.exe
  8. Linux的文件/目录的权限
  9. 关于windows phone 8.1系统手机对html5触摸事件的支持情况
  10. Spring Boot:简介
  11. appium常用方法
  12. 引用的作用&amp;引用与指针的区别
  13. Windows平台下nginx跨域配置
  14. 《DSP using MATLAB》Problem 6.14
  15. websocket python实现原理
  16. 【Cuda编程】加法归约
  17. UISearchBar和UISearchDisplayController
  18. webpack打包jquery并引用
  19. [转]C# 6.0 的新特性
  20. 基于Vue的WebApp项目开发(三)

热门文章

  1. EntityFramework 二
  2. (2)RDD的基本操作
  3. java JSON 和 Object 相互转换
  4. Spring IoC简介及使用
  5. 搭建ELK日志分析平台(上)—— ELK介绍及搭建 Elasticsearch 分布式集群
  6. ASP.NET-服务器客户端的信息保持
  7. spring boot基础
  8. java语言MySQL批处理
  9. iOS开发自己定义键盘回车键Return Key
  10. Android实战简易教程-第十三枪(五大布局研究)