pipioj 1466: PIPI撿垃圾Ⅱ(平衡樹or權值線段樹)

看了羣友的平衡樹stl板子,學到了orz

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #include <bits/stdc++.h>
 4 #include <ext/pb_ds/tree_policy.hpp>
 5 #include <ext/pb_ds/assoc_container.hpp>
 6  using namespace  std;
 7 typedef long long ll;
 8 using namespace std;
 9 using namespace __gnu_pbds;
10   
11 tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> T;
12  
13 const int N=1e5+10;
14  
15 int n;
16  
17  
18 int main(){
19     IO;
20     cin.tie(0),cout.tie(0);
21     cin>>n;
22     cout<<fixed<<setprecision(20);
23     for(int i=1;i<=n;i++){
24         int x,y;
25         cin>>x;
26         T.insert((1ll*x<<30)+i);//不能插入重複的值
27         x=(*T.find_by_order(i/2-1)>>30);
28         y=(*T.find_by_order(i/2)>>30);
29         if(i%2)cout<<1.0*y<<'\n';
30         else cout<<0.5*(x+y)<<'\n';
31     }
32      
33  
34  
35     /*int q, opt, x;
36   
37     scanf("%d", &q);
38     for (int i = 1; i <= q; ++ i)
39     {
40         scanf("%d%d", &opt, &x);
41         if(opt == 1) 
42             T.insert(x + i * 1e-6);
43         //插入一個數
44         if(opt == 2) 
45             T.erase(T.lower_bound(x));
46         //刪除一個數
47         if(opt == 3) 
48             printf("%d\n", (int)T.order_of_key(x) + 1);
49         //查詢一個數的排名
50         if(opt == 4) 
51             printf("%d\n", (int)*T.find_by_order(x - 1));
52         //查詢第k小的數 返回的是一個迭代器 這裏k是從0開始算的,意思是最小的數是第0小的
53         if(opt == 5) 
54             printf("%d\n", (int)round(*(-- T.lower_bound(x))));
55         //查詢一個數的前驅
56         if(opt == 6) 
57             printf("%d\n", (int)round(*T.lower_bound(x + 1)));
58         //查詢一個數的後繼
59     }
60   
61     return 0;*/
62 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章