当前位置:网站首页>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;
}
边栏推荐
- 苹果 5G 芯片研发失败?想要摆脱高通为时过早
- Using directive in angualr2 to realize that the picture size changes with the window size
- mysql80服务不启动
- Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
- Usage differences between isempty and isblank
- Cross process communication Aidl
- Applet image height adaptation and setting text line height
- NCP1342芯片替代料PN8213 65W氮化镓充电器方案
- [论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
- Detailed explanation of the use of staticlayout
猜你喜欢
[system design] index monitoring and alarm system
《天天数学》连载58:二月二十七日
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
一种用于干式脑电图的高密度256通道电极帽
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
[200 opencv routines] 219 Add digital watermark (blind watermark)
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
MySQL character type learning notes
QT realizes signal transmission and reception between two windows
随机推荐
RMS to EAP is simply implemented through mqtt
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
ConstraintLayout官方提供圆角ImageFilterView
Openes version query
Design and exploration of Baidu comment Center
Windows uses commands to run kotlin
高级 OpenCV:BGR 像素强度图
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
如何獲取GC(垃圾回收器)的STW(暫停)時間?
Coordinate system of view
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
Interview: how does the list duplicate according to the attributes of the object?
Fluent development: setting method of left and right alignment of child controls in row
钉钉、企微、飞书学会赚钱了吗?
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
Implementation of smart home project
Apache DolphinScheduler 入门(一篇就够了)
Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
StaticLayout的使用详解
剪掉ImageNet 20%数据量,模型性能不下降!Meta斯坦福等提出新方法,用知识蒸馏给数据集瘦身...