Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
for _ in range(int(input())):
x = int(input())
a = [0] + [int(x) for x in input().split()]
print("YES" if a[x] != 0 and a[a[x]] != 0 else "NO")
Идея: BledDest
Разбор
Tutorial is loading...
Решение (vovuh)
n, m = map(int, input().split())
a = list(map(int, input().split()))
l = [0] + [max(0, a[i] - a[i + 1]) for i in range(n - 1)]
r = [0] + [max(0, a[i] - a[i - 1]) for i in range(1, n)]
for i in range(n - 1):
l[i + 1] += l[i]
r[i + 1] += r[i]
for _ in range(m):
s, t = map(int, input().split())
if s < t:
print(l[t - 1] - l[s - 1])
else:
print(r[s - 1] - r[t - 1])
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
auto check = [](const string& s) {
int bal = 0;
for (char c : s) {
if (c == '(') ++bal;
if (c == ')') --bal;
if (bal < 0) return false;
}
return bal == 0;
};
ios::sync_with_stdio(false); cin.tie(0);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
vector<int> pos;
int op = s.size() / 2, cl = s.size() / 2;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '?') pos.push_back(i);
if (s[i] == '(') --op;
if (s[i] == ')') --cl;
}
for (int i = 0; i < pos.size(); ++i) {
if (i < op) s[pos[i]] = '(';
else s[pos[i]] = ')';
}
bool ok = true;
if (op > 0 && cl > 0) {
swap(s[pos[op - 1]], s[pos[op]]);
if (check(s)) ok = false;
}
cout << (ok ? "YES\n" : "NO\n");
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<int> a(m);
forn(i, m) scanf("%d", &a[i]);
int l = 0;
while ((1 << l) <= m) ++l;
vector<vector<int>> st(l, vector<int>(m));
forn(i, m) st[0][i] = a[i];
for (int j = 1; j < l; ++j) forn(i, m){
st[j][i] = st[j - 1][i];
if (i + (1 << (j - 1)) < m)
st[j][i] = max(st[j][i], st[j - 1][i + (1 << (j - 1))]);
}
vector<int> pw(m + 1, 0);
for (int i = 2; i <= m; ++i) pw[i] = pw[i / 2] + 1;
auto get = [&](int l, int r){
if (l > r) swap(l, r);
++r;
int len = pw[r - l];
return max(st[len][l], st[len][r - (1 << len)]);
};
int q;
scanf("%d", &q);
forn(_, q){
int xs, ys, xf, yf, k;
scanf("%d%d%d%d%d", &xs, &ys, &xf, &yf, &k);
--xs, --ys, --xf, --yf;
if (ys % k != yf % k || xs % k != xf % k){
puts("NO");
continue;
}
int mx = (n - xs - 1) / k * k + xs;
puts(get(ys, yf) <= mx ? "YES" : "NO");
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 13;
int n;
int a[N], b[N];
vector<int> g[N];
set<int> vals[N];
void init(int v, int p) {
b[v] = a[v];
if (p != -1) b[v] ^= b[p];
for (int u : g[v]) if (u != p)
init(u, v);
}
int ans;
void calc(int v, int p) {
bool bad = false;
vals[v].insert(b[v]);
for (int u : g[v]) if (u != p) {
calc(u, v);
if (vals[v].size() < vals[u].size()) vals[v].swap(vals[u]);
for (int x : vals[u]) bad |= vals[v].count(x ^ a[v]);
for (int x : vals[u]) vals[v].insert(x);
vals[u].clear();
}
if (bad) {
ans += 1;
vals[v].clear();
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x; --y;
g[x].push_back(y);
g[y].push_back(x);
}
init(0, -1);
calc(0, -1);
cout << ans << '\n';
}
Идея: BledDest
Разбор
Tutorial is loading...