原谅我的码风 /jk
快读快写
//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');
}一维树状数组
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;
}
};二维树状数组
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;
}
};并查集
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);
}
};