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的更多相关文章

  1. 洪水!(Flooded! ACM/ICPC World Final 1999,UVa815)

    题目描述:竞赛入门经典的习题4-10 解题思路:1.把各个网格想象成一个数组 2.排序 3.雨水总体积去铺满 //太懒了只写了求海拔 #include <stdio.h> #define ...

  2. 【转】lonekight@xmu·ACM/ICPC 回忆录

    转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...

  3. 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 ...

  4. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  5. 2017 ACM/ICPC Shenyang Online SPFA+无向图最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  6. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  7. 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 ...

  8. 【转】ACM/ICPC生涯总结暨退役宣言—alpc55

    转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...

  9. hduoj 4706 Children&#39;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) ...

随机推荐

  1. 创建自己的网站博客--Hexo

    原文地址:https://www.xingkongbj.com/blog/hexo/creat-hexo.html 安装环境 安装 node 下载对应版本并安装 node . 安装 Git Windo ...

  2. CI的子目录控制器问题

    不管是根目录还是子目录里面的文件名必须是首字母大写,否则会报404

  3. linux定时任务及练习

    第1章 定时任务 1.1 什么是定时任务 相当于闹钟每天叫你起床 设定一个时间去做某件事 1.2 系统定时任务 [root@zeq ~]# ll -d /etc/cron* drwxr-xr-x. 2 ...

  4. Linux常见文件管理命令

    1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示.(1)目录管理命令——ls:列出指定目录下的内容格式:ls [OPTION]... [FILE]... -a:显示所有文件包 ...

  5. 关于Pycharm基本操作笔记

    创建 project(工程,译音:破拽科特) 1.Create New project(创建一个新的工程,译音:科瑞特 纽 破摘科特) 2.pure python(纯派森,译音:皮忧儿 派森) 3.l ...

  6. 【转载】谈MongoDB的应用场景

    引用:http://blog.csdn.net/adparking/article/details/38727911 MongoDB的应用场景在网上搜索了下,很少介绍关于传统的信息化应用中如何使用Mo ...

  7. CentOS yum命令报错 Error: File /var/cache/yum/i386/6/epel/metalink.xml does not exist

    最近在虚拟机上执行yum命令一直报错:Could not parse metalink https://mirrors.fedoraproject.org/metalink?repo=epel-7&a ...

  8. 37-生成 JWT Token

    接到上篇文章 安装扩展插件nuget package方法安装包 使用 ctrl+shift+p打开命令面板 增加这个包,  Microsoft.AspNetCore.Authentication.Jw ...

  9. debug注意事项

    1 先看关键代码是否正确,然后查一遍是否有变量名打错. 2 再看初始化有没有问题 3 再把范围开大和开int64(这应该刚开始看题就要注意,在不爆内存的情况下开int64) 4 静态调试,输出中间值. ...

  10. 从Wireshark看TCP连接的建立与关闭

    TCP是一种面向连接.可靠的协议.TCP连接的建立与断开,都是需要经过通信双方的协商.用一句话概括就是:三次握手say hello(建立连接):四次握手say goodbye(断开连接).要了解TCP ...