HDU - 5861 Road

1.题面

http://acm.split.hdu.edu.cn/showproblem.php?pid=5861

2.题意

给你n个村庄,每两个相邻的村庄有一条路,m个操作,每次都要从一个村庄走到另外一个村庄,每一条路每次都有一个维修的费用,每条路一开始是关闭的,你可以打开一次,关闭一次。问你最少的费用是多少。

3.思路

需要统计出个路口开放和关闭的最大时间,我一开始用了线段树, 过了之后才发现别人都是没用任何数据结构过的, 算是长见识了.就当练习了一下线段树吧

复杂的方法和简单的方法我都会贴代码

4.代码

/*****************************************************************
    > File Name: cpp_acm.cpp
    > Author: Uncle_Sugar
    > Mail: uncle_sugar@qq.com
    > Created Time: Thu 18 Aug 2016 23:22:22 CST
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

# define rep(i,a,b) for (i=a;i<=b;i++)
# define rrep(i,a,b) for (i=b;i>=a;i--)

template<class T>void PrintArray(T* first,T* last,char delim=' '){
    for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}

const int debug = 1;
const int size  = 10 + 200000 ; 
const int INF = INT_MAX>>1;
typedef long long ll;

/*
1.see the size of the input data before you select your algorithm 
2.cin&cout is not recommended in ACM/ICPC
3.pay attention to the size you defined, for instance the size of edge is double the size of vertex
*/

struct Node{
    int ma, mi, flag;
}tree[4*size];

inline void PushDown(int no){
    if (tree[no].flag == 1){
        tree[no<<1].ma = tree[no].ma;
        tree[no<<1].mi = tree[no].mi;

        tree[no<<1|1].ma = tree[no].ma;
        tree[no<<1|1].mi = tree[no].mi;

        tree[no].flag = 0;
    }
}

inline void PushUp(int no){
    if (tree[no<<1].flag && tree[no<<1|1].flag && tree[no<<1].mi == tree[no<<1|1].mi && tree[no<<1].ma == tree[no<<1|1].ma){
        tree[no].mi = tree[no<<1].mi;
        tree[no].ma = tree[no<<1].ma;
        tree[no].flag = 1;
    }
}

void Build(int no, int l, int r){
    //# cout << "no l r " << no << " " << l << " " << r << endl;
    if (l==r){
        tree[no].ma = -INF; tree[no].mi = INF;
        tree[no].flag = 1;
    }else {
        int mid = l+r>>1;
        Build(no<<1, l, mid);
        Build(no<<1|1, mid+1, r);
        PushUp(no);
    }
}

void Update(int lb, int rb, int val, int no, int l, int r){
    if (tree[no].flag==1&& tree[no].mi <= val && val <= tree[no].ma) return;
    if (tree[no].flag==1&& lb <= l && r <= rb){
        tree[no].ma = max(val, tree[no].ma);
        tree[no].mi = min(val, tree[no].mi);
    }else {
        int mid = l+r>>1;
        PushDown(no);
        if (lb <= mid) Update(lb, rb, val, no<<1, l, mid);
        if (rb >  mid) Update(lb, rb, val, no<<1|1, mid+1, r);
        PushUp(no);
    }
}


int cost[size];
struct Point{
    int x,type,cost;
    Point(){}
    Point(int _x, int _type, int _cost):x(_x), type(_type), cost(_cost){}
    bool operator < (const Point& cmper)const{
		return x < cmper.x;
    }
    void Print(){
        cout << "x type cost " << x << " " << type << " " << cost << endl;
    }
}point[4*size];
int plen;

void Read(int no, int l, int r){
    //# cout << "no l r flag " << no << " " << l << " " << r << " " << tree[no].flag << endl;
    if (tree[no].flag == 1){
        for (int i = l; i <= r; i++){
            point[plen++] = Point(tree[no].mi, 1, cost[i]);
            point[plen++] = Point(tree[no].ma+1, 2, cost[i]);
        }
    }else {
        int mid = l+r>>1;
        Read(no<<1, l, mid);
        Read(no<<1|1, mid+1, r);
    }
}


int main(){
    /*std::ios::sync_with_stdio(false);cin.tie(0);*/
    int n,m;
    while (~scanf("%d%d",&n,&m)){
        plen = 0;
        Build(1, 1, n-1);
        for (int i = 1; i < n; i++) scanf("%d",&cost[i]);
        for (int i = 1; i <= m; i++){
            int l, r;
            scanf("%d%d",&l ,&r);
			if (l==r) continue;
			if (l >= r) swap(l, r); r--;
            Update(l, r, i, 1, 1, n-1);
        }
        Read(1, 1, n-1);
        sort(point, point+plen);
        int s = 0;
        ll sum = 0;
		while (s < plen && (point[s].x < 1))  s++;
        for (int i = 1; i <= m; i++){
            while (s < plen && (point[s].x <= i)){
                if (point[s].type == 1){
                    sum += point[s].cost;
                }else {
                    sum -= point[s].cost;
                }
                s++;
            }
            printf("%lld\n", sum);
        }
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值