[kuangbin帶你飛]線段樹

目錄

A:hdu1116-敵兵佈陣——單點增減,區間查詢

B:hdu1754-I Hate It ——單點替換,區間查詢

C:poj3468-A Simple Problem——區間增減,區間查詢

D:poj2528-Mayor’s posters——區間覆蓋,離散化

E:hdu1698-Just a Hook——區間替換,區間查詢

F:zoj1610-Count the Colors——區間覆蓋,區間查詢

G:poj3264-Balanced Lineup——區間差值查詢

H:hdu4027-Can you answer——單點修改,剪枝,區間查詢

A:hdu1116-敵兵佈陣

回目錄

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+9;
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
int t[N];
inline void pushup(int id){t[id]=t[id<<1]+t[id<<1|1];}
inline void build(int id,int L,int R)
{
	if(L==R){
		cin>>t[id];
		return;
	}
	build(lson);
	build(rson);
	pushup(id);
}
inline void update(int id,int L,int R,int a,int c)
{
	if(L==R){
		t[id]+=c;
		return;
	}
	if(a<=mid)update(lson,a,c);
	else update(rson,a,c);
	pushup(id);
}
inline int query(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R)return t[id];
	int ans=0;
	if(a<=mid)ans+=query(lson,a,b);
	if(b>mid)ans+=query(rson,a,b);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	int T,k=0;
	string op;
	cin>>T;
	while(T--){
		cout<<"Case "<<++k<<":"<<endl;
		int n;
		memset(t,0,sizeof(t));
		cin>>n;
		build(1,1,n);
		while(cin>>op){
			if(op=="End")break;
			else if(op=="Add"){
				int a,c;
				cin>>a>>c;
				update(1,1,n,a,c);
			}
			else if(op=="Sub"){
				int a,c;
				cin>>a>>c;
				update(1,1,n,a,-c);
			}
			else{
				int a,b;
				cin>>a>>b;
				cout<<query(1,1,n,a,b)<<endl;
			}
		}
	}
	return 0;
}

B:hdu1754-I Hate It

回目錄

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=8e5+9;
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
int t[N];
inline void pushup(int id)
{
	t[id]=max(t[id<<1],t[id<<1|1]);
}
inline void build(int id,int L,int R)
{
	if(L==R){
		cin>>t[id];
		return;
	}
	build(lson);
	build(rson);
	pushup(id);
}
inline void update(int id,int L,int R,int a,int b)
{
	if(L==R){
		t[id]=b;
		return;
	}
	if(a<=mid)update(lson,a,b);
	else update(rson,a,b);
	pushup(id);
}
inline int query(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R)return t[id];
	int ans=0;
	if(a<=mid)ans=max(ans,query(lson,a,b));
	if(b>mid)ans=max(ans,query(rson,a,b));
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	int n,m,a,b;
	char op;
	while(cin>>n>>m){
		build(1,1,n);
		while(m--){
			cin>>op>>a>>b;
			if(op=='U')update(1,1,n,a,b);
			else cout<<query(1,1,n,a,b)<<endl;
		}
	}
	return 0;
}

C:poj3468-A Simple Problem

回目錄

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const LL N=4e5+9;
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
LL t[N],lazy[N];
inline void pushup(LL id){t[id]=t[id<<1]+t[id<<1|1];}
inline void pushdown(LL id,LL L,LL R)
{
	if(lazy[id]){
		lazy[id<<1]+=lazy[id];
		lazy[id<<1|1]+=lazy[id];
		t[id<<1]+=lazy[id]*(mid-L+1);
		t[id<<1|1]+=lazy[id]*(R-mid);
		lazy[id]=0;
	}
}
inline void build(LL id,LL L,LL R)
{
	lazy[id]=0;
	if(L==R){
		cin>>t[id];
		return;
	}
	build(lson);
	build(rson);
	pushup(id);
}
inline void update(LL id,LL L,LL R,LL a,LL b,LL c)
{
	if(a<=L&&b>=R){
		lazy[id]+=c;
		t[id]+=c*(R-L+1);
		return;
	}
	pushdown(id,L,R);
	if(a<=mid)update(lson,a,b,c);
	if(b>mid)update(rson,a,b,c);
	pushup(id);
}
inline LL query(LL id,LL L,LL R,LL a,LL b)
{
	if(a<=L&&b>=R)return t[id];
	pushdown(id,L,R);
	LL ans=0;
	if(a<=mid)ans+=query(lson,a,b);
	if(b>mid)ans+=query(rson,a,b);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	LL n,m,a,b,c;
	char op;
	cin>>n>>m;
	build(1,1,n);
	while(m--){
		cin>>op>>a>>b;
		if(op=='C'){
			cin>>c;
			update(1,1,n,a,b,c);
		}
		else cout<<query(1,1,n,a,b)<<endl;
	}
	return 0;
}

