清北提高组基础班Day2

一、栈

#include<stack>
using namespace std;
stack<int> sta;
sta.top();
sta.pop();
sta.push(x);
sta.size();
sta.empty();
while (sta.size())
	sta.pop();

二、队列

#include<queue>
using namespace std;
queue<int> que;
que.push(x);
que.pop();
que.front();
que.size();
que.empty();

三、ST表

cin >> n;
for (int a=1;a<=n;a++)
	cin >> z[a];

for (int a=1;a<=n;a++)
	f[a][0] = z[a];

for (int a=1,b=2;b<=n;a++,b=b<<1)
	for (int c=1;c+b-1<=n;c++)
		f[c][a] = min(f[c][a-1],f[c+(b>>1)][a-1]);

for (int a=1;(1<<a)<=n;a++)
	er[1<<a] = a;

for (int a=1;a<=n;a++)
	if (!er[a]) er[a]=er[a-1];

cin >> m;
for (int a=1;a<=m;a++)
{
	int l,r;
	cin >> l >> r;
	int len = r-l+1;
	int j = er[len];
	int ans = min ( f[l][j], f[r-(1<<j)+1][j]);
	cout << ans << endl;
}

四、区间前缀和求和

cin >> n;
for (int a=1;a<=n;a++)
	cin >> z[a];
for (int a=1;a<=n;a++)
	sum[a] = sum[a-1] + z[a];
cin >> m;
for (int a=1;a<=m;a++)
{
	int l,r;
	cin >> l >> r;
	cout << sum[r]-sum[l-1] << endl;
}

五、单调队列

int front=1,tail=0,q[2333][2];

void push(int p)
{
	while (front<=tail && q[tail][1] > z[p])
		tail --;
	tail ++;
	q[tail][0] = p;
	q[tail][1] = z[p];
}

void pop(int p)
{
	if (q[front][0] == p)
		front ++;
}

cin >> n >> k;
for (int a=1;a<=n;a++)
	cin >> z[a];

for (int a=1;a<=k;a++)
	push(a);

cout << q[front][1] << endl;

for (int a=k+1;a<=n;a++)
{
	push(a);
	pop(a-k);
	cout << q[front][1] << endl;
}

六、堆

#include<queue>
using namespace std;

priority_queue<int> heap;

heap.push(-x);//log
heap.top();
heap.pop();//log
heap.size();
heap.empty();

实现小根堆

struct rec {
	int p,v;
	rec() {}
	rec(int a,int b) {
		p=a;v=b;
	}
};

bool operator<(const rec &a,const rec &b)
{
	return a.v<b.v;
}

priority_queue<rec> heap;

bool del[1000];

cin >> n >> k;
for (int a=1;a<=n;a++)
	cin >> z[a];

for (int a=1;a<=k;a++)
	heap.push(rec(a,z[a]));

cout << heap.top().v << endl;

for (int a=k+1;a<=n;a++)
{
	heap.push(rec(a,z[a]));
	del[a-k]=true;
	while (del[heap.top().p])
		heap.pop();
	cout << heap.top().v << endl;
}

七、并查集

int f[2333];

for (int a=1;a<=n;a++)
	f[a]=a;

int getf(int p)
{
	if (f[p] == p) return p;
	else return f[p]=getf(f[p]);
}

bool query(int p1,int p2)
{
	return getf(p1) == getf(p2);
}

void merge(int p1,int p2)
{
	f[getf(p1)] = getf(p2);
}

八、树状数组

int y[2333];

int lb(int x)//lowbit
{
	return x&(-x);
}

int query(int p)
{
	int ans=0;
	for (;p;p-=lb(p))
		ans+=y[p];
	return ans;
}

void modify(int p,int v)
{
	for (;p<=n;p+=lb(p))
		y[p]+=v;
}

cin >> n >> m;
for (int a=1;a<=n;a++)
{
	int v;
	cin >> v;
	modify(a,v);
}

for (int a=1;a<=m;a++)
{
	int op;
	cin >> op;
	if (op==1) {
		int l,r;
		cin >> l >> r;
		cout << query(r)-query(l-1) << endl;
	}
	else {
		int p,v;
		cin >> p >> v;
		modify(p,v);
	}
}

九、线段树

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=100010;

int y[maxn];
int z[maxn<<2];

void update(int rt)
{
	z[rt]=z[rt<<1]+z[rt<<1|1];
}

void build(int l,int r,int rt)
{
	if (l==r)
	{
		z[rt]=y[l];
		return;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	update(rt);
}

void modify(int l,int r,int rt,int p,int v)
{
	if (l==r)
	{
		z[rt]+=v;
		return;
	}
	int m=(l+r)>>1;
	if (p<=m) modify(lson,p,v);
	else modify(rson,p,v);
	update(rt);
}

int query(int l,int r,int rt,int nowl,int nowr)
{
	if (nowl<=l && r<=nowr) return z[rt];
	int m=(l+r)>>1;
	if (nowl<=m)
	{
		if (m<nowr) return query(lson,nowl,nowr)+query(rson,nowl,nowr);
		else return query(lson,nowl,nowr);
	}
	else return query(rson,nowl,nowr);
}

cin >> n >> m;
for (int a=1;a<=n;a++)
	cin >> y[a];

build(1,n,1);
for (int a=1;a<=m;a++)
{
	int op;
	cin >> op;
	if (op == 1)
	{
		int p,v;
		cin >> p >> v;
		modify(1,n,1,p,v);
	}
	else {
		int l,r;
		cin >> l >> r;
		cout << query(1,n,1,l,r) << endl;
	}
}

添加染色操作

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=100010;

int y[maxn];
int z[maxn<<2];
int col[maxn<<2];

void update(int rt)
{
	z[rt]=z[rt<<1]+z[rt<<1|1];
}

void color(int l,int r,int rt,int v)
{
	z[rt] = z[rt] + (r-l+1)*v;
	col[rt] = col[rt] + v;
}

void push_col(int l,int r,int rt)
{
	if (col[rt]) {
		int m=(l+r)>>1;
		color(lson,col[rt]);
		color(rson,col[rt]);
		col[rt]=0;
	}
}

void build(int l,int r,int rt)
{
	if (l==r)
	{
		z[rt]=y[l];
		return;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	update(rt);
}

void modify(int l,int r,int rt,int nowl,int nowr,int v)
{
	if (nowl <= l && r <= nowr) {
		color(l,r,rt,v);
		return;
	}
	push_col(l,r,rt);
	int m=(l+r)>>1;
	if (nowl<=m) modify(lson,nowl,nowr,v);
	if (m<nowr) modify(rson,nowl,nowr,v);
	update(rt);
}

int query(int l,int r,int rt,int nowl,int nowr)
{
	if (nowl<=l && r<=nowr) return z[rt];
	push_col(l,r,rt);
	int m=(l+r)>>1;
	if (nowl<=m)
	{
		if (m<nowr) return query(lson,nowl,nowr)+query(rson,nowl,nowr);
		else return query(lson,nowl,nowr);
	}
	else return query(rson,nowl,nowr);
}

cin >> n >> m;
for (int a=1;a<=n;a++)
	cin >> y[a];

build(1,n,1);
for (int a=1;a<=m;a++)
{
	int op;
	cin >> op;
	if (op == 1)
	{
		int l,r,v;
		cin >> l >> r >> v;
		modify(1,n,1,l,r,v);
	}
	else {
		int l,r;
		cin >> l >> r;
		cout << query(1,n,1,l,r) << endl;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值