P3378 堆の模板

如果不是可并堆/带修堆/卡常题,一般都用优先队列实现。

很多O(nlogn)过不了的题都可以用蚯蚓的套路来实现!!!

优先队列带修用延迟删除法。

堆,可以简单的用优先队列来实现,也可以自己手打。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int N = 1000010;
 6 long long int heap[4*N+1],top;
 7 void down(int);
 8 void up(int a)
 9 {
10     while(heap[a]<heap[a>>1] && a!=1) swap(heap[a],heap[a>>1]),a=a>>1;
11     down(a);
12     return;
13 }
14 void down(int a)
15 {
16     if((a<<1)>top) return;///这里的括号不能省;这里的等号不能加。调了一个中午的教训啊
17     if((a<<1)==top)
18     {
19         if(heap[a]>heap[top]) swap(heap[a],heap[top]);
20         return;
21     }
22     if(heap[a]<=heap[a<<1] && heap[a]<=heap[a<<1|1]) return;
23     if(heap[a]<heap[a<<1])
24     {
25         swap(heap[a],heap[a<<1|1]);
26         down(a<<1|1);
27         return;
28     }
29     if(heap[a]<heap[a<<1|1])
30     {
31         swap(heap[a],heap[a<<1]);
32         down(a<<1);
33         return;
34     }
35     if(heap[a<<1]<heap[a<<1|1])
36     {
37         swap(heap[a],heap[a<<1]);
38         down(a<<1);
39         return;
40     }
41     else
42     {
43         swap(heap[a],heap[a<<1|1]);
44         down(a<<1|1);
45         return;
46     }
47 }
48 void add(int a)
49 {
50     heap[++top]=a;
51     up(top);
52     return;
53 }
54 void del()
55 {
56     swap(heap[1],heap[top]);
57     top--;
58     if(top<=1) return;
59     down(1);
60     return;
61 }
62 int main()
63 {
64     int n;
65     scanf ("%d",&n);
66     int flag,x;
67     for(int i=1;i<=n;i++)
68     {
69         scanf ("%d",&flag);
70 
71         if(flag==2) printf("%d\n",heap[1]);
72         else if(flag==3) del();
73         else scanf ("%d",&x),add(x);
74     }
75 
76     return 0;
77 }
模板在此!!!

想不到居然写了70行......


之前写的跟屎一样......

最新模板在此:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using std::swap;
 4 typedef long long LL;
 5 const int N = 100010;
 6 
 7 struct SmallHeap {
 8     LL h[N];
 9     int top;
10     SmallHeap() { 
11         top = 0;
12     }
13     inline void up(int p) {
14         while(p != 1 && h[p] < h[p >> 1]) {
15             swap(h[p], h[p >> 1]);
16             p = p >> 1;
17         }
18         return;
19     }
20     inline void down(int p) {
21         int s = p << 1;
22         while(s <= top) {
23             if(s < top && h[s] > h[s | 1]) {
24                 s = s | 1;
25             }
26             if(h[s] < h[p]) {
27                 swap(h[s], h[p]);
28                 p = s;
29                 s = p << 1;
30             }
31             else break;
32         }
33         return;
34     }
35     inline void insert(LL a) {
36         h[++top] = a;
37         up(top);
38         return;
39     }
40     inline void del(int p) {
41         h[p] = h[top--];
42         up(p);
43         down(p);
44         return;
45     }
46     inline LL gettop() {
47         return h[1];
48     }
49     inline void pop() {
50         h[1] = h[top--];
51         down(1);
52         return;
53     }
54 }heap;
55 
56 int main() {
57     LL n, x;
58     scanf("%lld", &n);
59     for(int i = 1; i <= n; i++) {
60         scanf("%lld", &x);
61         heap.insert(x);
62     }
63     LL ans = 0;
64     for(int i = 1; i < n; i++) {
65         LL temp = heap.gettop();
66         heap.pop();
67         temp += heap.gettop();
68         heap.pop();
69         ans += temp;
70         heap.insert(temp);
71     }
72     printf("%lld", ans);
73     return 0;
74 }
小根堆

 

posted @ 2018-05-24 13:30  huyufeifei  阅读(157)  评论(0编辑  收藏  举报
试着放一个广告栏(虽然没有一分钱广告费)

ReadEra 阅读书籍

『Flyable Heart 応援中!』 HHG 高苗京铃 闪十PSS 双六 電動伝奇堂 章鱼罐头制作组 はきか 祝姬 星降夜