D:poj2528-Mayor’s posters

回目錄

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
using namespace std;
const int N=4e4+9;
int t[N*4],A[N*3];
pair<int,int> p[N];
set<int> ans;
inline void pushdown(int id)
{
	if(t[id]!=-1){
		t[id<<1]=t[id<<1|1]=t[id];
		t[id]=-1;
		return;
	}
}
inline void build(int id,int L,int R)
{
	t[id]=-1;
	if(L==R)return;
	build(lson);
	build(rson);
}
inline void update(int id,int L,int R,int a,int b,int c)
{
	if(a<=L&&b>=R){
		t[id]=c;
		return;
	}
	pushdown(id);
	if(a<=mid)update(lson,a,b,c);
	if(b>mid)update(rson,a,b,c);
}
inline void query(int id,int L,int R,int a,int b)
{
	if(L==R){
		if(t[id]!=-1)ans.insert(t[id]);
		return;
	}
	pushdown(id);
	query(lson,a,b);
	query(rson,a,b);
}
int main()
{
	ios::sync_with_stdio(false);
	int T,n,a,b,num,m;
	cin>>T;
	while(T--){
		cin>>n;
		num=0;
		for(int i=1;i<=n;++i){
			cin>>p[i].first>>p[i].second;
			A[++num]=p[i].first;
			A[++num]=p[i].second;
		}
		sort(A+1,A+num+1);
		m=unique(A+1,A+num+1)-A-1;
		for(int i=m;i>=2;--i)
			if(A[i]-A[i-1]>1)A[++m]=A[i]-1;/*解決1 10,1 4,6 10離散化後的錯誤情況*/
		sort(A+1,A+m+1);
		build(1,1,m);
		for(int i=1;i<=n;++i){
			a=lower_bound(A+1,A+m+1,p[i].first)-A;
			b=lower_bound(A+1,A+m+1,p[i].second)-A;
			update(1,1,m,a,b,i);
		}
		query(1,1,m,1,m);
		cout<<ans.size()<<endl;
		ans.clear();	
	}
	return 0;
}

E:hdu1698-Just a Hook

回目錄

#include<iostream>
#include<cmath>
using namespace std;
const int N=4e5+9;
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
int t[N],lazy[N];
inline void pushup(int id){t[id]=t[id<<1]+t[id<<1|1];}
inline void pushdown(int id,int L,int R)
{
	if(lazy[id]){
		lazy[id<<1]=lazy[id];
		lazy[id<<1|1]=lazy[id];
		t[id<<1]=lazy[id]*(mid-L+1);
		t[id<<1|1]=lazy[id]*(R-mid); 
		lazy[id]=0;
	}
}
inline void build(int id,int L,int R)
{
	lazy[id]=0;
	if(L==R){
		t[id]=1;
		return;
	}
	build(lson);
	build(rson);
	pushup(id);
}
inline void update(int id,int L,int R,int a,int b,int c)
{
	if(a<=L&&b>=R){
		t[id]=c*(R-L+1);
		lazy[id]=c;
		return;
	}
	pushdown(id,L,R);
	if(a<=mid)update(lson,a,b,c);
	if(b>mid)update(rson,a,b,c);
	pushup(id);
}
inline int query(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R)return t[id];
	pushdown(id,L,R);
	int ans=0;
	if(a<=mid)ans+=query(lson,a,b);
	if(b>mid)ans+=query(rson,a,b);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	int T,n,m,a,b,c;
	cin>>T;
	for(int k=1;k<=T;++k){
		cin>>n>>m;
		build(1,1,n);
		while(m--){
			cin>>a>>b>>c;
			update(1,1,n,a,b,c);
		}
		cout<<"Case "<<k<<": The total value of the hook is "<<query(1,1,n,1,n)<<"."<<endl;
	}
	return 0;
}

F:zoj1610-Count the Colors

回目錄

