hdu5861(Road)
题目链接:传送门
题目大意:有n个点 组成n-1段,每一段开着的时候都有花费Vi,有m组要求,对于每组要求 [x,y]之间可达,对于每一段你有一次开关的机会(最初都是关闭的)
问怎样安排段落得开闭时间使花费最小,输出每天的花费
题目思路:网上题解很多是线段树,但感觉不需要线段树,只需要统计每个点第一次出现的时间(开)和最后一次出现的时间(关)然后以时间为循环扫一遍即可。
于是自己先写了一发,全程用vector模拟,估计是插入和删除操作过多,以及vector本身速度慢,不幸TLE。实际上我模拟的操作也就是想对于每一次
询问,将未覆盖的点(之前还没出现过)标记为已覆盖,加入到相应vector中。而关键在于怎样快速的处理一段点是否被覆盖?
我是用vecotr保存 1~n然后只要当前被覆盖,将覆盖过的点删除,剩下没覆盖的点。而且每次询问的查找是用二分,应该很快了。结果还是Tle
第二种方法(参考网上大牛),用并查集。fp[i]表示 i~第一个未覆盖的点,这样对于询问来说,只需要常数级的操作就能判断更新出来。实在太高明了%%%
附上TLE代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 200005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL; int n,m,k,T;
int a[N];
vector<int>V[N],E[N],K,er;
struct Node{
int x,y;
}node[N];
int ans[N];
int main(){
//freopen("in.txt","r",stdin);
int i,j,x,y,v,l;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=;i<n;++i)scanf("%d",&a[i]);
K.clear();
for(i=;i<=n;++i)K.push_back(i);
for(i=;i<=m;++i){
V[i].clear();E[i].clear();
scanf("%d%d",&node[i].x,&node[i].y);
if(node[i].x>node[i].y){
node[i].x^=node[i].y^=node[i].x^=node[i].y;
}
node[i].y--;
}
vector<int>::iterator it;
for(i=;i<=m;++i){
int pos=lower_bound(K.begin(),K.end(),node[i].x)-K.begin();
it=K.begin()+pos;
int cnt=;
while(pos<K.size()&&node[i].y>=K[pos]){
V[i].push_back(K[pos++]);
++cnt;
}
for(;it!=K.end()&&cnt;--cnt){
K.erase(it);
}
}
K.clear();
for(i=;i<=n;++i)K.push_back(i);
for(i=m;i>=;--i){
int pos=lower_bound(K.begin(),K.end(),node[i].x)-K.begin();
it=K.begin()+pos;
int cnt=;
while(pos<K.size()&&node[i].y>=K[pos]){
E[i].push_back(K[pos++]);
++cnt;
}
for(;it!=K.end()&&cnt;--cnt)
K.erase(it);
}
int temp=;
for(i=;i<=m;++i){
for(int &u:V[i])
temp+=a[u];
for(int &u:E[i-])
temp-=a[u];
printf("%d\n",temp);
}
}
return ;
}
AC代码 1622ms
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 200005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL; int n,m,k,T;
int a[N];
vector<int>V[N],E[N],K,er;
struct Node{
int x,y;
}node[N];
int ans[N],fp[N];
int findp(int x){return fp[x]==x?x:fp[x]=findp(fp[x]);}
int main(){
//freopen("in.txt","r",stdin);
int i,j,x,y,v,l;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=;i<n;++i)scanf("%d",&a[i]);
for(i=;i<=m;++i){
V[i].clear();E[i].clear();
scanf("%d%d",&x,&y);
if(x>y)x^=y^=x^=y;
y--;
node[i].x=x;node[i].y=y;
}
for(i=;i<=n;++i)fp[i]=i;
for(i=;i<=m;++i){
while(true){
x=findp(node[i].x);
y=node[i].y;
if(x>y)break;
fp[x]=x+;
V[i].push_back(x);
}
}
for(i=;i<=n;++i)fp[i]=i;
for(i=m;i>=;--i){
while(true){
x=findp(node[i].x);
y=node[i].y;
if(x>y)break;
fp[x]=x+;
E[i].push_back(x);
}
}
int temp=;
for(i=;i<=m;++i){
for(int &u:V[i])temp+=a[u];
printf("%d\n",temp);
for(int &u:E[i])temp-=a[u];
}
}
return ;
}
hdu5861(Road)的更多相关文章
- 接口测试实例(Road)
		
以getObjectByCode接口为例,用jmeter2.13来进行接口测试. 测试前准备: 测试工具及版本:jmeter 2.13 r1665067(须包含__MD5函数) 示例接口:8.1根据单 ...
 - 【2019.7.24 NOIP模拟赛 T1】道路建设(road)(水题)
		
