pojLost Cows(线段树)

题目链接:http://poj.org/problem?id=2182

ac代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 const int N = 8000 + 10;
 6 
 7 int a[N], Tree[N << 2], ans[N], vis[N];
 8 
 9 void Build(int l, int r, int rt) {
10     Tree[rt] = r - l + 1;
11     if (l == r) {
12         return;
13     }
14     int m = (l + r) >> 1;
15     Build(l, m, rt << 1);
16     Build(m + 1, r, rt << 1 | 1);
17 }
18 void Update(int p,int cut, int l, int r, int rt) {
19     Tree[rt]--;
20     if (l == r) {
21         ans[cut] = l;
22         return;
23     }
24     int m = (l + r) >> 1;
25     if (p <= Tree[rt << 1]) Update(p, cut, l, m, rt << 1);
26     else Update(p - Tree[rt << 1], cut, m + 1, r, rt << 1 | 1);
27 }
28 
29 int main() {
30 
31     ios::sync_with_stdio(0);
32     cin.tie(0);
33     int n;
34     cin >> n;
35     for (int i = 2; i <= n; i++) {
36         cin >> a[i];
37     }
38     Build(1, n, 1);
39     for (int i = n; i >= 2; i--) {
40         Update(a[i] + 1, i, 1, n, 1);
41         vis[ans[i]] = 1;
42     }
43     for (int i = 1; i <= n; i++) {
44         if (!vis[i]) cout << i << "\n";
45     }
46     for (int i = 2; i <= n; i++) {
47         cout << ans[i] << "\n";
48     }
49 
50     return 0;
51 }

 

posted @ 2022-03-21 11:44  Keyzee  阅读(27)  评论(0)    收藏  举报