跳到正文

个人记录

板子

I_do_Cpp约 4 分钟
原文

这是 2026年1月28日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

原谅我的码风 /jk

快读快写

C++
//Linux 评测机是xxx_unlocked
//Windows 要去掉_unlocked
inline void read(int& x)
{
	x=0;
	char ch=getchar_unlocked();
	bool flag=false;
	while(ch<'0' || ch>'9')
	{
		if(ch=='-') flag=1;
		ch=getchar_unlocked();
	}
	while(ch>='0' && ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar_unlocked();
	}
	if(flag) x=-x;
}
inline void write(int x)
{
	if(x<0)
	{
		putchar_unlocked('-');
		x=-x;
	}
	if(x>9) write(x/10);
	putchar_unlocked(x%10+'0');
}

一维树状数组

C++
class BIT
{
private:
	int lowbit(int x)
	{
		return (x&-x);
	}
public:
	int n;
	vector<int> t;
	BIT(int sz)
	{
		t.resize(sz+4);
        n=sz;
	}

	void add(int id,int k) //加入一个数字
	{
		while(id<=n) 
		{
			t[id]+=k;
			id+=lowbit(id);
		}
	}
	int query(int id) //查询前缀和
	{
		int ans=0;
		while(id>=1)
		{
			ans+=t[id];
			id-=lowbit(id);
		}
		return ans;
	}
};

二维树状数组

C++
class BIT2D
{
	private:
		int n,m;
		vector<vector<int>> tree;
		int lowbit(int x)
		{
			return x&-x;
		}
	public:
		BIT2D(int _n,int _m)
		{
			tree.resize(_n+4,vector<int>(_m+4));
			n=_n,m=_m;
		}
		void add(int x,int y,int k) //增加数
		{
			for(int i=x;i<=n;i+=lowbit(i))
			{
				for(int j=y;j<=m;j+=lowbit(j))
				{
					tree[i][j]+=k;
				}
			}
		}
		int query(int x,int y) //前缀和
		{
			int ans=0;
			for(int i=x;i>=1;i-=lowbit(i))
			{
				for(int j=y;j>=1;j-=lowbit(j))
				{
					ans+=tree[i][j];
				}
			}
			return ans;
		}
};

并查集

C++
class DSU
{
public:
	vector<int> f;
	DSU(int sz)
	{
		f.resize(sz+4);
    for(int i=0;i<=sz;i++)
    {
        f[i]=i;
    }
	}
	int get_f(int x) //寻找x的代表点
	{
		if(f[x]==x)
		{
			return x;
		}
		return f[x]=get_f(f[x]);
	}
	void merge(int a,int b) //合并 
	{
		f[get_f(a)]=get_f(b);
	}
	bool is_same_f(int a,int b) // a和b是否在同一集合
	{
		return get_f(a)==get_f(b);
	}
	
};