Educational Codeforces Round 6 题解&&代码

A
设坐标(x1,y1)(x2,y2)答案max(abs(x1-x2),abs(y1-y2))
B
预处理出来每个数字要用多少。。暴力就好
如果是10^12也不难(可以数位dp)
C
贪心就好,每次发现重复的就重新计数
写T了一次。。自己的程序弱可以被卡到n^2,考完才发现
D
大概就是暴力?还没写
E
维护一颗线段树,树上有60个标记,压到一个longlong里面可以少个log
大概就是跑一边dfs记录子树在线段树中的位置,然后写一个带标记下放的线段树就好
F

出题人算法是莫队+可持久化trie,但是由于是cf然后又有10s。。。暴力跑了过去。。。

A

//Copyright(c)2016 liuchenrui
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#define mod 1000000007ll
#define inf 1100000000ll
using namespace std;
inline void splay(int &v){
	v=0;char c=0;int p=1;
	while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
	while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
	v*=p;
}
#include<cmath>
int main(){
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	cout<<max(abs(a-c),abs(b-d));
}
B
//Copyright(c)2016 liuchenrui
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#define mod 1000000007ll
#define inf 1100000000ll
using namespace std;
inline void splay(int &v){
	v=0;char c=0;int p=1;
	while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
	while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
	v*=p;
}
const int v[]={6,2,5,5,4,5,6,3,7,6};
int main(){
	//freopen("xxx.in","r",stdin);
	//freopen("xxx.out","w",stdout);
	int a,b,ans=0;
	cin>>a>>b;
	for(int i=a;i<=b;i++){
		int t=i;
		while(t){
			ans+=v[t%10];
			t/=10;
		}
	} 
	cout<<ans<<endl;
}
C

//Copyright(c)2016 liuchenrui
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#define mod 1000000007ll
#define inf 1100000000ll
using namespace std;
inline void splay(int &v){
	v=0;char c=0;int p=1;
	while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
	while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
	v*=p;
}
#include<set>
#include<vector>
set<int>s;
vector< pair<int,int> >ans;
int a[300010];
int main(){
	freopen("xxx.in","r",stdin);
	freopen("xxx.out","w",stdout);
	int n;splay(n);
	for(int i=1;i<=n;i++){
		splay(a[i]);
	}
	for(int i=1;i<=n;i++){
		s.clear();
		for(int j=i;j<=n;j++){
			if(s.find(a[j])!=s.end()){
				ans.push_back(make_pair(i,j));
				i=j;break;
			}
			s.insert(a[j]);
			if(j==n)goto end;
		}
	}
	end:;
	if(ans.size()==0)puts("-1");
	else{
		cout<<ans.size()<<endl;
		ans[ans.size()-1].second=n;
		for(int i=0;i<ans.size();i++){
			printf("%d %d\n",ans[i].first,ans[i].second);
		}
	}
	
}

E
//Copyright(c)2016 liuchenrui
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#define mod 1000000007ll
#define inf 1100000000ll
#define ll long long
using namespace std;
inline void splay(int &v){
	v=0;char c=0;int p=1;
	while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
	while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
	v*=p;
}
struct Edge{
	int to,next;
}edge[800010];
ll val[5000010];
ll mark[5000010];
int size,a[400010],first[400010],last[400010],tot,n,q;
int begin[400010];
void addedge(int x,int y){
	size++;
	edge[size].to=y;
	edge[size].next=first[x];
	first[x]=size;
}
void push_down(int now){
	if(mark[now]){
		mark[now<<1]=mark[now<<1|1]=mark[now];
		val[now]=mark[now];mark[now]=0;
	}
}
void update(int now){
	push_down(now),push_down(now<<1),push_down(now<<1|1);
	val[now]=val[now<<1]|val[now<<1|1];
}
void modify(int l,int r,int w,int L,int R,ll v){
	update(w);
	if(L<=l && r<=R){
		mark[w]=v;push_down(w);return;
	}
	int mid=l+r>>1;
	if(R>=mid+1)modify(mid+1,r,w<<1|1,L,R,v);
	if(L<=mid)modify(l,mid,w<<1,L,R,v);
	update(w);
}
void modify(int pos,int color){
	modify(1,n,1,begin[pos],last[pos],1ll<<(long long)(color-1));
}
ll query(int l,int r,int w,int L,int R){
	update(w);
	if(L<=l && r<=R){
		return val[w];
	}
	int mid=l+r>>1;ll ret=0;
	if(R>=mid+1)ret|=query(mid+1,r,w<<1|1,L,R);
	if(L<=mid)ret|=query(l,mid,w<<1,L,R);
	return ret;
}
int query(int pos){
	ll ret=query(1,n,1,begin[pos],last[pos]),ans=0;
	for(ll i=0;i<=59;i++){
		if(ret>>i&1ll)ans++;
	}
	return ans;
}
void dfs(int now,int fa){
	begin[now]=++tot; 
	for(int u=first[now];u;u=edge[u].next){
		if(edge[u].to!=fa){
			dfs(edge[u].to,now);
		}
	}
	last[now]=tot;
}
void dfs2(int now,int fa){
	modify(now,a[now]);
	for(int u=first[now];u;u=edge[u].next){
		if(edge[u].to!=fa){
			dfs2(edge[u].to,now);
		}
	}
}
int main(){
	splay(n),splay(q);
	for(int i=1;i<=n;i++)splay(a[i]);
	for(int i=1;i<n;i++){
		int x,y;splay(x),splay(y);
		addedge(x,y),addedge(y,x);
	}
	dfs(1,0);
	dfs2(1,0);
	while(q--){
		int op;splay(op);
		if(op==1){
			int pos,v;splay(pos),splay(v);
			modify(pos,v);
		}
		else{
			int pos;splay(pos);
			printf("%d\n",query(pos));
		}
	}
}

F
//Copyright(c)2016 liuchenrui
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
inline void splay(int &v){
	v=0;char c=0;int p=1;
	while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
	while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
	v*=p;
}
int v[1000010],ans[50010];
int n,m;
int a[50010],l[5010],r[5010],b[50010];
int main(){
	freopen("xxx.in","r",stdin);
	freopen("xxx.out","w",stdout);
	for(int i=1;i<=1000000;i++){
		v[i]=v[i-1]^i;
	}
	splay(n),splay(m);
	for(int i=1;i<=n;i++)splay(a[i]);
	for(int i=1;i<=m;i++){
		splay(l[i]),splay(r[i]);
	}
	for(int i=1;i<=n;i++){
		int nowans=0;
		for(int j=i;j<=n;j++){
			nowans=max(nowans,v[a[i]]^v[a[j]]^(min(a[i],a[j])));
			b[j]=nowans;
		}
		for(int j=1;j<=m;j++){
			if(l[j]<=i&&i<=r[j]){
				ans[j]=max(ans[j],b[r[j]]);
			}
		}
	}
	for(int i=1;i<=m;i++){
		printf("%d\n",ans[i]);
	}
}



你可能感兴趣的:(题解,总结,codeforces)