LOJ2831 JOISC2018 道路建设 LCT、树状数组
题目的操作大概是:求某个点到根的链的逆序对,然后对这条链做区间赋值
求某个点到根的链,就是LCT中的access操作,所以我们每一次把access过后的链打上标记,就可以做到区间赋值了。
计算答案只需要在access的过程中用树状数组求一下逆序对即可。
#include<bits/stdc++.h>
#define PII pair < int , int >
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return f ? -a : a;
}
const int MAXN = 1e5 + 7;
struct node{
int fa , ch[2] , sz;
bool mark;
}Tree[MAXN];
int N , cnt , val[MAXN] , lsh[MAXN];
long long ans;
queue < PII > q;
namespace BIT{
#define lowbit(x) ((x) & -(x))
int BIT[MAXN];
inline void add(int x , int num){
while(x <= cnt){
BIT[x] += num;
x += lowbit(x);
}
}
inline int get(int x){
int sum = 0;
while(x){
sum += BIT[x];
x -= lowbit(x);
}
return sum;
}
}
using namespace BIT;
inline bool nroot(int x){
return Tree[Tree[x].fa].ch[0] == x || Tree[Tree[x].fa].ch[1] == x;
}
inline bool son(int x){
return Tree[Tree[x].fa].ch[1] == x;
}
inline void pushup(int x){
Tree[x].sz = Tree[Tree[x].ch[0]].sz + Tree[Tree[x].ch[1]].sz + 1;
}
inline void rotate(int x){
bool f = son(x);
int y = Tree[x].fa , z = Tree[y].fa , w = Tree[x].ch[f ^ 1];
Tree[x].fa = z;
if(nroot(y))
Tree[z].ch[son(y)] = x;
Tree[y].fa = x;
Tree[x].ch[f ^ 1] = y;
Tree[y].ch[f] = w;
if(w)
Tree[w].fa = y;
pushup(y);
}
inline void mark(int x){
Tree[x].mark ^= 1;
swap(Tree[x].ch[0] , Tree[x].ch[1]);
}
inline void pushdown(int x){
if(Tree[x].mark){
mark(Tree[x].ch[0]);
mark(Tree[x].ch[1]);
Tree[x].mark = 0;
}
}
void pushdown_all(int x){
if(nroot(x))
pushdown_all(Tree[x].fa);
pushdown(x);
}
inline void Splay(int x){
pushdown_all(x);
while(nroot(x)){
if(nroot(Tree[x].fa))
rotate(son(x) == son(Tree[x].fa) ? Tree[x].fa : x);
rotate(x);
}
pushup(x);
}
inline void access(int x){
int y = x;
x = Tree[x].fa;
for( ; x ; y = x , x = Tree[x].fa){
Splay(x);
int t = x;
while(Tree[t].ch[1])
pushdown(t = Tree[t].ch[1]);
q.push(PII(val[t] , Tree[Tree[x].ch[0]].sz + 1));
ans += 1ll * get(val[t] - 1) * (Tree[Tree[x].ch[0]].sz + 1);
add(val[t] , Tree[Tree[x].ch[0]].sz + 1);
Tree[x].ch[1] = y;
pushup(x);
}
}
inline void clear(){
while(!q.empty()){
PII t = q.front();
q.pop();
add(t.first , -t.second);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
N = read();
for(int i = 1 ; i <= N ; ++i)
val[i] = lsh[i] = read();
sort(lsh + 1 , lsh + N + 1);
cnt = unique(lsh + 1 , lsh + N + 1) - lsh - 1;
for(int i = 1 ; i <= N ; ++i)
val[i] = lower_bound(lsh + 1 , lsh + cnt + 1 , val[i]) - lsh;
for(int i = 1 ; i < N ; ++i){
int a = read() , b = read();
ans = 0;
Tree[b].fa = a;
access(b);
Splay(b);
cout << ans << '\n';
clear();
}
return 0;
}
LOJ2831 JOISC2018 道路建设 LCT、树状数组的更多相关文章
- [JOISC2018]道路建设 LCT
[JOISC2018]道路建设 LOJ传送门 考的时候打的大暴力,其实想到了LCT,但是思路有点没转过来.就算想到了估计也不能切,我没有在考场写LCT的自信... 其实这题不是让你直接用LCT维护答案 ...
- bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...
- [Codeforces1137F]Matches Are Not a Child's Play——LCT+树状数组
题目链接: [Codeforces1137F]Matches Are Not a Child's Play 题目大意: 我们定义一棵树的删除序列为:每一次将树中编号最小的叶子删掉,将该节点编号加入到当 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- 【BZOJ2870】最长道路tree 点分治+树状数组
[BZOJ2870]最长道路tree Description H城很大,有N个路口(从1到N编号),路口之间有N-1边,使得任意两个路口都能互相到达,这些道路的长度我们视作一样.每个路口都有很多车辆来 ...
- 【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询
题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作 ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- 【BZOJ-1103】大都市meg 树状数组 + DFS序
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2009 Solved: 1056[Submit][Sta ...
- 【BZOJ-3648】寝室管理 环套树 + 树状数组 + 点分治
3648: 寝室管理 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 239 Solved: 106[Submit][Status][Discuss] ...
随机推荐
- PE知识复习之PE新增节
PE知识复习之PE新增节 一丶为什么新增节.以及新增节的步骤 例如前几讲.我们的PE文件在空白区可以添加代码.但是这样是由一个弊端的.因为你的空白区节属性可能是只读的不能执行.如果你修改了属性.那么程 ...
- 罗汉果与Java虚拟机系列目录与说明
声 明 罗汉果与Java虚拟机系列博文仅为本银结构性整合Java虚拟机知识的笔记和日常JVM问题的DEBUG记录.放到网上主要是为了方便自己今后查看.顺带能帮助到别人就更奈斯了. 目 录 ...
- Javascript继承3:将优点为我所有----组合式继承
//声明父类 function ParentClass(name){ //值类型公有属性 this.name = name //引用类型公有属性 this.books = ['Html'] } //父 ...
- TSP(Traveling Salesman Problem)-----浅谈旅行商问题(动态规划,回溯实现)
1.什么是TSP问题 一个售货员必须访问n个城市,这n个城市是一个完全图,售货员需要恰好访问所有城市的一次,并且回到最终的城市. 城市于城市之间有一个旅行费用,售货员希望旅行费用之和最少. 完全图:完 ...
- #WEB安全基础 : HTML/CSS | 0x5a标签拓展和class、id属性的使用
a标签不只是能链接到其他网页,也能链接到网页中的元素 class属性让你用CSS对特定的元素进行修饰 这些是一个网页设计者的有力武器 这节课还是一个index.html文件 以下是代码 <h ...
- Vs2017 xaramin mac build agent部署后记
换了四种黑苹果,最终成功了 步骤: 1.升级vs2017, 2.安装XCODE 8.3 3.安装vs2017 for mac 企业版 4 ...
- vue.js的手脚架vue-cli项目搭建的步骤
手脚架是什么? 众所周知,现在的前端项目发展得越渐越大,我们前端程序员要从0开始去搭建一套完整的项目很费时,所以这时候前端工程的手脚架就出现了. 我用得vue-cli也是其中之一,还有其他的我也说不清 ...
- 转:sql server锁知识及锁应用
sql server锁(lock)知识及锁应用 提示:这里所摘抄的关于锁的知识有的是不同sql server版本的,对应于特定版本时会有问题. 一 关于锁的基础知识 (一). 为什么要引入锁 当多个用 ...
- Json对象遍历
var json = {"id":"123","name":"tom","sex":"ma ...
- python3 文件操作
步骤:打开文件->操作文件->关闭文件 打开文件 文件句柄 = open('文件路径', '模式') 指定文件编码 文件句柄= open('文件路径','模式',encoding='utf ...