# 第十四届蓝桥杯三月真题刷题训练——第 16 天 （3.19）

## 火星旅行

``````#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e6 + 10;
int s[N * 2];
int o[N], d[N];
int n;
bool ans[N];

signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> o[i] >> d[i];
}
for (int i = 1; i <= n; i++) {
s[i] = o[i] - d[i];
s[i + n] = s[i];
}
for (int i = 1; i <= 2 * n; i++) {
s[i] += s[i - 1];
}
deque<int>qu;
qu.push_back(2 * n + 1);
for (int i = 2 * n; i >= 1; i--) {
if (qu.size() && qu.front() > i + n - 1) qu.pop_front();
if (i < n) {
if (s[qu.front()] - s[i - 1] >= 0) ans[i] = true;
}
while (qu.size() && s[qu.back()] >= s[i - 1]) qu.pop_back();
qu.push_back(i - 1);
}
d[0] = d[n];
for (int i = 1; i <= n; i++) {
s[i] = o[i] - d[i - 1];
s[i + n] = s[i];
}
for (int i = 1; i <= 2 * n; i++) {
s[i] += s[i - 1];
}
qu.clear();
qu.push_back(0);
for (int i = 0; i <= 2 * n - 1; i++) {
if (qu.size() && qu.front() < i - n + 1) qu.pop_front();
if (i + 1 > n) {
if (s[i + 1] - s[qu.front()] >= 0) ans[i - n + 1] = true;
}
while (qu.size() && s[qu.back()] <= s[i + 1]) qu.pop_back();
qu.push_back(i + 1);
}
for (int i = 1; i <= n; i++) {
if (ans[i]) puts("TAK");
else puts("NIE");
}
}
``````

## 推导部分和

``````#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e6 + 10;
int n, m, q;
int p[N];
int sum[N];

int find(int v) {
if (p[v] != v) {
int rt = find(p[v]);
sum[v] += sum[p[v]];
p[v] = rt;
}
return p[v];
}

signed main() {
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) p[i] = i;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
int pa = find(a - 1), pb = find(b);
if (pa != pb) {
p[pb] = pa;
sum[pb] = sum[a - 1] - sum[b] + c;
}
}
while (q--) {
int a, b;
cin >> a >> b;
int pa = find(a - 1), pb = find(b);
if (pa != pb) {
cout << "UNKNOWN" << endl;
}
else {
cout << sum[b] - sum[a - 1] << endl;
}
}
}
``````
...全文
60 3 打赏 收藏 举报

3 条回复

TralSun 03-19
• 打赏
• 举报

• 举报

@TralSun 是你太强了
TralSun 03-19
• 打赏
• 举报

%%%%%%%%%%%%%%%%%%%%%%

4.3w+

2023-03-19 14:23

刷题！