当前位置:网站首页>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;
}
边栏推荐
- Windows uses commands to run kotlin
- Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
- Apache DolphinScheduler 系统架构设计
- (1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
- Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
- Kotlin compose and native nesting
- La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
- [论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
- Matrix processing practice
- QT realizes signal transmission and reception between two windows
猜你喜欢

如何獲取GC(垃圾回收器)的STW(暫停)時間?

mysql80服务不启动

Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft

Cross process communication Aidl

Kotlin Compose 多个条目滚动

RMS TO EAP通过MQTT简单实现

Implementation of smart home project
![[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems](/img/6c/5b14f47503033bc2c85a259a968d94.png)
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems

Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process

Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
随机推荐
让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
ArcGIS Pro creating features
@JsonAdapter注解使用
Getting started with Apache dolphin scheduler (one article is enough)
Generics, generic defects and application scenarios that 90% of people don't understand
Z-blog template installation and use tutorial
Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
Constraintlayout officially provides rounded imagefilterview
RMS to EAP is simply implemented through mqtt
一种用于干式脑电图的高密度256通道电极帽
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
. Net delay queue
Wechat applet - simple diet recommendation (2)
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
Android SQLite database encryption
Using directive in angualr2 to realize that the picture size changes with the window size
Mysql80 service does not start
如何獲取GC(垃圾回收器)的STW(暫停)時間?
@SerializedName注解使用

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