signed main() {
int T = 1;
T = read();
while (T--) {
vector<int> a(5);
for (int i = 1; i <= 4; ++i) a[i] = read();
int t1 = a[1] * a[4], t2 = a[2] * a[3];
t1 > t2? puts("S"): puts("Y");
}
return 0;
}
signed main() {
int T = 1;
T = read();
while (T--) {
string s1, s2; cin >> s1 >> s2;
bool f = 1;
for (auto it: s1) {
if (s2.find(it) == -1) {
f = 0;
break;
}
}
if (s2.size() > 1) f? puts("10"): puts("0");
else f? puts("10"): puts("0");
}
return 0;
}
signed main() {
int T = 1;
// T = read();
while (T--) {
int n = read(), m = read();
vector<int> a(n + 1), pref(n + 1);
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1; i <= n; ++i) {
if (a[i] ^ a[i - 1]) pref[i] = 1;
pref[i] += pref[i - 1];
}
while (m--) {
int l = read(), r = read();
writeln(pref[r] - pref[l - 1] + (a[l - 1] == a[l]));
}
}
return 0;
}
string mp[SN];
int n, m;
int d[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};
bool vis[SN][SN];
tuple<int, int, int, int, int> dfs(int x, int y) {
int x1 = x, y1 = y, x2 = x, y2 = y, sum1 = 1;
for (int i = 0; i < 4; ++i) {
int xx = x + d[i][0], yy = y + d[i][1];
if (xx < 0 || yy < 0 || xx >= n || yy >= m || vis[xx][yy] || mp[xx][yy] == '*') continue;
vis[xx][yy] = 1;
auto [xs1, ys1, xs2, ys2, sum] = dfs(xx, yy);
x1 = min(x1, xs1), y1 = min(y1, ys1);
x2 = max(x2, xs2), y2 = max(y2, ys2);
sum1 += sum;
}
return {x1, y1, x2, y2, sum1};
}
signed main() {
int T = 1;
// T = read();
while (T--) {
n = read(), m = read();
for (int i = 0; i < n; ++i) cin >> mp[i];
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < mp[i].size(); ++j) {
if (mp[i][j] ^ '*' && !vis[i][j]) {
vis[i][j] = 1;
auto [x1, y1, x2, y2, sum] = dfs(i, j);
if ((x2 - x1 + 1) * (y2 - y1 + 1) == sum) ++ans;
}
}
}
write(ans);
}
return 0;
}
线段树 c o d e code code
vector<int> w(N), d(N), prefd(N), prefw(N);
struct p {
int l, r, Min;
}tr[N << 2];
void pushup(int i) { tr[i].Min = min (tr[ls].Min, tr[rs].Min); }
void build(int i, int l, int r) {
tr[i] = {l, r, (int)1e18};
if (l == r) {
tr[i].Min = prefd[l];
return ;
}
int mid = (l + r) >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
pushup(i);
}
int query(int i, int l, int r) {
if (tr[i].l >= l && tr[i].r <= r) return tr[i].Min;
int res = 1e18;
if (tr[ls].r >= l) res = min(res, query(ls, l, r));
if (tr[rs].l <= r) res = min(res, query(rs, l, r));
return res;
}
signed main() {
int T = 1;
// T = read();
while (T--) {
int n = read(), W = read();
for (int i = 1; i <= n; ++i) {
prefw[i] = w[i] = read();
prefw[i] += prefw[i - 1];
}
for (int i = 1; i <= n; ++i) {
prefd[i] = d[i] = read();
prefd[i] += prefd[i - 1];
}
build(1, 1, n);
int res = 0, ans = -1e18, ww = 0;
for (int i = 1; i <= n; ++i) {
ww += w[i], res += d[i];
if (ww >= W) {
int pos = upper_bound(prefw.begin() + 1, prefw.begin() + i + 1, ww - W) - prefw.begin();
if (!--pos) continue;
ans = max({ans, res - query(1, 1, pos), res});
}
}
write(ans);
}
return 0;
}
双指针 c o d e code code
signed main() {
int T = 1;
// T = read();
while (T--) {
vector<int> w(N), d(N), prefd(N), prefw(N);
int n = read(), W = read();
for (int i = 1; i <= n; ++i) {
prefw[i] = w[i] = read();
prefw[i] += prefw[i - 1];
}
for (int i = 1; i <= n; ++i) {
prefd[i] = d[i] = read();
prefd[i] += prefd[i - 1];
}
int res = 0, ans = -1e18, ww = 0, l = 0, Min = 1e18;
for (int i = 1; i <= n; ++i) {
ww += w[i], res += d[i];
while (ww - W >= prefw[l]) Min = min(Min, prefd[l++]);
ans = max({ans, res - Min});
}
write(ans);
}
return 0;
}
树状数组 c o d e code code
vector<int> a(N), tr(N);
signed main() {
auto add = [] (int x, int n, int v) {
for (int i = x; i <= n; i += lowbit(i)) tr[i] += v;
};
auto query = [&] (int x, int n) {
int ans = 0;
for (int i = x; i; i -= lowbit(i)) ans += tr[i];
return ans;
};
int T = 1;
// T = read();
while (T--) {
int n = read(), m = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
add(i, n, a[i] - a[i - 1]);
}
set<int> endpos;
for (int i = 1; i <= n; ++i) {
if (a[i] ^ (a[i - 1] + 1)) endpos.insert(i - 1);
}
endpos.insert(0), endpos.insert(n);
int s = 0, pre = 0;
for (auto it: endpos) {
s += (it - pre) * (it - pre);
pre = it;
}
auto powt = [&] (int x) {
return x * x;
};
while (m--) {
int l = read(), r = read(), w = read();
if (!w) {
writeln(s);
continue;
}
add(l, n, w), add(r + 1, n, -w);
auto pos1 = endpos.lower_bound(l), pos2 = endpos.lower_bound(r);
if (l == *prev(pos1) + 1 && r ^ *pos2) {
if (*prev(pos1)) {
int v1 = query(l - 1, n), v2 = query(l, n);
if (v1 + 1 == v2) {
int t1 = powt(*pos1 - *prev(pos1)), t2 = powt(*prev(pos1) - *prev(prev(pos1)));
int t3 = powt(*pos1 - *prev(prev(pos1)));
s = s - t1 - t2 + t3;
endpos.erase(l - 1);
}
}
int t1 = powt(*pos2 - *prev(pos2));
int t2 = powt(*pos2 - r), t3 = powt(r - *prev(pos2));
s = s - t1 + t2 + t3;
endpos.insert(r);
}
else if (l == *prev(pos1) + 1 && r == *pos2) {
if (*prev(pos1)) {
int v1 = query(l - 1, n), v2 = query(l, n);
if (v1 + 1 == v2) {
int t1 = powt(*pos1 - *prev(pos1)), t2 = powt(*prev(pos1) - *prev(prev(pos1)));
int t3 = powt(*pos1 - *prev(prev(pos1)));
s = s - t1 - t2 + t3;
endpos.erase(l - 1);
}
}
if (r ^ n) {
int v1 = query(r, n), v2 = query(r + 1, n);
if (v1 + 1 == v2) {
int t1 = powt(*next(pos2) - r), t2 = powt(r - *prev(pos2));
int t3 = powt(*next(pos2) - *prev(pos2));
s = s - t1 - t2 + t3;
endpos.erase(r);
}
}
}
else if (l ^ (*prev(pos1) + 1) && r == *pos2) {
if (r ^ n) {
int v1 = query(r, n), v2 = query(r + 1, n);
if (v1 + 1 == v2) {
int t1 = powt(*next(pos2) - r), t2 = powt(r - *prev(pos2));
int t3 = powt(*next(pos2) - *prev(pos2));
s = s - t1 - t2 + t3;
endpos.erase(r);
}
}
int t1 = powt(*pos1 - *prev(pos1));
int t2 = powt(*pos1 - l + 1), t3 = powt(l - 1 - *prev(pos1));
s = s - t1 + t2 + t3;
endpos.insert(l - 1);
}
else if (l ^ (*prev(pos1) + 1) && r ^ *pos2) {
int t1 = powt(*pos1 - *prev(pos1));
int t2 = powt(*pos1 - l + 1), t3 = powt(l - 1 - *prev(pos1));
s = s - t1 + t2 + t3;
endpos.insert(l - 1);
t1 = powt(*pos2 - *prev(pos2));
t2 = powt(*pos2 - r), t3 = powt(r - *prev(pos2));
s = s - t1 + t2 + t3;
endpos.insert(r);
}
writeln(s);
}
}
return 0;
}
线段树 c o d e code code
vector<int> a(N);
struct p {
int l, r, v, tag;
}tr[N << 2];
void build(int i, int l, int r) {
tr[i] = {l, r, 0, 0};
if (l == r) {
tr[i].v = a[l];
return ;
}
int mid = (l + r) >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
}
void pushdown(int i) {
if (tr[i].tag) {
tr[ls].tag += tr[i].tag, tr[rs].tag += tr[i].tag;
tr[ls].v += (tr[ls].r - tr[ls].l + 1) * tr[i].tag;
tr[rs].v += (tr[rs].r - tr[rs].l + 1) * tr[i].tag;
tr[i].tag = 0;
}
}
void modify(int i, int l, int r, int k) {
if (tr[i].l >= l && tr[i].r <= r) {
tr[i].tag += k;
tr[i].v += k;
return ;
}
pushdown(i);
if (tr[ls].r >= l) modify(ls, l, r, k);
if (tr[rs].l <= r) modify(rs, l, r, k);
}
int query(int i, int pos) {
if (tr[i].l == tr[i].r) return tr[i].v;
pushdown(i);
if (pos <= tr[ls].r) return query(ls, pos);
else return query(rs, pos);
}
signed main() {
int T = 1;
// T = read();
while (T--) {
int n = read(), m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
build(1, 1, n);
set<int> endpos;
for (int i = 1; i <= n; ++i) {
if (a[i] ^ (a[i - 1] + 1)) endpos.insert(i - 1);
}
endpos.insert(0), endpos.insert(n);
int s = 0, pre = 0;
for (auto it: endpos) {
s += (it - pre) * (it - pre);
pre = it;
}
auto powt = [&] (int x) {
return x * x;
};
for (int i = 1; i <= m; ++i) {
int l = read(), r = read(), w = read();
if (!w) {
writeln(s);
continue;
}
modify(1, l, r, w);
auto pos1 = endpos.lower_bound(l), pos2 = endpos.lower_bound(r);
if (l == *prev(pos1) + 1 && r ^ *pos2) {
if (*prev(pos1)) {
int v1 = query(1, l - 1), v2 = query(1, l);
if (v1 + 1 == v2) {
int t1 = powt(*pos1 - *prev(pos1)), t2 = powt(*prev(pos1) - *prev(prev(pos1)));
int t3 = powt(*pos1 - *prev(prev(pos1)));
s = s - t1 - t2 + t3;
endpos.erase(l - 1);
}
}
int t1 = powt(*pos2 - *prev(pos2));
int t2 = powt(*pos2 - r), t3 = powt(r - *prev(pos2));
s = s - t1 + t2 + t3;
endpos.insert(r);
}
else if (l == *prev(pos1) + 1 && r == *pos2) {
if (*prev(pos1)) {
int v1 = query(1, l - 1), v2 = query(1, l);
if (v1 + 1 == v2) {
int t1 = powt(*pos1 - *prev(pos1)), t2 = powt(*prev(pos1) - *prev(prev(pos1)));
int t3 = powt(*pos1 - *prev(prev(pos1)));
s = s - t1 - t2 + t3;
endpos.erase(l - 1);
}
}
if (r ^ n) {
int v1 = query(1, r), v2 = query(1, r + 1);
if (v1 + 1 == v2) {
int t1 = powt(*next(pos2) - r), t2 = powt(r - *prev(pos2));
int t3 = powt(*next(pos2) - *prev(pos2));
s = s - t1 - t2 + t3;
endpos.erase(r);
}
}
}
else if (l ^ (*prev(pos1) + 1) && r == *pos2) {
if (r ^ n) {
int v1 = query(1, r), v2 = query(1, r + 1);
if (v1 + 1 == v2) {
int t1 = powt(*next(pos2) - r), t2 = powt(r - *prev(pos2));
int t3 = powt(*next(pos2) - *prev(pos2));
s = s - t1 - t2 + t3;
endpos.erase(r);
}
}
int t1 = powt(*pos1 - *prev(pos1));
int t2 = powt(*pos1 - l + 1), t3 = powt(l - 1 - *prev(pos1));
s = s - t1 + t2 + t3;
endpos.insert(l - 1);
}
else if (l ^ (*prev(pos1) + 1) && r ^ *pos2) {
int t1 = powt(*pos1 - *prev(pos1));
int t2 = powt(*pos1 - l + 1), t3 = powt(l - 1 - *prev(pos1));
s = s - t1 + t2 + t3;
endpos.insert(l - 1);
t1 = powt(*pos2 - *prev(pos2));
t2 = powt(*pos2 - r), t3 = powt(r - *prev(pos2));
s = s - t1 + t2 + t3;
endpos.insert(r);
}
writeln(s);
}
}
return 0;
}