zoukankan      html  css  js  c++  java
  • 用pb_ds写一颗线段树

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/branch_policy/branch_policy.hpp>
    
    using namespace std;
    using namespace __gnu_pbds;
    
    template<class T1,class T2>
    pair<T1,T2> operator +(const pair<T1,T2> &a,const pair<T1,T2> &b){
    	return pair<T1,T2>(a.first+b.first,a.second+b.second);
    }
    #define TTT template<typename Node_CItr, typename Node_Itr, typename Cmp_Fn, typename _Alloc>
    
    TTT class interval_sum_node_update : detail::branch_policy<Node_CItr, Node_Itr, _Alloc>
    {
    	private:
    		typedef detail::branch_policy<Node_CItr, Node_Itr, _Alloc> base_type;
    	public:
    		typedef Cmp_Fn 									cmp_fn;
    		typedef _Alloc 									allocator_type;
    		typedef typename allocator_type::size_type 		size_type;
    		typedef typename base_type::key_type 			key_type;
    		typedef typename base_type::key_const_reference	key_const_reference;
    		typedef typename tuple_element<0,key_type>::type key_type_0;
    		typedef typename tuple_element<1,key_type>::type key_type_1;
    		typedef const key_type_0& key_0_const_reference;
    		typedef const key_type_1& key_1_const_reference;
    		
    		typedef key_type 									metadata_type;
    		typedef Node_CItr 	       							node_const_iterator;
    		typedef Node_Itr 									node_iterator;
    		typedef typename node_const_iterator::value_type 	const_iterator;
    		typedef typename node_iterator::value_type 			iterator;
    		
    		key_type_1
    		interval_sum(key_0_const_reference, key_0_const_reference);
    
    	private:
    		typedef typename base_type::const_reference 	const_reference;
    		typedef typename base_type::const_pointer 		const_pointer;
    
    		typedef typename _Alloc::template rebind<metadata_type>::other __rebind_m;
    		typedef typename __rebind_m::const_reference       metadata_const_reference;
    		typedef typename __rebind_m::reference 		metadata_reference;
    
    		virtual node_const_iterator
    		node_begin() const = 0;
    
    		virtual node_iterator
    		node_begin() = 0;
    
    		virtual node_const_iterator
    		node_end() const = 0;
    
    		virtual node_iterator
    		node_end() = 0;
    
    		virtual cmp_fn&
    		get_cmp_fn() = 0;
    		
    		key_type_1
    		prefix_sum(key_const_reference);
    	
    		static const key_type_1 SUP=INT_MAX,INF=INT_MIN;
    	
    	protected:
    		inline void
    		operator()(node_iterator, node_const_iterator) const;
    };
    
    #define BELONG interval_sum_node_update<Node_CItr, Node_Itr, Cmp_Fn, _Alloc>
    
    TTT
    void BELONG::
    operator ()(node_iterator cur, node_const_iterator end) const
    {
    	node_iterator l = cur.get_l_child(), r = cur.get_r_child();
    	key_type_1 sl = l == end ? 0 : l.get_metadata().second,
    		sr = r == end ? 0 : r.get_metadata().second;
    	const_cast<metadata_reference>(cur.get_metadata()) = **cur + key_type(0,sl) + key_type(0,sr);
    }
    
    TTT
    typename BELONG::key_type_1 BELONG::
    prefix_sum(key_const_reference key)
    {
    	key_type_1 ret = 0;
    	cmp_fn C = get_cmp_fn();
    	node_iterator i = node_begin();
    	for(node_iterator l, r; i != node_end(); )
    	{
    		l = i.get_l_child(), r = i.get_r_child();
    		if(C(key, **i)) i = l;
    		else
    		{
    			ret += (**i).second;
    			if(l != node_end()) ret += l.get_metadata().second;
    			i = r;
    		}
    	}
    	return ret;
    }
    
    TTT
    typename BELONG::key_type_1 BELONG::
    interval_sum(key_0_const_reference l, key_0_const_reference r)
    {
    	return prefix_sum(make_pair(r,SUP)) - prefix_sum(make_pair(l,INF));
    }
    
    int main()
    {
    	tree<pair<int,int>, null_type, less<pair<int,int> >, splay_tree_tag, interval_sum_node_update> tr;
    	int n;
    	scanf("%d", &n);
    	for(int i = 1; i <= n; i++)
    	{
    		int t,tt;
    		scanf("%d%d", &t,&tt);
    		tr.insert(make_pair(t,tt));
    	}
    	printf("%d
    ", tr.interval_sum(1, n));
    }
    

    注意typedef模板类时typename要记得加,参考自> https://www.cnblogs.com/js2xxx/p/9446963.html
    tuple_element推断pair类型
    程序中有人为设定的INT_MAX和INT_MIN

    试用一下pb_ds的map

    #include <iostream>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/branch_policy/branch_policy.hpp>
    
    using namespace std;
    using namespace __gnu_pbds;
    
    int main()
    {
    	tree<int, int, less<int>, splay_tree_tag, tree_order_statistics_node_update> tr;
    	int n;
    	scanf("%d", &n);
    	for(int i = 1; i <= n; i++)
    	{
    		int t,tt;
    		scanf("%d%d", &t,&tt);
    		tr[t]=tt;
    	}
    	printf("%d
    ", tr[1]);
    }
    
  • 相关阅读:
    装饰器
    提供离线chrome谷歌浏览器插件crx的网站有
    关于使用AWS上的RHEL8.x/Redhat系统使用自己单独购买的Redhat官网license导致的yum命令报错处理
    关于aws账单数据中几个重要的与费用相关的字段的意义分析
    在vCenter或者ESXi中通过ova/ovf进行还原部署虚拟机的过程记录
    关于python爬虫request.get()方法的常用参数
    关于aws cli命令的exit/return code分析
    关于pycharm代码运行后控制台的输出不完整被截断的处理
    关于变量的值中包含另一个变量引用的处理间接变量引用
    关于在python中使用pandas模块将列表list/元组tuple写入excel中
  • 原文地址:https://www.cnblogs.com/Yuhuger/p/14071366.html
Copyright © 2011-2022 走看看