點分治
- Luogu P4206 [NOI2005] 聰聰與可可
板子題,記一下 $ mod \ 3 $ 意義下餘數分別為 $ 1 $ $ 2 $ $ 0 $ 的個數,合併時統計即可;
- Luogu P4149 [IOI2011] Race
板子題,開個二元組記錄一下權值和邊數即可;
- Luogu P4178 Tree
板子題,和第一題類似,只不過開個樹狀陣列記錄一下字首和,然後就解決了;
- Luogu P3714 [BJOI2017] 樹的難題
這題。。。我TM調了三個小時,結果學校OJ上還得卡常!!!
只要用上線段樹等資料結構,學校OJ就過不去
順便發洩一下自己的情緒:上次模擬賽T2開了
27個線段樹,常數確實有些大,但是Luogu上過了,學校OJ上就咋都過不去;
你可能會說,學校OJ咋能和Luogu比呢?
但今天上午,一道虛樹的入門題,時限2s,在Luogu上跑剛過1s,結果在學校OJ上直接TLE倆點,經過我嚴謹的時間複雜度分析,大約是2e8+1e7,我就不理解了,就TM多這麼100ms咋就跑不過去了?(其實可能確實是我菜,整不出題解的優秀複雜度),跟題解一比,多了個線段樹的複雜度,整的我現在打比賽都不敢用線段樹,但就是每場比賽都TM能想出來用線段樹的卡常做法,結果今天上午這題經過_lhx_和cpa一個多小時的大力卡常才勉強過;
現在能力沒咋提升,倒是卡常進步不少;
迴歸正題;
其實思路不難,但細節太多了。。。
首先,路徑還是能拆分成兩類:經過根的和不經過根的;
所以可以點分治;
首先將每個點的兒子按大小排序,因為這樣我們就可以比較方便的處理到根的路徑顏色相同的子樹們;
然後進行點分治,我們開兩個線段樹,把與當前路徑顏色相同的放進一個線段樹,不同的放進一個線段樹,然後正常跑就行;
注意線段樹的清空,可以直接在根節點上打懶標記;
然後就是一些細節,不說了,可以看程式碼;
點選檢視程式碼
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, l, r;
int c[500005];
int rt, sum;
struct sss{
int t, ne, w;
}e[1000005];
int h[1000005], cnt;
void add(int u, int v, int ww) {
e[++cnt].t = v;
e[cnt].ne = h[u];
e[cnt].w = ww;
h[u] = cnt;
}
vector<pair<int, int> > v[200005];
struct sas{
int dis, sum;
}dis[200005], rem[200005], po[200005];
int maxp[1000005], siz[1000005], dep[1000005];
bool vis[1000005];
int ans;
namespace seg{
inline int ls(int x) {
return x << 1;
}
inline int rs(int x) {
return x << 1 | 1;
}
struct asa{
int l, r, ma, lz;
}tr[2][900005];
inline void push_up(int s, int id) {
tr[s][id].ma = max(tr[s][ls(id)].ma, tr[s][rs(id)].ma);
}
inline void push_down(int s, int id) {
if(tr[s][id].lz != 0) {
tr[s][ls(id)].lz = tr[s][id].lz;
tr[s][rs(id)].lz = tr[s][id].lz;
tr[s][ls(id)].ma = tr[s][id].lz;
tr[s][rs(id)].ma = tr[s][id].lz;
tr[s][id].lz = 0;
}
}
void bt(int s, int id, int l, int r) {
tr[s][id].l = l;
tr[s][id].r = r;
if (l == r) {
tr[s][id].ma = -0x3f3f3f3f;
tr[s][id].lz = 0;
return;
}
int mid = (l + r) >> 1;
bt(s, ls(id), l, mid);
bt(s, rs(id), mid + 1, r);
push_up(s, id);
}
inline void clear(int s) {
tr[s][1].lz = -0x3f3f3f3f;
tr[s][1].ma = -0x3f3f3f3f;
}
int ask(int s, int id, int l, int r) {
if (tr[s][id].l >= l && tr[s][id].r <= r) {
return tr[s][id].ma;
}
push_down(s, id);
int mid = (tr[s][id].l + tr[s][id].r) >> 1;
if (r <= mid) return ask(s, ls(id), l, r);
else if (l > mid) return ask(s, rs(id), l, r);
else return max(ask(s, ls(id), l, mid), ask(s, rs(id), mid + 1, r));
}
void add(int s, int id, int pos, int d) {
if (tr[s][id].l == tr[s][id].r) {
tr[s][id].ma = max(tr[s][id].ma, d);
tr[s][id].lz = 0;
return;
}
push_down(s, id);
int mid = (tr[s][id].l + tr[s][id].r) >> 1;
if (pos <= mid) add(s, ls(id), pos, d);
else add(s, rs(id), pos, d);
push_up(s, id);
}
}
void get_rt(int x, int f) {
siz[x] = 1;
maxp[x] = 0;
for (int i = h[x]; i; i = e[i].ne) {
int u = e[i].t;
if (u == f || vis[u]) continue;
get_rt(u, x);
siz[x] += siz[u];
maxp[x] = max(maxp[x], siz[u]);
}
maxp[x] = max(maxp[x], sum - siz[x]);
if (maxp[rt] > maxp[x]) rt = x;
}
void get_dis(int x, int f, int pre) {
dep[x] = dep[f] + 1;
if (dep[x] > r) return;
dis[x].dis = dep[x];
for (int i = h[x]; i; i = e[i].ne) {
int u = e[i].t;
if (vis[u] || u == f) continue;
dis[u].sum = dis[x].sum; //注意繼承的問題;
if (e[i].w != pre && e[i].w) dis[u].sum += c[e[i].w]; //注意判斷;
get_dis(u, x, e[i].w);
}
}
void dfs(int x, int f) {
if (dis[x].dis == 0) return;
rem[++rem[0].sum] = sas{dis[x].dis, dis[x].sum};
for (int i = h[x]; i; i = e[i].ne) {
int u = e[i].t;
if (u == f || vis[u]) continue;
dfs(u, x);
}
}
void calc(int x) {
int color = 0;
int o = 0;
dep[x] = 0;
for (int i = h[x]; i; i = e[i].ne) {
int u = e[i].t;
if (vis[u]) continue;
if (color == 0) {
color = e[i].w;
} else if (color != e[i].w) {
color = e[i].w;
seg::clear(1);
for (int j = 1; j <= o; j++) {
seg::add(0, 1, po[j].dis, po[j].sum);
}
o = 0;
}
rem[0].sum = 0;
dis[u].sum = c[e[i].w];
get_dis(u, x, e[i].w);
dfs(u, x);
for (int j = 1; j <= rem[0].sum; j++) {
if (rem[j].dis > r) continue;
if (rem[j].dis >= l && rem[j].dis <= r) {
ans = max(ans, rem[j].sum);
}
if (rem[j].dis == r) continue;
if (rem[j].dis == 0) continue;
int aa = seg::ask(0, 1, max(0, l - rem[j].dis), r - rem[j].dis);
int bb = seg::ask(1, 1, max(0, l - rem[j].dis), r - rem[j].dis);
bb -= c[e[i].w];
ans = max(ans, max(rem[j].sum + aa, rem[j].sum + bb));
}
for (int j = 1; j <= rem[0].sum; j++) {
if (rem[j].dis == 0) continue;
o++;
po[o].dis = rem[j].dis;
po[o].sum = rem[j].sum;
}
for (int j = 1; j <= rem[0].sum; j++) {
if (rem[j].dis == 0) continue;
seg::add(1, 1, rem[j].dis, rem[j].sum);
}
}
seg::clear(0);
seg::clear(1);
}
void solve(int x) {
vis[x] = true;
calc(x);
for (int i = h[x]; i; i = e[i].ne) {
int u = e[i].t;
if (vis[u]) continue;
rt = 0;
maxp[rt] = 0x3f3f3f3f;
sum = siz[u];
get_rt(u, 0);
solve(rt);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> l >> r;
for (int i = 1; i <= m; i++) {
cin >> c[i];
}
int x, y, w;
for (int i = 1; i <= n - 1; i++) {
cin >> x >> y >> w;
v[x].push_back({w, y});
v[y].push_back({w, x});
}
for (int i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end());
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < v[i].size(); j++) {
add(i, v[i][j].second, v[i][j].first);
}
}
seg::bt(0, 1, 0, n);
seg::bt(1, 1, 0, n);
ans = -0x3f3f3f3f;
rt = 0;
maxp[rt] = 0x3f3f3f3f;
sum = n;
get_rt(1, 0);
solve(rt);
cout << ans;
return 0;
}
貌似題解有單調佇列的優秀做法,但我不會,有興趣的可以去看看;
走了,去卡常了;