A.
题意很简单,给出两个区间
[
l
,
r
]
[l,r]
[l,r],
[
x
,
y
]
[x,y]
[x,y],问是否存在两个数
a
a
a,
b
b
b,满足
a
a
a在第一个区间,而
b
b
b在第二个区间,且
a
/
b
=
k
a/b = k
a/b=k。
数据范围1e7,暴力遍历区间
[
x
,
y
]
[x,y]
[x,y],计算
b
∗
k
b*k
b∗k能否落在区间一即可。
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int l,r,x,y,k; cin >> l >> r >> x >> y >> k;
for(int i = x; i <= y; ++i){
long long sum = 1ll *i *k;
if(sum >= l && sum <= r){
cout <<"YES" << endl;
return 0;
}
}
cout << "NO"<<endl;
return 0;
}
B.
问小圆是否在外面的圆环中,几何题,看俩圆心距离和半径,建立一个关系就可以了。
#include <bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
Point(int xx,int yy){x =xx,y=yy;}
};
double Cal_dis(Point x,Point y){
return sqrt((x.x - y.x)*(x.x-y.x) + (x.y - y.y)*(x.y - y.y));
}
bool check(double dis,int r,int d,int _r){
if(dis - (r-d) < _r) return false;
if(dis + _r > r) return false;
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int r,d,n; cin >> r >> d >> n;
Point center = Point(0,0);
int cnt = 0;
for(int i = 0; i < n; ++i){
int x,y,_r; cin >> x >> y >> _r;
double dis = Cal_dis(center,Point(x,y));
if(check(dis,r,d,_r))
++cnt;
}cout << cnt << endl;
return 0;
}
C.一个树上的问题,对于每个点来说要找到从根到该节点路径上的数的最大公约数,其中有个操作就是删除该路径中的一个节点(数字变0) .
那么对于每个节点来说只有修改or不修改两种操作,去维护每个节点的最大公约数值即可,建立一个记忆化搜索求解就可以了。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
int a[maxn];
int head[maxn],cnt;
struct Edge{
int u,v,nxt;
}edge[maxn<<1];
inline void addedge(int u,int v){
edge[++cnt] = {u,v,head[u]};
head[u] = cnt;
}
int gcd(int x,int y){
return y == 0? x : gcd(y,x%y);
}
map<int,bool> vis[maxn][2];
int dp[maxn];
void dfs(int u,int fa,int opt,int now){
// u 当前结点,fa u的父节点 opt 是否进行修改操作过 now 当前的gcd值
if(vis[u][opt][now]) return; // 该状态查过了
vis[u][opt][now] = true;
int tt = gcd(now,a[u]); // 这个点不修改,求解一个gcd值
dp[u] = max(dp[u],tt); // 维护最大美丽值
if(!opt) dp[u] = max(dp[u],now); // 如果还没修改过,那么修改u为0,则此时公约数最大为之前的now,所以同样要进行max一遍操作
for(int i = head[u]; ~i; i = edge[i].nxt){
Edge &e = edge[i];
if(e.v == fa) continue;
dfs(e.v,u,opt,tt); // 这一层没用修改操作(保持原样,仅仅gcd当前层)进行递归
if(!opt) dfs(e.v,u,1,now); // 如果没进行过修改,那么递归一个修改后的dfs(也就是假定这层用掉了修改操作)
}
}
int main()
{
memset(head,-1,sizeof(head));
int n; cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 1; i < n; ++i){
int x,y; cin >> x >> y;
addedge(x,y);
addedge(y,x);
}
dfs(1,0,0,0);
for(int i = 1; i <= n; ++i){
cout << dp[i] << ' ';
}cout << endl;
return 0;
}
D.
这题不懂,查了题解理解了之后才明白的。感觉是个不错的题目,顺便回顾一下01字典树的运用。
该问题就是给出一个数组a,然后每次查询xor一个数x,然后对新的数组进行mex查询。
(mex查询具体含义为:查询数组中没出现过的最小值)
题解:
- 应用xor的结合律的性质,每次无需去修改数组,因为 x^ y ^ z = x^ (y^z)
- 假定数组b为查询后xor数x的结果,对于数组a中没出现过的数c,每次查询xor 数x之后,显然应该也是不会出现在数组b当中。因此查询的应该是这些没出现过的数中的最小值。
- 而01字典树可以用于查询xor最值或者xor的和问题。因此这里将a数组中没出现过的数建立一颗字典树,然后对查询的数q,去字典树中查询xor最小值,即为结果。
- 这里构建字典树的时候,需要遍历给定的数的范围大2倍(这个点还有点疑惑!?)
// D
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5+5;
int trie[maxn*30][2];
int cnt = 0;
inline void Insert(int x){
int root = 0;
for(int i = 20; i >= 0; --i){
int tag = (x >> i)&1;
if(!trie[root][tag]) trie[root][tag] = ++cnt;
root = trie[root][tag];
}
}
inline int query(int x){
// 查询异或的最小值
int root = 0,ans = 0;
for(int i = 20; i >= 0; --i){
int tag = (x >> i)&1;
// cout << i << ' ' << tag << endl;
if(trie[root][tag] == 0){
// 相同位的不能走
ans += (1 << i);
// cout << i << ' ' << ans << endl;
root = trie[root][tag ^ 1];
}else root = trie[root][tag];
}
return ans;
}
bool vis[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m; cin >> n >> m;
for(int i = 0; i < n; ++i){
int a; cin >> a;
vis[a] = 1;
}
for(int i = 0; i <= 600000; ++i){
if(!vis[i]) Insert(i);
}
int now = 0;
for(int i = 0; i < m; ++i){
int q; cin >> q;
now ^= q;
cout << query(now) << endl;
}
return 0;
}
E.
关于树的题目题意: 给一棵只有一个根节点为1的树,后续有n个操作,每次添加一个新的节点到树上,然后询问成为直径端点这样的点的个数有多少个
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+5;
int depth[maxn];
int lg[maxn];
int fa[maxn][31];
inline void init(){ // Initiallize
for(int i = 1; i < maxn; ++i) lg[i] = lg[i-1] + (1<<lg[i-1]==i);
}
int lca(int x,int y){
if(depth[x] < depth[y]) swap(x,y);
while(depth[x] > depth[y]){
x = fa[x][lg[depth[x]-depth[y]]-1];
}
if(x == y) return x;
for(int i = lg[depth[x]]-1; i >= 0; --i){
if(fa[x][i]!=fa[y][i]){
x = fa[x][i];
y = fa[y][i];
}
}
return x==y ? x : fa[x][0];
}
void update(int x,int f){
depth[x] = depth[fa[x][0] = f] + 1;
for(int i = 1; i <= lg[depth[x]]-1; ++i){
fa[x][i] = fa[fa[x][i-1]][i-1];
}
}
int dis(int x,int y){
return depth[x] + depth[y] - 2*depth[lca(x,y)];
}
vector<int> s1,s2;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
init();
int n; cin >> n;
update(1,0); s1.push_back(1);
int max_dis = 0;
for(int i = 2; i <= n+1; ++i){
int p; cin >> p;
update(i,p);
int d1 = s1.empty()? 0 : dis(i,s1[0]);
int d2 = s2.empty()? 0 : dis(i,s2[0]);
// cout << "d1 :" << d1 << ' ' << "d2 :" << d2 << endl;
if(max(d1,d2) > max_dis){
max_dis = max(d1,d2);
//cout <<"dis: "<< max_dis << endl;
if(max_dis == d1){
for(auto j : s2){
if(dis(j,i) == d1){
s1.push_back(j);
}
}s2.clear();
}else if(max_dis == d2){
for(auto j : s1){
if(dis(j,i)== d2){
s2.push_back(j);
}
}s1.clear();
}
}
if(max(d1,d2)==max_dis){
( max_dis == d1? s2:s1).push_back(i);
}//cout << s1.size() << ' ' << s2.size() << endl;
cout << s1.size() + s2.size() << endl;
}
return 0;
}