P5076 【深基16.例7】普通二叉树(简化版)

https://www.luogu.com.cn/problem/P5076

模版题

 1 #include<cstdio>
 2 #include<set>
 3 using namespace std;
 4 multiset<int>ms;
 5 int q, op, x;
 6 int main()
 7 {
 8     scanf("%d",&q);
 9     while(q--)
10     {
11         scanf("%d%d",&op,&x);
12         if(op==1){
13             multiset<int>::iterator it=ms.lower_bound(x);
14             int order=1;
15             for(multiset<int>::iterator i=ms.begin();i!=it;i++,order++);
16             printf("%d\n",order);
17         }
18         if(op==2){
19             multiset<int>::iterator it;
20             int order=1;
21             for(it=ms.begin(); it!=ms.end(); it++){
22                 if(order==x){
23                     printf("%d\n", *it);
24                     break;
25                 }    
26                 order++;
27             }    
28         }
29         if(op==3){
30             if(ms.lower_bound(x)==ms.begin())
31                 printf("-2147483647\n");
32             else
33                 printf("%d\n", *(--ms.lower_bound(x)) );
34         }
35         if(op==4){
36             if(ms.upper_bound(x)==ms.end())
37                 printf("2147483647\n");
38             else
39                 printf("%d\n", *(ms.upper_bound(x)));
40         }
41         if(op==5){
42             ms.insert(x);
43         }
44     }
45     return 0;
46  } 

注释代码

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<set>
 6 using namespace std;
 7 multiset<int>q;
 8 int n,t,x,order;
 9 int main()
10 {
11     q.insert(-0x7fffffff);
12     q.insert(0x7fffffff);
13     //提前放入这两个数,避免错误
14     scanf("%d",&n);
15     while(n--)
16     {
17         scanf("%d%d",&t,&x);
18         if(t==1)
19         {
20             multiset<int>::iterator it=q.lower_bound(x);
21             order=0;
22             for(multiset<int>::iterator i=q.begin();i!=it;i++,order++);
23             printf("%d\n",order);
24         }
25         else if(t==2)
26         {
27             order=-1;
28             //初值为-1是因为前面有一个-0x7fffffff,所以order要多跑一步
29 
30             for(multiset<int>::iterator i=q.begin(); i != q.end(); i++)
31                 if(++order==x)
32                 //缩写,order先自增一,再判断是否与x相等
33                 //如果是(order++==x),那就是先判断再自增,这里要尤其注意
34                     printf("%d\n",i);
35                 //i就是容器里的值,输出i
36 
37             //注意这里的for(:)循环,是只有C++11以上才支持的
38             //和auto一样,都是noip不能用的
39             //这种循环,i就是容器里的值而不是下标
40             //也可以使用迭代器循环,上面的循环等价于
41             /*
42                 for(multiset<int>::iterator it=q.begin();it!=q.end();it++)
43                 {
44                     order++;
45                     if(order==x)
46                         printf("%d\n",*it);
47                 }
48             */
49             //这种循环是noip可以使用的
50         }
51         else if(t==3)
52         {
53             //取得第一个大于等于x的值
54             //也就是第一个x的位置
55             //由于我们要取得前驱,所以it要自减一
56             printf("%d\n",*(--q.lower_bound(x)));
57             //这句是先自减,再输出,是缩写
58             //等价于:
59             /*
60                 it--;
61                 printf("%d\n",*it);
62             */
63             //因为是迭代器(指针),所以输出前面加 *
64         }
65         else if(t==4)
66         {
67             printf("%d\n",*q.upper_bound(x));
68             //要取得后继,就是第一个大于x的值
69             //用upper_bound方法取得第一个大于x的迭代器
70             //输出即可
71             //因为是迭代器(指针),所以输出前面加 *
72         }
73         else
74         {
75             q.insert(x);
76             //直接添加即可
77         }
78     }
79     return 0;
80 }
View Code

 

posted @ 2020-12-31 15:14  TFLSNOI  阅读(183)  评论(0编辑  收藏  举报