#include<iostream>
#include<cstring>
#include<map>
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
using namespace std; 
const int N=1e5+9;
int t[N<<2],A[N];
map<int,int> ans;
inline void pushdown(int id)
{
	if(t[id]!=-1){
		t[id<<1]=t[id<<1|1]=t[id];
		t[id]=-1;
	}
}
inline void build(int id,int L,int R)
{
	t[id]=-1;
	if(L==R)return;
	build(lson);
	build(rson);
}
inline void update(int id,int L,int R,int a,int b,int c)
{
	if(a<=L&&b>=R){
		t[id]=c;
		return;
	}
	pushdown(id);
	if(a<=mid)update(lson,a,b,c);
	if(b>mid)update(rson,a,b,c);
}
inline void query(int id,int L,int R,int a,int b)
{
	if(L==R){
		if(t[id]!=-1)A[L]=t[id];
		return;
	}
	pushdown(id);
	query(lson,a,b);
	query(rson,a,b);
}
int main()
{
	ios::sync_with_stdio(false);
	int m,a,b,c,n=8000;
	while(cin>>m){
		memset(A,-1,sizeof(A));
		build(1,1,n);
		while(m--){
			cin>>a>>b>>c;
			update(1,1,n,a+1,b,c);/*a+1的原因是建樹是按端點建的,而題目是按區間染色,
									看第一組樣例分別畫線段和端點比較比較*/ 
		}
		query(1,1,n,1,n);
		for(int i=0;i<=n;++i)
			if(A[i]!=-1&&A[i]!=A[i+1])
				ans[A[i]]++;
		map<int,int>:: iterator it;
		for(it=ans.begin();it!=ans.end();++it)
			cout<<(*it).first<<" "<<(*it).second<<endl;
		cout<<endl;
		ans.clear();
	}
	return 0;
}

G:poj3264-Balanced Lineup

回目錄

用cin超時。。。scanf水過

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+9;
#define inf 0x3f3f3f3f
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
int t1[N],t2[N];
inline void pushup(int id)
{
	t1[id]=max(t1[id<<1],t1[id<<1|1]);
	t2[id]=min(t2[id<<1],t2[id<<1|1]);
}
inline void build(int id,int L,int R)
{
	if(L==R){
		scanf("%d",&t1[id]);
		t2[id]=t1[id];
		return;
	}
	build(lson);
	build(rson);
	pushup(id);
}
inline int query1(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R)return t1[id];
	int ans=-inf;
	if(a<=mid)ans=max(ans,query1(lson,a,b));
	if(b>mid)ans=max(ans,query1(rson,a,b));
	return ans;
}
inline int query2(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R)return t2[id];
	int ans=inf;
	if(a<=mid)ans=min(ans,query2(lson,a,b));
	if(b>mid)ans=min(ans,query2(rson,a,b));
	return ans;
}
int main()
{
	int n,m,a,b;
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--){
		scanf("%d%d",&a,&b);
		printf("%d\n",query1(1,1,n,a,b)-query2(1,1,n,a,b));
	}
	return 0;
}

H:hdu4027-Can you answer

回目錄

錯了好幾次,都是sqrtf()函數的鍋,精度不夠

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=4e5+99;
LL t[N];
#define mid ((L+R)>>1)
#define lson id<<1,L,mid
#define rson id<<1|1,mid+1,R
inline void pushup(int id){t[id]=t[id<<1]+t[id<<1|1];}
inline void build(int id,int L,int R)
{
	if(L==R){
		cin>>t[id];
		return;
	}
	build(lson);
	build(rson);
	pushup(id);
}
inline void update(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R&&t[id]==R-L+1)return;
	if(L==R){
		t[id]=sqrt(t[id]);
		return;
	}
	if(a<=mid)update(lson,a,b);
	if(b>mid)update(rson,a,b);
	pushup(id);
}
inline LL query(int id,int L,int R,int a,int b)
{
	if(a<=L&&b>=R)return t[id];
	LL ans=0;
	if(a<=mid)ans+=query(lson,a,b);
	if(b>mid)ans+=query(rson,a,b);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	LL n,m,op,a,b,k=0;
	while(cin>>n){
		build(1,1,n);
		cout<<"Case #"<<++k<<":"<<endl;
		cin>>m;
		while(m--){
			cin>>op>>a>>b;
			if(a>b)swap(a,b);
			if(op==0)update(1,1,n,a,b);
			else cout<<query(1,1,n,a,b)<<endl;
		}
		cout<<endl;
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章