hdu 5861 Road 两棵线段树
传送门:hdu 5861 Road
题意:
- 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi
- 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路
- 每条路只能开放或关闭一次. (不能重复开关)
- 求每天的最小花费.
思路:
- 第一次线段树:维护每条路第一次和最后一次被用到的天数.以下代码维护了 mn:第一次被用到,mx:最后一次被用到,lazy:被更新的最大值
若当前区间被lazy维护而没有更新到点,那么这个子节点的最小值就可能被改变.所以我这里的子节点更新是根据父节点的最大和最小来更新,因为没有pushup操作,所以父节点记录的就是这个区间的最大和最小值。而lazy的作用仅是为了判断是否需要更新子节点,以减少更新操作 - 第二次线段树:维护每天用了多少钱.
根据以上结果维护,对于每条路(被用到过的)区间更新第一次到最后一次这些天用的钱.
/**************************************************************
Problem:hdu 5861 Road
User: youmi
Language: C++
Result: Accepted
Time:2714MS
Memory:23936K
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <cmath>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scs(a) scanf("%s",a)
#define sclld(a) scanf("%I64d",&a)
#define pt(a) printf("%d\n",a)
#define ptlld(a) printf("%I64d\n",a)
#define rep(i,from,to) for(int i=from;i<=to;i++)
#define irep(i,to,from) for(int i=to;i>=from;i--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define lson (step<<1)
#define rson (lson+1)
#define eps 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl
const double pi=*atan(1.0); using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template <class T> inline void read(T &n)
{
char c; int flag = ;
for (c = getchar(); !(c >= '' && c <= '' || c == '-'); c = getchar()); if (c == '-') flag = -, n = ; else n = c - '';
for (c = getchar(); c >= '' && c <= ''; c = getchar()) n = n * + c - ''; n *= flag;
}
ll Pow(ll base, ll n, ll mo)
{
if (n == ) return ;
if (n == ) return base % mo;
ll tmp = Pow(base, n >> , mo);
tmp = (ll)tmp * tmp % mo;
if (n & ) tmp = (ll)tmp * base % mo;
return tmp;
}
//*************************** int n,m;
const int maxn=+;
const ll mod=;
pii p[maxn];
int a[maxn];
ll ans[maxn];
struct tree
{
int l,r;
int mx,mn;
int lz;
tree(){}
tree(int _l,int _r,int _mx,int _mn,int _lz)
{
l=_l,r=_r,mx=_mx,mn=_mn,lz=_lz;
}
}seg[maxn<<];
struct tree1
{
int l,r;
ll val;
tree1(){}
tree1(int _l,int _r,ll _val)
{
l=_l,r=_r,val=_val;
}
}seg1[maxn<<];
void build(int step,int l,int r)
{
seg[step]=tree(l,r,-oo,oo,);
if(l==r)
return;
int mid=(l+r)>>;
build(lson,l,mid);
build(rson,mid+,r);
}
void build1(int step,int l,int r)
{
seg1[step]=tree1(l,r,);
if(l==r)
return;
int mid=(l+r)>>;
build1(lson,l,mid);
build1(rson,mid+,r);
}
void pushdown(int step)
{
seg[lson].mn=min(seg[lson].mn,seg[step].mn);
seg[rson].mn=min(seg[rson].mn,seg[step].mn);
seg[lson].mx=max(seg[lson].mx,seg[step].mx);
seg[rson].mx=max(seg[rson].mx,seg[step].mx);
seg[lson].lz=max(seg[lson].lz,seg[step].lz);
seg[rson].lz=max(seg[rson].lz,seg[step].lz);
seg[step].lz=;
}
void pushdown1(int step)
{
seg1[lson].val+=seg1[step].val;
seg1[rson].val+=seg1[step].val;
seg1[step].val=;
}
void update(int step,int l,int r,int val)
{
if(seg[step].l==l&&seg[step].r==r)
{
seg[step].mx=max(seg[step].mx,val);
seg[step].mn=min(seg[step].mn,val);
seg[step].lz=max(seg[step].lz,val);
return ;
}
if(seg[step].lz)
pushdown(step);
int mid=(seg[step].l+seg[step].r)>>;
if(mid>=r)
update(lson,l,r,val);
else if(mid<l)
update(rson,l,r,val);
else
{
update(lson,l,mid,val);
update(rson,mid+,r,val);
}
}
void update1(int step,int l,int r,int v)
{
if(seg1[step].l==l&&seg1[step].r==r)
{
seg1[step].val+=v;
return ;
}
if(seg1[step].val)
pushdown1(step);
int mid=(seg1[step].l+seg1[step].r)>>;
if(mid>=r)
update1(lson,l,r,v);
else if(mid<l)
update1(rson,l,r,v);
else
{
update1(lson,l,mid,v);
update1(rson,mid+,r,v);
}
}
void query(int step)
{
if(seg[step].l==seg[step].r)
{
p[seg[step].l]=make_pair(seg[step].mn,seg[step].mx);
return ;
}
if(seg[step].lz)
pushdown(step);
query(lson);
query(rson);
}
void query1(int step)
{
if(seg1[step].l==seg1[step].r)
{
ans[seg1[step].l]=seg1[step].val;
return ;
}
if(seg1[step].val)
pushdown1(step);
query1(lson);
query1(rson);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(~sc2(n,m))
{
rep(i,,n-)
sc(a[i]);
build(,,n-);
rep(i,,m)
{
int l,r;
sc2(l,r);
if(l>r)
swap(l,r);
update(,l,r-,i);
}
query();
build1(,,m);
rep(i,,n-)
if(p[i].first!=oo&&p[i].second!=-oo)
update1(,p[i].first,p[i].second,a[i]);
query1();
rep(i,,m)
ptlld(ans[i]);
}
}
hdu 5861 Road 两棵线段树的更多相关文章
- cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做
搞了一晚上,错了,以后回头再来看 /* 对于每次更新,先处理其儿子方向,再处理其父亲方向 处理父亲方向时无法达到根,那么直接更新 如果能达到根,那么到兄弟链中去更新,使用bfs序 最后,查询结点v的结 ...
- HDU 5861 Road (线段树)
Road 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Description There are n villages alo ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- UVA - 12424 Answering Queries on a Tree(十棵线段树的树链剖分)
You are given a tree with N nodes. The tree nodes are numbered from 1 to N and have colors C1, C2,. ...
- HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4819:Mosaic(线段树套线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给出一个矩阵,然后q个询问,每个询问有a,b,c,代表(a,b)这个点上下左右c/2的矩形区域内的( ...
- Codeforces J. A Simple Task(多棵线段树)
题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...
- R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数
R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- 序列中找子序列的dp
题目网址: http://codeforces.com/problemset/problem/414/B Description Mashmokh's boss, Bimokh, didn't lik ...
- JMS学习(一)基本概念
这两天面试了一两个公司,由于简历中的最近一个项目用到了JMS,然而面试官似乎对这个很感兴趣,所以都被问到了,但可惜的是,我除了说我们使用了JMS外,面对他们提出的一些关于JMS的问题,我回答得相当差, ...
- Hibernate关联映射及高级查询
一.Hibernate中的关联关系 1.1.单向一对多关联关系 按照以下步骤配置hibernate中持久化类的一对多对象关联: (1).持久化类添加关联类的相关属性及getter/setter方法. ...
- tomcat filewatchdog but has failed to stop it原因以及解决方法
停止tomcat,有些时候会报The web application [/XXX] appears to have started a thread named [FileWatchdog] but ...
- C++ 面向对象的三个特点--继承与封装(二)
顺着上一篇的内容,我们继续来了解继承的基本知识. 派生类的构造函数和析构函数 派生类继承了基类的成员,但是不能继承基类的构造函数和析构函数,首先,我们了解构造函数和析构函数的执行顺序是当我们创建一个派 ...
- jQuery中的尺寸及位置的取和设
1.offset(); 获取位置值: $(selector).offset().left; $(selector).offset().top; 设置位置值: $(selector).offset({t ...
- andriod 读取网络图片
来自:http://blog.csdn.net/jianghuiquan/article/details/8641283 Android手机上,我们常用ImageView显示图片,我们本章获取网络图片 ...
- MySQL数据库中字符集的问题
今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...
- Python基础(7)--函数
本篇文章将介绍如何将语句组织成函数,以及参数概念以及在程序中的用途 本文地址:http://www.cnblogs.com/archimedes/p/python-function.html,转载请注 ...
- IOS 支付宝-五福简单框架实现-线性滚动(UICollectionView)
猴年支付宝可算是给大家一个很好的惊喜,刺激.大家都在为敬业福而四处奔波.可是到最后也没有几个得到敬业福德,就像我.不知道大家有没有观察,五福界面的滚动是一个很好的设计.在这里,给大家带来简单的滚动实现 ...