ACM ICPC China final G Pandaria
ACM ICPC China final G Pandaria
题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\)代表点\(i\)的颜色,每条边有一个权值
现在有q个询问如下
u w代表从点u出发,每次只能走权值不超过w的边,经过的颜色中次数最多的是哪种颜色,若有多种,输出编号最小的。
$ 1<=n<=1e5$
$ 1<=m,q<=2e5$
$ 1<=c_i<=n$
$ 1<=w<=1e6$
ps:强制在线
思路: 把边从小到大排序,依次合并,现在就是如何维护每个连通块的答案了。
用主席树维护区间最大值和合并操作,查询就是二分最大值的最左位置
如果这题不强制在线的话,那么我们可以把查询按w从小大排序,模拟过去即可
强制在线就多了一些东西,考虑合并的过程,其实是一颗自底向上的树,越往上边权越大
前面主席树已经算出每个结点的答案,如何找到要查询的历史版本,通过pa数组自底向上倍增即可
#include<bits/stdc++.h>
#define P pair<int,int>
#define LL long long
#define ls(i) seg[i].lc
#define rs(i) seg[i].rc
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 1;
const int M = 2 * N;
const int maxn = 3 * N;
const int maxh = 25;
int n, m,tott;
int c[N];
int ans[maxn];
struct Tree{
int ls, rs, w;
}tree[maxn];
struct node{
int lc,rc,cnt;
}seg[maxn * maxh];///主席树维护的区间的最大值,二分查询最大值所在的最左位置
int root[maxn * maxh];
struct Edge{
int u, v, w;
Edge(){};
bool operator<(const Edge&rhs)const{
return w < rhs.w;
}
}e[M];
void pushup(int rt){
seg[rt].cnt = max(seg[ls(rt)].cnt,seg[rs(rt)].cnt);
}
void update(int &rt,int l,int r,int pos,int val){
seg[++tott] = seg[rt];
rt = tott;
if(l == r){
seg[rt].cnt += val;
return ;
}
int m = (l + r) >> 1;
if(pos <= m) update(ls(rt),l,m,pos,val);
else update(rs(rt),m+1,r,pos,val);
pushup(rt);
}
int query(int rt,int l,int r){
if(l == r) return l;
int m = (l + r) >> 1;
if(seg[ls(rt)].cnt >= seg[rs(rt)].cnt) return query(ls(rt),l,m);
return query(rs(rt),m+1,r);
}
int Merge(int rt1,int rt2,int l,int r){
if(!rt1 || !rt2) return rt1 + rt2;///最多只有n个点保证了合并的复杂度科学
if(l == r){
seg[rt1].cnt += seg[rt2].cnt;
return rt1;
}
int m = l + r >> 1;
ls(rt1) = Merge(ls(rt1),ls(rt2),l,m);
rs(rt1) = Merge(rs(rt1),rs(rt2),m+1,r);
pushup(rt1);
return rt1;
}
int pa[maxn][maxh];
int Find(int x){
return pa[x][0] == x?x:pa[x][0] = Find(pa[x][0]);
}
int ask(int u,int w){ ///倍增找接近的历史版本的答案
for(int i = maxh - 1;i >= 0;i--) if(pa[u][i] && tree[pa[u][i]].w <= w) u = pa[u][i];
return ans[u];
}
int main(){
int T, cas = 1;
cin>>T;
while(T--){
scanf("%d%d",&n,&m);
tott = 0;
for(int i = 1;i <= n;i++) {
scanf("%d",c + i);
root[i] = root[0];
update(root[i],1,n,c[i],1);
pa[i][0] = i;
tree[i].w = 0; ///叶子结点权值为0
ans[i] = query(root[i],1,n);
}
for(int i = 0;i < m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
int tot = n;
sort(e, e + m);
for(int i = 0;i < m;i++){
int u = Find(e[i].u), v = Find(e[i].v), w = e[i].w;
if(u == v) continue;
tree[++tot].w = w;///合并得到新的父结点
tree[tot].ls = u, tree[tot].rs = v;///两个孩子
pa[tot][0] = pa[u][0] = pa[v][0] = tot;
root[tot] = Merge(root[u],root[v],1,n);
ans[tot] = query(root[tot],1,n);
}
for(int i = n + 1;i <= tot;i++) pa[tree[i].ls][0] = pa[tree[i].rs][0] = i;///前面为路径压缩的用途,现在改为上一级祖先用途
for(int i = 1;i <= tot;i++) pa[i][0] = pa[i][0] == i?0:pa[i][0];
for(int i = 1;i < maxh;i++)
for(int j = 1;j <= tot;j++) pa[j][i] = pa[pa[j][i-1]][i-1];
int q, last = 0, u, w;
printf("Case #%d:\n",cas++);
scanf("%d",&q);
while(q--){
scanf("%d%d",&u,&w);
u ^= last, w ^= last;
printf("%d\n",last = ask(u,w));
}
}
return 0;
}
ACM ICPC China final G Pandaria的更多相关文章
- 洪水!(Flooded! ACM/ICPC World Final 1999,UVa815)
题目描述:竞赛入门经典的习题4-10 解题思路:1.把各个网格想象成一个数组 2.排序 3.雨水总体积去铺满 //太懒了只写了求海拔 #include <stdio.h> #define ...
- 【转】lonekight@xmu·ACM/ICPC 回忆录
转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering
Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...
- 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2017 ACM/ICPC Shenyang Online SPFA+无向图最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time
Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...
- 【转】ACM/ICPC生涯总结暨退役宣言—alpc55
转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...
- hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- POJ的层次感分类
转载自:[http://blog.csdn.net/zzycsx/article/details/49103451] OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,po ...
- 转:比较spring cloud和dubbo,各自的优缺点是什么
原文:https://blog.csdn.net/u010664947/article/details/80007767 dubbo由于是二进制的传输,占用带宽会更少 springCloud是http ...
- 详解MessageBox(),MsgBox函数的正确使用
//或者使用chr(13),chr(10)效果一样 MsgBox "a"&chr(13)&"b"&chr(10)&"c ...
- linux下csv导出文件中文乱码问题
近日在服务器端通过导出csv文件,将数据从linux服务器端保存到windows桌面端,以便用户可以通过excel打开使用数据. 但是在使用excel打开csv文件时,出现了中文乱码的情况,但是使用记 ...
- Ball CodeForces - 12D
传送门 N ladies attend the ball in the King's palace. Every lady can be described with three values: be ...
- 笔记-python-standard library-16.3 time
笔记-python-standard library-16.3 time 1. time 1.1. 开始 time模块中时间表现的格式主要有三种: timestamp时间戳,时间戳表示 ...
- spark基于win上面的操作
自己前面的小练习一直都是在linux上面写的,可是最近由于要把他迁移到win上面,我在自己的csdn博客有对如何在win上面搭建spark环境做出说明,好了,我们还是先看看 今天的内容吧 1.假如你有 ...
- SpringMVC---其它常用注解
常用注解 PathVariable @RequestMapping注解中使用占位符的情况下,需要使用@PathVariable注解指定占位符参数.即指定占位符中的值与方法中哪一个参数进行匹配.如果方法 ...
- Spring---资源访问工具类
JDK所提供的访问资源的类并不能很好的满足各种底层资源的访问需求,因此,Spring设计了一个Resource接口,它为应用提供了更强大的访问底层资源的能力 主要方法 boolean exists() ...
- android singleTop 不起作用
今天,排查问题,发现设置了singleTop 的activity, 多次启动依然是多个acitivity,而不是一个. 明明在清单里面设置了,但是就是启动了多个. 可能是因为启动的太快,导致系统判断有 ...