当前位置:网站首页>AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
2022-07-05 09:48:00 【Chels.】
AtCoder Beginner Contest 254
E - Small d and k
题目描述:
给你一个无向图,每个点的度数最多为3,进行Q次询问,每次询问都给
x,k,表示询问以x为起点,距离小于等于k步的点的id的和其中
k<=3
思路:
因为度最多为3,且
k<=3,所以每次询问最多就涉及到27个点,27 * Q也才4e6,复杂度说的过去,我们之间暴力跑bfs就行
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define mod7 1000000007
#define mod9 998244353
#define m_p(a,b) make_pair(a, b)
#define mem(a,b) memset((a),(b),sizeof(a))
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(a) cout << "Debuging...|" << #a << ": " << a << "\n";
typedef long long ll;
typedef pair <int,int> pii;
#define MAX 300000 + 50
int n, m, k, x, y;
vector<int>G[MAX];
bool vis[MAX];
void work(){
cin >> n >> m;
for(int i = 1; i <= m; ++i){
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
cin >> m;
while(m--){
cin >> x >> k;
if(k == 0){
cout << x << endl;
continue;
}
vector<int>v;
queue<pii>q;
q.push(m_p(x, k));
vis[x] = 1;
while(!q.empty()){
auto [u, d] = q.front();q.pop();
v.push_back(u);
// cout << u << ' ' << d << endl;
for(auto v : G[u]){
if(!vis[v] && d){
q.push(m_p(v, d - 1));
vis[v] = 1;
}
}
}
int ans = 0;
for(auto x : v){
ans += x;
vis[x] = 0;
}
cout << ans << endl;
}
}
int main(){
io;
work();
return 0;
}
F - Rectangle GCD
题目描述:
给你两个数组
ar,br,长度都是n,定一个一个n*n的矩阵C,其中C[i][j] = ar[i] + br[j],现在给你Q次询问,每次询问都给你一个小矩形的左上角和右下角,问这个小矩形的所有元素的gcd
思路:
首先要清楚一个序列所有元素的最大公约数等于差分数组的最大公约数 ,即 g c d ( a [ 1 ] , a [ 2 ] , . . . , a [ n ] ) = g c d ( a [ 1 ] , a [ 2 ] − a [ 1 ] , a [ 3 ] − a [ 2 ] , . . . , a [ n ] − a [ n − 1 ] ) gcd(a[1], a[2], ...,a[n]) = gcd(a[1], a[2] - a[1], a[3] - a[2], ... ,a[n] - a[n - 1]) gcd(a[1],a[2],...,a[n])=gcd(a[1],a[2]−a[1],a[3]−a[2],...,a[n]−a[n−1])
所以我们考虑查询区间的第
i行g c d ( a [ i ] + b [ j ] , a [ i + 1 ] + b [ j ] , a [ i + 2 ] + b [ j ] , . . . . ) = g c d ( a [ i ] + b [ j ] , a [ i + 1 ] − a [ i ] , a [ i + 2 ] − a [ i + 1 ] . . . ) gcd(a[i]+b[j], a[i + 1]+b[j], a[i + 2]+b[j],....) = gcd(a[i] + b[j], a[i + 1]-a[i], a[i + 2] - a[i + 1]...) gcd(a[i]+b[j],a[i+1]+b[j],a[i+2]+b[j],....)=gcd(a[i]+b[j],a[i+1]−a[i],a[i+2]−a[i+1]...) 我们先不看
a[i]+b[j],则根据后面的那段的gcd,我们可以通过求gcd(a[i+1]-a[i], a[i+2]-a[i+1]...)获得下图紫色部分的所有的数字的gcd
同理,我们可以利用
gcd(br[i+1]-br[i], br[i+2]-br[i+1]....)获得下面蓝色部分的所有数字的gcd
而我们需要的是从
C[h1][w1]到C[h2][w2]的所有数字的gcd,综合一下图型可以发现左上角的数字没有被计算过,所以我们只需要将上面两个差分区间的gcd和C[h1][w2]也就是A[h1]+B[w1]求一个gcd即可所以我们可以利用st表来维护差分数组的区间gcd
说实话我写的时候真没想这么深,就想到了
gcd(x, y)可以转换成gcd(x-y, x),就觉得是求差分数组的gcd,再加上光求差分数组不够,还得有个x,就加上了左上角的元素三者一起求了个gcd,背了个st表套上去没想到真的过了
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define mod7 1000000007
#define mod9 998244353
#define m_p(a,b) make_pair(a, b)
#define mem(a,b) memset((a),(b),sizeof(a))
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(a) cout << "Debuging...|" << #a << ": " << a << "\n";
typedef long long ll;
typedef pair <int,int> pii;
#define MAX 300000 + 50
int n, m, k, x;
ll ar[MAX];
ll br[MAX];
ll gcd(ll a, ll b){
if(b) return gcd(b, a % b);
else return a;
}
ll lg[MAX];
ll g1[MAX][22];
ll g2[MAX][22];
void init(){
for(int i = 2; i <= n; ++i){
lg[i] = lg[i / 2] + 1;
}
for(int i = 2; i <= n; ++i){
g1[i][0] = ar[i] - ar[i - 1];
g2[i][0] = br[i] - br[i - 1];
}
for(int j = 1; j <= lg[n]; ++j){
for(int i = 2; i + (1 << j) - 1 <= n; ++i){
g1[i][j] = gcd(g1[i][j - 1], g1[i + (1 << (j - 1))][j - 1]);
g2[i][j] = gcd(g2[i][j - 1], g2[i + (1 << (j - 1))][j - 1]);
}
}
}
ll get_gcd(int l, int r, int op){
ll d = lg[r - l + 1];
if(op)return abs(gcd(g1[l][d], g1[r - (1 << d) + 1][d]));
else return abs(gcd(g2[l][d], g2[r - (1 << d) + 1][d]));
}
void work(){
cin >> n >> m;
for(int i = 1; i <= n; ++i)cin >> ar[i];
for(int j = 1; j <= n; ++j)cin >> br[j];
init();
int h1, h2, w1, w2;
while(m--){
cin >> h1 >> h2 >> w1 >> w2;
ll a = 0, b = 0;
if(h1 == h2)a = 0;
else a = get_gcd(h1 + 1, h2, 1);
if(w1 == w2)b = 0;
else b = get_gcd(w1 + 1, w2, 0);
cout << gcd(a, gcd(b, ar[h1] + br[w1])) << endl;
}
}
int main(){
io;
work();
return 0;
}
边栏推荐
- How to use sqlcipher tool to decrypt encrypted database under Windows system
- How to get the STW (pause) time of GC (garbage collector)?
- [论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
- Cross process communication Aidl
- [200 opencv routines] 219 Add digital watermark (blind watermark)
- 美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
- Usage differences between isempty and isblank
- MySQL字符类型学习笔记
- 【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
- Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
猜你喜欢

如何写出高质量的代码?

钉钉、企微、飞书学会赚钱了吗?

《天天数学》连载58:二月二十七日

ConstraintLayout官方提供圆角ImageFilterView

Detailed explanation of the use of staticlayout

Hard core, have you ever seen robots play "escape from the secret room"? (code attached)

Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上

字节跳动面试官:一张图片占据的内存大小是如何计算

mongoDB副本集

Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
随机推荐
高级 OpenCV:BGR 像素强度图
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
QT event filter simple case
能源势动:电力行业的碳中和该如何实现?
The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
《天天数学》连载58:二月二十七日
Apache dolphin scheduler system architecture design
[C language] the use of dynamic memory development "malloc"
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
字节跳动面试官:一张图片占据的内存大小是如何计算
Apache DolphinScheduler 入门(一篇就够了)
.Net之延迟队列
mysql80服务不启动
ConstraintLayout官方提供圆角ImageFilterView
天龙八部TLBB系列 - 单体技能群伤
Lepton 无损压缩原理及性能分析
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
How to judge that the thread pool has completed all tasks?

![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KgJeB0Cd-1656599477903)(/Users/chelsea/Library/Application Support/typora-user-images/image-20220630222632492.png)]](/img/ef/a7ccf6d88729b3e5f3419e30914267.png)