码迷,mamicode.com
首页 > 其他好文 > 详细

[zoj3632]线段树的应用

时间:2015-04-12 08:02:49      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

题意:f[i] = min(f[i+L]~f[i+R]) + x,计算f数组。从大到小计算即可,用线段树维护一下。

技术分享
  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 
 17 using namespace std;
 18 
 19 #define mem0(a) memset(a, 0, sizeof(a))
 20 #define lson l, m, rt << 1
 21 #define rson m + 1, r, rt << 1 | 1
 22 #define define_m int m = (l + r) >> 1
 23 #define rep(a, b) for (int a = 0; a < b; a++)
 24 #define rrep(a, b) for (int a = (b - 1); a >= 0; a--)
 25 #define all(a) (a).begin(), (a).end()
 26 #define lowbit(x) ((x) & (-(x)))
 27 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 28 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 29 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 30 #define pc(a) putchar(a)
 31 #define ps(a) printf("%s", a)
 32 #define pd(a) printf("%d", a)
 33 #define sd(a) scanf("%d", &a)
 34 
 35 typedef double db;
 36 typedef long long LL;
 37 typedef pair<int, int> pii;
 38 typedef multiset<int> msi;
 39 typedef set<int> si;
 40 typedef vector<int> vi;
 41 typedef map<int, int> mii;
 42 
 43 const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
 44 const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
 45 const int maxn = 5 * 1e4 + 7;
 46 const int maxm = 1e5 + 7;
 47 const int minv = 1e7 + 7;
 48 const int max_val = 1e6 + 7;
 49 const int MD = 1e9 +7;
 50 const LL INF = 1e15;
 51 const double PI = acos(-1.0);
 52 const double eps = 1e-10;
 53 
 54 template<class T> T gcd(T a, T b) { return b == 0? a : gcd(b, a % b); }
 55 
 56 struct SegTree {
 57     LL minv[maxn << 2];
 58     void build(int l, int r, int rt) {
 59         minv[rt] = INF;
 60         if (l == r) return ;
 61         define_m;
 62         build(lson);
 63         build(rson);
 64     }
 65     LL get(int L, int R, int l, int r, int rt) {
 66         if (L <= l && r <= R) return minv[rt];
 67         define_m;
 68         LL res = INF;
 69         if (L <= m) res = min(res, get(L, R, lson));
 70         if (R > m) res = min(res, get(L, R, rson));
 71         return res;
 72     }
 73     void update(int p, LL x, int l, int r, int rt) {
 74         if (l == r) {
 75             minv[rt] = x;
 76             return ;
 77         }
 78         define_m;
 79         if (p <= m) update(p, x, lson);
 80         else update(p, x, rson);
 81         minv[rt] = min(minv[rt << 1], minv[rt << 1 | 1]);
 82     }
 83 };
 84 
 85 SegTree st;
 86 int cost[maxn], len[maxn];
 87 int main() {
 88     //freopen("in.txt", "r", stdin);
 89     int n;
 90     while (cin >> n) {
 91         rep(i, n) sd(cost[i]);
 92         rep(i, n) sd(len[i]);
 93         st.build(1, n, 1);
 94         rrep(i, n) {
 95             LL x = cost[i];
 96             if (i + len[i] < n) x  += st.get(i + 1, i + 1 + len[i], 1, n, 1);
 97             st.update(i + 1, x, 1, n, 1);
 98         }
 99         cout << st.get(1, 1, 1, n, 1) << endl;
100     }
101     return 0;
102 }
View Code

 

[zoj3632]线段树的应用

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4419006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!