0% found this document useful (0 votes)
4 views3 pages

Message

The document contains a C++ program that implements a segment tree for handling updates and queries on an array of characters. It allows for updating character values and querying specific conditions based on the sum and minimum of segments in the array. The program reads input values, processes commands, and outputs results based on the specified logic.

Uploaded by

pharminsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Message

The document contains a C++ program that implements a segment tree for handling updates and queries on an array of characters. It allows for updating character values and querying specific conditions based on the sum and minimum of segments in the array. The program reads input values, processes commands, and outputs results based on the specified logic.

Uploaded by

pharminsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "bits/stdc++.

h"
using namespace std;

#define task ""


#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()

#define ep emplace_back
#define pb push_back
#define pf push_front

const ll mod = 1e9 + 7;


const int N = 1e6 + 5;
const ll oo = 1e18;

bool START;

int n, q, x,a[N];
ll p[N];

struct node {
ll min, sum;
node(ll min_ = oo, ll sum_ = 0) : min(min_), sum(sum_) {}
node operator+(const node &other) const {
node res;
if (min < other.min) {
res.min = min;
} else {
res.min = other.min;
}
res.sum = sum + other.sum;
return res;
}
};

struct ST {
node st[N << 2];

void update(int id, int l, int r, int i, int val) {


if (l > i || r < i) return;
if (l == r) {
st[id] = node(val, p[val]);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, i, val);
update(id << 1 | 1, mid + 1, r, i, val);
st[id] = st[id << 1] + st[id << 1 | 1];
}
node get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return node();
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
} st;

inline void solve() {


p[0] = 1;
for (int i = 1; i <= 26; ++i) p[i] = (p[i - 1] << 1ll);
for (int i = 1; i <= n; ++i) st.update(1, 1, n, i, a[i]);
while(q--) {
char t, ch;
int i;
cin >> t;
if (t == '!') {
cin >> i >> ch;
a[i] = (ch - 'a');
st.update(1, 1, n, i, a[i]);
}

int l, r; ll x;
if (t == '?') {
cin >> l >> r >> x;
int len = r - l + 1;
if (len == 1) {
cout << ((x == p[a[l]]) ? "YES" : "NO") << endl;
continue;
}
if (len == 2) {
cout << ((x == p[a[r]] - p[a[l]]) ? "YES" : "NO") << endl;
continue;
}
ll need = x - p[a[r]] + p[a[r - 1]];
auto[Min, sum] = st.get(1, 1, n, l, r - 2);

ll gw = p[Min];
if (need % gw != 0) {
cout << "NO" << endl;
} else {
ll sum_ = sum / gw;
ll need_ = need / gw;

cout << ((abs(need_) > sum_ || ((sum_ - need_) & 1)) ? "NO"
: "YES") << endl;
}
}
}
}

inline void input() {


cin >> n >> q >> x;
for (int i = 1; i <= n; ++i) {
char ch;
cin >> ch;
a[i] = (ch - 'a');
}
return solve();
}
bool END;

int main() {
if(fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

input();

cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
return 0;
}

You might also like