跳到正文

算法·理论

封装

#SUSPENDED#约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月21日
篇幅
约 7 分钟

版本

  1. v1当前
C++
namespace OI
{
	template<typename T>
	inline void sw(T &a,T &b)
	{
		T c=a;
		a=b;
		b=c;
	}
	template<typename T>
	class heap
	{
		private :
			int n;
			T h[1000010];
		public :
			inline heap(void)
			{
				n=0;
			}
			inline void up(int p)
			{
				while(p>1)
				{
					if(!(h[p]<h[p/2]))
					{
						sw(h[p],h[p/2]);
						p/=2;
					}
					else break;
				}
			}
			inline void down(int p)
			{
				int s=p*2;
				while(s<=n)
				{
					if(s<n&&h[s]<h[s+1])s++;
					if(!(h[s]<h[p]))
					{
						sw(h[s],h[p]);
						p=s,s=p*2;
					}
					else break;
				}
			}
			inline void insert(T num)
			{
				h[++n]=num;
				up(n);
			}
			inline T top(void)
			{
				return h[1];
			}
			inline bool pop(void)
			{
				if(n==0)return false;
				h[1]=h[n--];
				down(1);
				return true;
			}
			inline bool remove(int k)
			{
				if(n<k)return false;
				h[k]=h[n--];
				up(k);
				down(k);
				return true;
			}
			inline bool empty(void)
			{
				if(n==0)return true;
				return false;
			}
			inline int size(void)
			{
				return n;
			}
			inline T value(int k)
			{
				return h[k];
			}
	};
	template<typename T>
	class sumtree1
	{
		private :
			T a1[100010];
			int n;
		public : 
			inline T lowbit(const T x)
			{
				return x&-x;
			}
			inline void maketree1(T t,const T v)
			{
				while(t<=n)
				{
					a1[t]+=v;
					t+=lowbit(t);
				}
			}
			inline void update(T t,const T v)
			{
				while(t<=n)
				{
					a1[t]+=v;
					t+=lowbit(t);
				}
			}
			inline int findsum(T x,T y)
			{
				T t=y,ans=0;
				while(t>=1)
				{
					ans+=a1[t];
					t-=lowbit(t);
				}
				--x;
				t=x;
				while(t>=1)
				{
					ans-=a1[t];
					t-=lowbit(t);
				}
				return ans;
			}
	};
	template<typename T>
	class maxtree
	{
	public :
		T tree[100010],a[100010];
	public :
		inline T lowbit(const T &x)
		{
			return x&-x;
		}
		inline T max(const T &a,const T &b)
		{
			return (a > b) ? a : b;
		}
		inline T findmax(const T &x,const T &y)
		{
			T t1=y,maxn=0;
			while(t1-lowbit(t1)>=x)
			{
				maxn=max(maxn,tree[t1]);
				t1-=lowbit(t1);
			}
			while(t1>=x)
			{
				maxn=max(a[t1],maxn);
				--t1;
			}
			return maxn;
		}
		inline void update(const T i,const T &n,const T now)
		{
			T t=i;
			while(t<=n)
			{
				tree[t]=max(tree[t],now);
				t+=lowbit(t);
			}
		}
		inline void firstcmd(const T &x,const T &y,const T &n)
		{
			T tmp=a[x],t=x;
			a[x]=y;
			while(t<=n)
			{
				if(tree[t]==tmp)tree[t]=findmax(t-lowbit(t)+1,t);
				else tree[t]=max(tree[t],y);
				t+=lowbit(t);
			}
		}
	};
	template<typename T>
	inline void read(T &x)
	{
		int f = x = 0, v = getchar();
		while(v != '-' && (v < '0' || v > '9')) v = getchar();
		if(v == '-') f = 1, v = getchar();
		while(v >= '0' && v <= '9') x = (x << 3) + (x << 1) + (v & 15), v = getchar();
		if(f) x = -x;
	}
	template<typename T>
	inline void write(T x)
	{
		if(x<0)putchar('-'),x=-x;
		if(x>9)write(x/10);
		putchar(x%10+'0');
		return;
	}
	template<typename T>
	inline void qsort(T &a[],T l,T r)
	{
		if(l<r)
		{
			T i=l,j=r,x=a[l];
			while(i<j)
			{
				while(i<j&&a[j]>=x)
				{
					--j;
				}
				if(i<j)
				{
					a[i++]=a[j];
				}
				while(i<j&&a[i]<x)
				{
					++i;
				}
				if(i<j)
				{
					a[j--]=a[i];
				}
			}
			a[i]=x;
			qsort(a,l,i-1);
			qsort(a,i+1,r);
		}
	}
}
原文