[hdu4123]dfs区间化+RMQ

时间:2023-02-02 21:20:41

题意:给一个树编号0~n-1,一个数组a[i]为节点i在树上走的最大距离(不重复点),然后求最大的区间,使得区间最大差异小于某个值。dfs求出每个数组,同时区间化。枚举区间左边界,右边界同样递增,类似单调队列,区间最值用RMQ查询(常数小)。

[hdu4123]dfs区间化+RMQ[hdu4123]dfs区间化+RMQ
  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 lowbit(x) ((x) & (-(x)))
 25 #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) {}
 26 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 27 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 28 
 29 typedef double db;
 30 typedef long long LL;
 31 typedef pair<int, int> pii;
 32 typedef multiset<int> msi;
 33 typedef multiset<int>::iterator msii;
 34 typedef set<int> si;
 35 typedef set<int>::iterator sii;
 36 typedef vector<int> vi;
 37 
 38 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
 39 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
 40 const int maxn = 1e5 + 7;
 41 const int maxm = 1e5 + 7;
 42 const int maxv = 1e7 + 7;
 43 const int MD = 1e9 +7;
 44 const int INF = 1e9 + 7;
 45 const double PI = acos(-1.0);
 46 const double eps = 1e-10;
 47 
 48 template<class T> struct MonotoneQueue{
 49     deque<T> Q;
 50     MonotoneQueue<T>() { Q.clear(); }
 51     void clear() { Q.clear(); }
 52     bool empty() { return Q.empty(); }
 53     void add_back(T x) { while (!Q.empty() && !(Q.back() < x)) Q.pop_back(); Q.push_back(x); }
 54     void pop_front() { Q.pop_front(); }
 55     T back2() { if(Q.size() < 2) return T(); return *(Q.end() - 2); }
 56     T front() { return Q.front(); }
 57 };
 58 
 59 template<class edge> struct Graph {
 60     vector<vector<edge> > adj;
 61     Graph(int n) { adj.clear(); adj.resize(n + 5); }
 62     Graph() { adj.clear(); }
 63     void resize(int n) { adj.resize(n + 5); }
 64     void add(int s, edge e){ adj[s].push_back(e); }
 65     void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
 66     void clear() { adj.clear(); }
 67     vector<edge>& operator [](int t) { return adj[t]; }
 68 };
 69 
 70 Graph<int> G, W;
 71 
 72 
 73 
 74 int maxd, id, d, n, m;
 75 int dis[maxn], t[maxn], maxf[maxn][20], minf[maxn][20];
 76 bool vis[maxn];
 77 
 78 void DFS(int node) {
 79     vis[node] = true;
 80     dis[node] = max(dis[node], d);
 81     if (d > maxd) {
 82         maxd = d;
 83         id = node;
 84     }
 85     for (int i = 0; i < G[node].size(); i++) {
 86         int u = G[node][i];
 87         if (!vis[u]) {
 88             d += W[node][i];
 89             DFS(u);
 90             d -= W[node][i];
 91         }
 92     }
 93 }
 94 
 95 void InitRMQ() {
 96     for (int i = 1; i <= n; i++) maxf[i][0] = minf[i][0] = dis[i];
 97     for (int j = 1; (1 << j) <= n; j++) {
 98         for (int i = 1; i + (1 << j) - 1 <= n; i++) {
 99             maxf[i][j] = max(maxf[i][j - 1], maxf[i + (1 << (j - 1))][j - 1]);
100             minf[i][j] = min(minf[i][j - 1], minf[i + (1 << (j - 1))][j - 1]);
101         }
102     }
103 }
104 int RMQ_max(int L, int R) {
105     int x = t[R - L + 1];
106     return max(maxf[L][x], maxf[R - (1 << x) + 1][x]);
107 }
108 int RMQ_min(int L, int R) {
109     int x = t[R - L + 1];
110     return min(minf[L][x], minf[R - (1 << x) + 1][x]);
111 }
112 
113 int solve(int q) {
114     int L = 1, ans = 0;
115     for (int R = 1; R <= n; R++) {
116         while (RMQ_max(L, R) - RMQ_min(L, R) > q) L++;
117         ans = max(ans, R - L + 1);
118     }
119     return ans;
120 }
121 
122 int main() {
123     //freopen("in.txt", "r", stdin);
124     for (int i = 1; i <= 50000; i++) {
125         int j = 0;
126         while ((1 << (j + 1)) <= i) j++;
127         t[i] = j;
128     }
129     while (cin >> n >> m, n || m) {
130         G.clear(); G.resize(n);
131         W.clear(); W.resize(n);
132         for (int i = 1, u, v, w; i < n; i++) {
133             scanf("%d%d%d", &u, &v, &w);
134             G.add(u, v);
135             W.add(u, w);
136             G.add(v, u);
137             W.add(v, w);
138         }
139         mem0(vis);
140         maxd = -1;
141         DFS(1);
142         int node1 = id;
143         mem0(vis);
144         maxd = -1;
145         DFS(id);
146         int node2 = id;
147         mem0(dis);
148         mem0(vis);
149         DFS(node1);
150         mem0(vis);
151         DFS(node2);
152         InitRMQ();
153         for (int i = 0, q; i < m; i++) {
154             scanf("%d", &q);
155             printf("%d\n", solve(q));
156         }
157     }
158     return 0;
159 }
View Code