4 条题解

  • 0
    @ 2026-8-3 11:17:35
    #include<bits/stdc++.h>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/tree_policy.hpp>
    #define f first
    using namespace std;
    using namespace __gnu_pbds;
    template<typename T>using trset=tree<
    	T,null_type,less<T>,rb_tree_tag,
    	tree_order_statistics_node_update
    >;
    trset<pair<int,int> >s;int id;
    int main()
    {
    	int q;scanf("%d",&q);
    	while(q--)
    	{
    		int op,x;scanf("%d%d",&op,&x);
    		if(op==0)s.insert({x,++id});
    		else if(op==1)
    		{
    			auto it=s.lower_bound({x,0});
    			s.erase(it);
    		}
    		else if(op==2)
    		{
    			if(int(s.size())<x)puts("-1");
    			else
    			{
    				auto it=s.find_by_order(x-1);
    				printf("%d\n",it->f);
    			}
    		}
    		else if(op==3)printf("%d\n",(int)s.order_of_key({x,0}));
    		else if(op==4)
    		{
    			auto it=s.upper_bound({x,0});
    			if(it==s.begin())puts("-1");
    			else printf("%d\n",prev(it)->f);
    		}
    		else
    		{
    			auto it=s.lower_bound({x+1,0});
    			if(it==s.end())puts("-1");
    			else printf("%d\n",it->f);
    		}
    	}
    	return 0;
    }
    
    • 0
      @ 2026-8-3 10:26:56

      或许我们可以不用 PII?

      `

      #include<bits/stdc++.h>
      #include<bits/extc++.h>
      #define int long long
      using namespace std;
      using namespace __gnu_pbds;
      const int B=1e6;
      tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>s;
      map<int,int>mp;
      signed main()
      {
      	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
      	int q;cin>>q;
          while(q--)
          {
              int op,x;cin>>op>>x;
              if(op==0)mp[x]++,s.insert(x*B+mp[x]);
              if(op==1)s.erase(x*B+mp[x]),mp[x]--;
              if(op==2)cout<<*s.find_by_order(x-1)/B<<'\n';
              if(op==3)cout<<s.order_of_key(x*B)<<'\n';
              if(op==4)
              {
                  auto it=s.lower_bound(x*B);
                  if(it!=s.begin())it--,cout<<*it/B<<'\n';
                  else cout<<-1<<'\n';
              }
              if(op==5)
              {
                  auto it=s.lower_bound((x+1)*B);
                  if(it!=s.end())cout<<*it/B<<'\n';
                  else cout<<-1<<'\n';
              }
          }
      	return 0;
      }
      
      
      • 0
        @ 2026-8-3 1:36:40
        #include <bits/stdc++.h>
        #include <ext/pb_ds/assoc_container.hpp>
        #include <ext/pb_ds/tree_policy.hpp>
        #define int long long
        using namespace std;
        using namespace __gnu_pbds;
        
        // 使用 pb_ds 的平衡树,存储 pair<int, int> 以支持重复元素(多重集)
        // first 存储实际的值,second 存储插入时的唯一编号
        typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
        
        signed main() {
            ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
            int n;
            cin >> n;
            ordered_set os;
            int id = 0; // 用于区分相同元素
            
            for (int i = 1; i <= n; i++) {
                int op, x;
                cin >> op >> x;
                if (op == 0) { // 插入 x
                    os.insert({x, ++id});
                } else if (op == 1) { // 删除一个 x
                    // lower_bound({x, 0}) 会找到第一个 first >= x 的元素
                    // 因为保证 x 存在,所以一定能找到 {x, y} (y > 0)
                    os.erase(os.lower_bound({x, 0}));
                } else if (op == 2) { // 求第 k 小
                    // find_by_order(k) 返回第 k+1 小的元素的迭代器(从 0 开始计数)
                    cout << os.find_by_order(x - 1)->first << '\n';
                } else if (op == 3) { // 求小于 x 的元素个数
                    // order_of_key({x, 0}) 返回严格小于 {x, 0} 的元素个数
                    // 即所有 first < x 的元素个数
                    cout << os.order_of_key({x, 0}) << '\n';
                } else if (op == 4) { // 求小于 x 的最大数(前驱)
                    auto it = os.lower_bound({x, 0});
                    if (it == os.begin()) {
                        cout << -1 << '\n';
                    } else {
                        cout << prev(it)->first << '\n';
                    }
                } else if (op == 5) { // 求大于 x 的最小数(后继)
                    // 大于 x 的最小数,即 >= x + 1 的最小数
                    auto it = os.lower_bound({x + 1, 0});
                    if (it == os.end()) {
                        cout << -1 << '\n';
                    } else {
                        cout << it->first << '\n';
                    }
                }
            }
            return 0;
        }
        
        • 0
          @ 2025-10-8 16:50:29
          #include<bits/stdc++.h>
          #include<ext/pb_ds/assoc_container.hpp>
          #include<ext/pb_ds/tree_policy.hpp>
          using namespace std;
          using namespace __gnu_pbds;
          typedef pair<int,int> PII;
          typedef tree<PII,None_type,less<PII>,rb_tree_tag,tree_order_statistics_node_update> Tree;
          Tree T;int n;unordered_map<int,int>ma;
          int main()
          {
              scanf("%d",&n);
              T.insert({-1,1});T.insert({int(1e9+1),1});
              for(int i=1;i<=n;i++)
              {
                  int op;scanf("%d",&op);
                  int x;scanf("%d",&x);
                  if(op==0)T.insert({x,++ma[x]});
                  if(op==1)T.erase({x,ma[x]--});
                  if(op==2)printf("%d\n",*T.find_by_order(x));
                  if(op==3)printf("%d\n",T.order_of_key({x,0})-1);
                  if(op==4)printf("%d\n",*--T.lower_bound({x,0}));
                  if(op==5)
                  {
                      PII t=*T.upper_bound({x+1,0});
                      if(t.first==int(1e9+1))printf("-1\n");else printf("%d\n",t);
                  }
              }
              return 0;
          }
          
          • 1

          信息

          ID
          449
          时间
          5000ms
          内存
          256MiB
          难度
          8
          标签
          (无)
          递交数
          95
          已通过
          14
          上传者