原题与此题 原题是一道神仙不可做题,两者区别在于,原题不能有重边和自环. 然而,这题可以有重边... 于是这题就变成了一道大水题. 此题的解法 考虑如何构造. 对于\(n\le10^4\)的情况: 对 ...
 - 洛谷P2242 公路维修问题(Road)
		
题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜. 有些牛棚里有牛 ...
 - 【Silverlight】Bing Maps学习系列(六):使用扩展模式(Extended Modes)(转)
		
[Silverlight]Bing Maps学习系列(六):使用扩展模式(Extended Modes) 微软Bing Maps推出有有段时间了,通过不断的改进和新的地图更新,现在已经基本上形成了一套 ...
 - 【Silverlight】Bing Maps学习系列(二):通过Bing Maps Silverlight Control如何显示地图(转)
		
[Silverlight]Bing Maps学习系列(二):通过Bing Maps Silverlight Control如何显示地图 如本系列第一篇你所介绍的,开发基于Silverlight的Bin ...
 - codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)
		
Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...
 - FPM一:简单的road map(GAF)
		
首先要有个简单的认识: 1.FPM支持的几种UI配置界面接口: Object Instance Floorplan (OIF) Overview Page Floorplan (OVP) Guided ...
 - Entity Framework 6 Recipes 2nd Edition(10-6)译 -> TPT继承模型中使用存储过程
		
10-6. TPT继承模型中使用存储过程 问题 想在一个TPT继承模型中使用存储过程 解决方案 假设已有如Figure 10-6所示模型. 在模型里, Magazine(杂志) and DVD继承于基 ...
 - Unbroken(坚不可摧)——Mateusz M
		
Unbroken(坚不可摧)——Mateusz M YouTube励志红人账号Mateusz M 的作品,短片由几位演讲家Les Brown.Eric Thomas.Steve Jobs.Louis ...
 
随机推荐
- Latex排版:CTeX winEdit 输出“系统找不到指定的文件”的解决办法)
			
winEdit输出“系统找不到指定的文件”,这里“指定的文件”是“TeXify.exe”等需要运行的程序,而不是当前需要编译的“.tex”文件.所以,问题的本质就是系统找不到“TeXify.exe”等 ...
 - xpath的基础实例
			
本文分为路径表达式和常用函数两部分,整理自火车浏览器官方教程-火车浏览器之Xpath讲解. 小提示:可以使用火狐浏览器.我用的是火狐浏览器+firebug+firepath来进行调试,调试界面是这样的 ...
 - c语言中有bool型变量吗?
			
C语言里面是没有bool(布尔)类型的,C++里面才有,这就是说,在C++里面使用bool类型是没有问题的. bool类型有只有两个值:true =1 .false=0. 但是,C99标准里面,又定义 ...
 - python 实现元组中的的数据按照list排序, python查询mysql得到的数据是元组格式,按照list格式对他们排序
			
需求: 需要用echart实现软件模块的统计分析,首先是对数据库的数据查询出来,然后给数据封装成列表(list)格式,数据传到前台,在echart实现绑定数据. 因为数据已经按照从大到小的顺序显示出来 ...
 - django源码分析---- Model类型&Field类型
			
djiango在数据库这方式自己实现了orm(object relationship mapping 对象关系模型映射).这个主要是用到python 元类这一 项python中的高级技术来实现的. c ...
 - bitnami-redmineserver迁移
			
1. 背景 在Redmineserver迁移过程中.假设前后两个Redmine的版本号一样,事情就简单,假设版本号不一样,就有可能面临两个版本号数据库不兼容.那就比較麻烦了.本文旨在介绍数据库不兼容时 ...
 - JSP应用开发        --------  电纸书(未完待续)
			
http://www.educity.cn/jiaocheng/j9415.html JSP程序员常用的技术 第1章 JSP及其相关技术导航 [本章专家知识导学] JSP是一种编程语言,也是一种动 ...
 - 点滴积累【JS】---JS小功能(JS实现多物体缓冲运动)
			
效果: 思路: 利用setInterval计时器进行运动,offsetWidth实现宽度的变动,在用onmouseover将终点和所选中的DIV放入参数再进行缓冲运动. 代码: <head ru ...
 - Atitit. Class  元数据的反射操作 api apache  工具
			
Atitit. Class 元数据的反射操作 api apache 工具 1 BeanUtils & PropertyUtils & MethodUtils类使用方法 - 短裤党 ...
 - Linux之IO Redirection
			
一.引言 前几天使用一个linux下的内存检测工具valgrind,想要把检测的结果重定向到文件,结果总是没有任何内容,最后才发现是重定向的原因,它输出的信息是输出到stderr的,所以我使用 > ...