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] ...
随机推荐
- 如何用sysbench做好IO性能测试
sysbench 是一个非常经典的综合性能测试工具,通常都用它来做数据库的性能压测,但也可以用来做CPU,IO的性能测试.而对于IO测试,不是很推荐sysbench,倒不是说它有错误,工具本身没有任何 ...
- 组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)
组合模式(合成模式 COMPOSITE) 意图 将对象组合成树形结构以表示“部分-整体”的层次结构. Composite使得用户对单个对象和组合对象的使用具有一致性. 树形结构介绍 为了便于理解, ...
- Zuul上实现限流(spring-cloud-zuul-ratelimit)
简述 Spring Cloud Zuul RateLimit项目Github地址: https://github.com/marcosbarbero/spring-cloud-zuul-ratelim ...
- [java]static关键字的四种用法
在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们先来了解一下stat ...
- webpack4.0各个击破(6)—— Loader篇
webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决方式的模式,以前端构建中遇到的具体需求为出发点,学习we ...
- c#调用com组件,程序 发生意外<hr=0x80020009>
引用dll,确认dll没有问题,版本正确,可是一直报发生意外,没有任何其他提示. 解决方案: 看dll引用选项配置 复制到本地:设为true,我的就是false; 嵌入互操作类型:false,如果是t ...
- vue 常用语法糖
//来自 https://www.cnblogs.com/lhl66/p/8021730.html 侵删 el:element 需要获取的元素,一定是HTML中的根容器元素 data:用于数据的存储 ...
- 谷歌AI涉足艺术、太空、外科手术,再强调AI七原则
谷歌AI涉足艺术.太空.外科手术,再强调AI七原则 https://mp.weixin.qq.com/s/MJG_SvKCEBKRvL3IWpL0bA 9月18日上午,Google在上海的2018世界 ...
- mybatis中:selectKey返回最近插入记录的id
<insert id="insert" parameterType="com.lls.model.Employee"> <!-- select ...
- 注册Github过程
第一步当然是建立自己的账号密码了: 一: github官网地址:https://github.com/ (1)第一步:首先起一个属于自己用户的名字(username),用户名字只能包含字母数字的字符或 ...