线段树+spfa求最长路。
逆向思维,从最底下一块板子建图。需要注意的是任何一个板子掉落下面再无板子,此时都可以看做一个终结状态。
/* 3016 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
int v, w, nxt;
} edge_t; typedef struct node_t {
int h, xl, xr, w; node_t() {} friend bool operator< (const node_t& a, const node_t& b) {
return a.h < b.h;
} } node_t; const int BASE = ;
const int maxn = 1e5+;
const int maxv = 1e5+;
const int maxe = 5e5+;
node_t nd[maxv];
bool mark[maxn<<];
int ID[maxn<<];
int X[maxn];
edge_t E[maxe];
int head[maxv];
int dis[maxv];
bool visit[maxv];
int m; void init() {
memset(head, -, sizeof(head));
m = ;
} void addEdge(int u, int v, int w) {
E[m].v = v;
E[m].w = w;
E[m].nxt = head[u];
head[u] = m++;
} inline void PushDown(int rt) {
if (mark[rt]) {
ID[rt<<] = ID[rt<<|] = ID[rt];
mark[rt<<] = mark[rt<<|] = true;
mark[rt] = false;
}
} void Build(int l, int r, int rt) {
mark[rt] = true;
ID[rt] = ;
if (l == r)
return ; int mid = (l + r) >> ;
Build(lson);
Build(rson);
} void Update(int L, int R, int id, int l, int r, int rt) {
if (L<=l && R>=r) {
ID[rt] = id;
mark[rt] = true;
return ;
} PushDown(rt);
int mid = (l + r) >> ; if (L > mid) {
Update(L, R, id, rson);
} else if (R <= mid) {
Update(L, R, id, lson);
} else {
Update(L, R, id, lson);
Update(L, R, id, rson);
}
} int Query(int x, int l, int r, int rt) {
if (l == r)
return ID[rt]; PushDown(rt);
int mid = (l + r) >> ; if (x <= mid)
return Query(x, lson);
else
return Query(x, rson);
} int spfa(int s, int t) {
queue<int> Q;
int u, v, k; memset(dis, , sizeof(dis));
memset(visit, false, sizeof(visit));
dis[s] = ;
visit[s] = true;
Q.push(s); while (!Q.empty()) {
u = Q.front();
Q.pop();
visit[u] = false;
for (k=head[u]; k!=-; k=E[k].nxt) {
v = E[k].v;
if (dis[u]+E[k].w > dis[v]) {
dis[v] = dis[u] + E[k].w;
if (!visit[v]) {
visit[v] = true;
Q.push(v);
}
}
}
} int ret = (dis[t]==) ? - : dis[t];
return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n;
int lid;
int rid;
int ans;
int src, des; while (scanf("%d", &n) != EOF) {
memset(visit, false, sizeof(visit));
rep(i, , n+) {
scanf("%d %d %d %d", &nd[i].h, &nd[i].xl, &nd[i].xr, &nd[i].w);
visit[nd[i].xl] = visit[nd[i].xr] = true;
}
sort(nd+, nd++n);
if (nd[n].w <= -) {
puts("-1");
continue;
}
int nx = ;
rep(i, , maxn) {
if (visit[i])
X[i] = ++nx;
}
src = ;
des = n+; init();
Build(, nx, );
Update(X[nd[].xl], X[nd[].xr], , , nx, );
addEdge(, des, );
rep(i, , n+) {
lid = Query(X[nd[i].xl], , nx, );
if (lid)
addEdge(i, lid, nd[lid].w);
else
addEdge(i, des, );
rid = Query(X[nd[i].xr], , nx, );
if (rid) {
if (lid != rid)
addEdge(i, rid, nd[rid].w);
} else {
addEdge(i, des, );
}
Update(X[nd[i].xl], X[nd[i].xr], i, , nx, );
}
addEdge(src, n, nd[n].w); ans = spfa(src, des);
printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}