ABC435 题解(A~F)
A - Triangular Number
有
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 5005;
constexpr int P = 998244353;
int n, ans;
signed main() {
ios::sync_with_stdio(0);
cin >> n;
cout << ((n+1)*n/2) << endl;
return 0;
}
B - No-Divisible Range
枚举即可,前缀和优化求和。
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 5005;
constexpr int P = 998244353;
int n, a[N], s[N], ans;
signed main() {
ios::sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
s[i] = a[i] + s[i-1];
}
for(int l = 1; l <= n; l ++){
for(int r = l; r <= n; r ++){
const int sm = s[r] - s[l-1];
bool flag = true;
for(int i = l; i <= r; i ++)
if(sm % a[i] == 0)
flag = false;
if(flag)
++ ans;
}
}
cout << ans << endl;
return 0;
}
C - Domino
维护一个变量
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 500005;
constexpr int P = 998244353;
int n, a[N], r = 1;
signed main() {
ios::sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i <= n and i <= r; i ++)
r = max(r, i + a[i] - 1);
cout << min(r, n) << endl;
return 0;
}
D - Reachability Query 2
考虑在一个点被涂黑以后会导致哪些点的答案改变,我们可以建反图遍历该点能到达的所有点,所有找到的点都打上标记。同时,不难发现,如果当前遍历到的点已经被打上标记,那么它所能到的点也一定已经被打标记,此时退出即可。
这样一来,每个点我们只会遍历到一次,时间复杂度
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 500005;
constexpr int P = 998244353;
int n, m, q;
bool vis[N];
vector<int> to[N];
void dfs(const int &p){
if(vis[p])
return;
vis[p] = true;
for(auto v: to[p])
dfs(v);
}
signed main() {
int u, v, op;
ios::sync_with_stdio(0);
cin >> n >> m;
while(m --){
cin >> v >> u;
to[u].push_back(v);
}
cin >> q;
while(q --){
cin >> op >> v;
if(op == 1)
dfs(v);
else
cout << (vis[v]? "Yes": "No") << endl;
}
return 0;
}
E - Cover query
这里提供一个比较猎奇但是非常好用的方法,我们直接开一颗下标从
可以加一点优化,如果当前节点已经被全部赋值为
每次操作最多遍历到
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 200005;
int n, q, l, r;
struct Node{
int l, r, sm;
bool blk;
}nd[N<<5];
int cntnd = 1;
void upd(const int &L, const int &R, const int &p, const int &l, const int &r){
if(nd[p].blk)
return;
if(L <= l and r <= R){
nd[p].blk = true;
nd[p].sm = r - l + 1;
return;
}
const int mid = ((l + r) >> 1);
if(L > mid){
if(not nd[p].r) nd[p].r = ++cntnd;
upd(L, R, nd[p].r, mid+1, r);
}else if(R <= mid){
if(not nd[p].l) nd[p].l = ++cntnd;
upd(L, R, nd[p].l, l, mid);
}else{
if(not nd[p].l) nd[p].l = ++cntnd;
if(not nd[p].r) nd[p].r = ++cntnd;
upd(L, R, nd[p].l, l, mid);
upd(L, R, nd[p].r, mid+1, r);
}
nd[p].sm = nd[nd[p].l].sm + nd[nd[p].r].sm;
}
signed main() {
ios::sync_with_stdio(0);
cin >> n >> q;
for(int i = 1; i <= q; i ++){
cin >> l >> r;
upd(l, r, 1, 1, n);
cout << n - nd[1].sm << endl;
}
return 0;
}
F - Cat exercise
假设当前耄耋在连续区间
- 他一定到达世界最高峰
- 一个区间的子集一定没有这个区间优,因为通过删塔一定也可以将耄耋赶到这个子集里面
- 所以下一步的区间不是在最高点左侧就是右侧
我们假设这个最高点为
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 200005;
int n, a[N], f[N][20], lg[N];
inline int maxx(const int &x, const int &y) {return a[x] > a[y]? x: y; }
inline int getmax(const int &l, const int &r){
const int j = lg[r - l + 1];
return maxx(f[l][j], f[r-(1<<j)+1][j]);
}
int dfs(const int &l, const int &r){
const int mid = getmax(l, r);
int rtn = 0;
if(l < mid){
const int nw = mid - getmax(l, mid-1) + dfs(l, mid-1);
rtn = max(rtn, nw);
}
if(mid < r){
const int nw = getmax(mid+1, r) - mid + dfs(mid+1, r);
rtn = max(rtn, nw);
}
return rtn;
}
signed main() {
ios::sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
f[i][0] = i;
}
lg[1] = 0;
for(int i = 2; i <= n; i ++)
lg[i] = lg[i>>1] + 1;
for(int j = 1; j <= lg[n]; j ++)
for(int i = 1; i + (1 << j) - 1 <= n; i ++)
f[i][j] = maxx(f[i][j-1], f[i+(1<<(j-1))][j-1]);
cout << dfs(1, n) << endl;
return 0;
}