problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02763 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <forward_list>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);
int n;
vector<bitset<26>> seg(1000010);
string s;
void init(int n_) {
n = 1;
while (n < n_) {
n *= 2;
}
}
void update(int i, char x) {
seg[i + n - 1].reset(s[i] - 'a');
s[i] = x;
seg[i + n - 1].set(s[i] - 'a');
i += n - 1;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = seg[2 * i + 1] | seg[2 * i + 2];
}
}
bitset<26> query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) {
bitset<26> bit(0ll);
return bit;
}
if (a <= l && r <= b) {
return seg[k];
} else {
bitset<26> bit1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
bitset<26> bit2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
return bit1 | bit2;
}
}
int main(void) {
int q;
int i, j;
int n_;
vector<int> ans;
cin >> n_ >> s >> q;
init(n_);
rep(i, 0, n_) { update(i, s[i]); }
init(n_);
rep(i, 0, q) {
int x;
cin >> x;
if (x == 1) {
int iq;
char c;
cin >> iq >> c;
iq--;
update(iq, c);
} else {
int l, r;
int an = 0;
cin >> l >> r;
l--;
bitset<26> bit = query(l, r, 0, 0, n);
an = bit.count();
ans.push_back(an);
}
}
for (auto &z : ans) {
cout << z << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <forward_list>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);
int n;
vector<bitset<26>> seg(10000100);
string s;
void init(int n_) {
n = 1;
while (n < n_) {
n *= 2;
}
}
void update(int i, char x) {
seg[i + n - 1].reset(s[i] - 'a');
s[i] = x;
seg[i + n - 1].set(s[i] - 'a');
i += n - 1;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = seg[2 * i + 1] | seg[2 * i + 2];
}
}
bitset<26> query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) {
bitset<26> bit(0ll);
return bit;
}
if (a <= l && r <= b) {
return seg[k];
} else {
bitset<26> bit1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
bitset<26> bit2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
return bit1 | bit2;
}
}
int main(void) {
int q;
int i, j;
int n_;
vector<int> ans;
cin >> n_ >> s >> q;
init(n_);
rep(i, 0, n_) { update(i, s[i]); }
init(n_);
rep(i, 0, q) {
int x;
cin >> x;
if (x == 1) {
int iq;
char c;
cin >> iq >> c;
iq--;
update(iq, c);
} else {
int l, r;
int an = 0;
cin >> l >> r;
l--;
bitset<26> bit = query(l, r, 0, 0, n);
an = bit.count();
ans.push_back(an);
}
}
for (auto &z : ans) {
cout << z << endl;
}
} | replace | 21 | 22 | 21 | 22 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
int tree[26][N];
int n;
void add(int type, int id, int val) {
while (id <= n) {
tree[type][id] += val;
id += (id & -id);
}
}
int qu(int type, int till) {
int ans = 0;
while (till >= 1) {
ans += tree[type][till];
till -= (till & -till);
}
return ans;
}
int res(int l, int r, int type) { return qu(type, r) - qu(type, l - 1); }
int32_t main() {
cin >> n;
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
add(s[i - 1] - 'a', i, 1);
}
int q, ch, pos, l, r;
char c;
cin >> q;
while (q--) {
cin >> ch;
if (ch == 1) {
cin >> pos >> c;
add(c - 'a', pos, 1);
add(s[pos - 1] - 'a', pos, -1);
s[pos - 1] = c;
} else {
cin >> l >> r;
int ans = 0;
for (int i = 0; i < 26; i++) {
if (res(l, r, i) > 0)
ans++;
}
cout << ans << "\n";
}
}
} | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 6;
int tree[26][N];
int n;
void add(int type, int id, int val) {
while (id <= n) {
tree[type][id] += val;
id += (id & -id);
}
}
int qu(int type, int till) {
int ans = 0;
while (till >= 1) {
ans += tree[type][till];
till -= (till & -till);
}
return ans;
}
int res(int l, int r, int type) { return qu(type, r) - qu(type, l - 1); }
int32_t main() {
cin >> n;
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
add(s[i - 1] - 'a', i, 1);
}
int q, ch, pos, l, r;
char c;
cin >> q;
while (q--) {
cin >> ch;
if (ch == 1) {
cin >> pos >> c;
add(c - 'a', pos, 1);
add(s[pos - 1] - 'a', pos, -1);
s[pos - 1] = c;
} else {
cin >> l >> r;
int ans = 0;
for (int i = 0; i < 26; i++) {
if (res(l, r, i) > 0)
ans++;
}
cout << ans << "\n";
}
}
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) begin(x), end(x)
#define show(obj) \
{ \
for (auto x : obj) \
cout << x << ' '; \
cout << endl; \
}
#define line "----------"
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int inf = 1001001000;
const ll INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
int seg_len = 1;
int SEG_MAX = 20;
vector<map<char, int>> seg(SEG_MAX);
void seg_update(int pos, char c) {
pos += seg_len;
while (pos > 0) {
seg[pos][c]++;
pos /= 2;
}
}
void seg_erase(int pos, char c) {
pos += seg_len;
while (pos > 0) {
seg[pos][c]--;
if (seg[pos][c] <= 0)
seg[pos].erase(c);
pos /= 2;
}
}
int res(int l, int r) {
l += seg_len;
r += seg_len;
map<char, int> ret;
while (l < r) {
if (l % 2 == 1) {
for (auto x : seg[l])
ret[x.first] += x.second;
++l;
}
if (r % 2 == 1) {
for (auto x : seg[r - 1])
ret[x.first] += x.second;
--r;
}
l /= 2;
r /= 2;
}
return ret.size();
}
int main() {
int N, Q, i, l, r, tp;
char c;
string s;
cin >> N >> s >> Q;
while (seg_len < N)
seg_len *= 2;
rep(i, N) seg_update(i, s[i]);
rep(_, Q) {
cin >> tp;
if (tp == 1) {
cin >> i >> c;
--i;
seg_update(i, c);
seg_erase(i, s[i]);
s[i] = c;
} else {
cin >> l >> r;
cout << res(l - 1, r) << endl;
}
}
// for(auto x: seg){
// for(auto y:x)cout << y.first << ' ';
// cout << endl;
// }
return 0;
}
| #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) begin(x), end(x)
#define show(obj) \
{ \
for (auto x : obj) \
cout << x << ' '; \
cout << endl; \
}
#define line "----------"
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int inf = 1001001000;
const ll INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
int seg_len = 1;
int SEG_MAX = 10000000;
vector<map<char, int>> seg(SEG_MAX);
void seg_update(int pos, char c) {
pos += seg_len;
while (pos > 0) {
seg[pos][c]++;
pos /= 2;
}
}
void seg_erase(int pos, char c) {
pos += seg_len;
while (pos > 0) {
seg[pos][c]--;
if (seg[pos][c] <= 0)
seg[pos].erase(c);
pos /= 2;
}
}
int res(int l, int r) {
l += seg_len;
r += seg_len;
map<char, int> ret;
while (l < r) {
if (l % 2 == 1) {
for (auto x : seg[l])
ret[x.first] += x.second;
++l;
}
if (r % 2 == 1) {
for (auto x : seg[r - 1])
ret[x.first] += x.second;
--r;
}
l /= 2;
r /= 2;
}
return ret.size();
}
int main() {
int N, Q, i, l, r, tp;
char c;
string s;
cin >> N >> s >> Q;
while (seg_len < N)
seg_len *= 2;
rep(i, N) seg_update(i, s[i]);
rep(_, Q) {
cin >> tp;
if (tp == 1) {
cin >> i >> c;
--i;
seg_update(i, c);
seg_erase(i, s[i]);
s[i] = c;
} else {
cin >> l >> r;
cout << res(l - 1, r) << endl;
}
}
// for(auto x: seg){
// for(auto y:x)cout << y.first << ' ';
// cout << endl;
// }
return 0;
}
| replace | 33 | 34 | 33 | 34 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// 定数
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
const long double PI = 3.1415926535897932;
const long long MAXLL = 9223372036854775807;
const long long INF = 2305843009213693951;
const long long dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const long long dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
// 省略
#define ll long long
#define ull unsigned long long
#define ld long double
#define uld unsigned long double
#define pll pair<long long, long long>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vvvl vector<vector<vector<long long>>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vs vector<string>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<pair<long long, long long>>
#define umap unordered_map
#define uset unordered_set
#define Lqueue priority_queue<long long>
#define Squeue priority_queue<long long, vector<long long>, greater<long long>>
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
// マクロ
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rbf(a, x) for (auto &a : x)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define bitrep(i, s, n) for (long long i = (s); i < (1LL << (n)); i++)
#define bitcheck(bit, i) (bit) & (1LL << (i))
#define Maxe(x) *max_element((x).begin(), (x).end())
#define Mine(x) *min_element((x).begin(), (x).end())
#define Size(x) ((long long)(x).size())
#define Lin(s) getline(cin, (s))
// Yes,No
void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }
// MAX,MIN
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
// 最大公約数,最小公倍数
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
// 切り上げ除算
long long cutup(long long a, long long b) { return (a + b - 1) / b; }
// 累乗
template <typename t> constexpr t my_pow(t a, long long b) {
if (b == 0)
return 1;
if (a == 0)
return 0;
t x = 1;
while (b > 0) {
if (b & 1LL)
x *= a;
a *= a;
b >>= 1LL;
}
return x;
}
#define pow my_pow
// chmin,chmax
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// 組み合わせ O(r)
template <typename t> constexpr t nCr(t n, long long r) {
if (r == 0)
return 1;
if (n == 0)
return 0;
if (n < r)
return 0;
t x = 1;
for (long long i = 1; i <= r; i++) {
x *= n - i + 1;
x /= i;
}
return x;
}
// main関数
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> C(26);
rep(i, n) C[s[i] - 'a'].insert(i + 1);
rep(i, q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
C[s[b - 1] - 'a'].erase(b);
C[c - 'a'].insert(b);
s[b - 1] = c;
} else {
int l, r;
cin >> l >> r;
int res = 0;
rep(j, 26) {
if (lower_bound(all(C[j]), l) != upper_bound(all(C[j]), r))
res++;
}
cout << res << "\n";
}
}
} | #include <bits/stdc++.h>
using namespace std;
// 定数
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
const long double PI = 3.1415926535897932;
const long long MAXLL = 9223372036854775807;
const long long INF = 2305843009213693951;
const long long dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const long long dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
// 省略
#define ll long long
#define ull unsigned long long
#define ld long double
#define uld unsigned long double
#define pll pair<long long, long long>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vvvl vector<vector<vector<long long>>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vs vector<string>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<pair<long long, long long>>
#define umap unordered_map
#define uset unordered_set
#define Lqueue priority_queue<long long>
#define Squeue priority_queue<long long, vector<long long>, greater<long long>>
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
// マクロ
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rbf(a, x) for (auto &a : x)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define bitrep(i, s, n) for (long long i = (s); i < (1LL << (n)); i++)
#define bitcheck(bit, i) (bit) & (1LL << (i))
#define Maxe(x) *max_element((x).begin(), (x).end())
#define Mine(x) *min_element((x).begin(), (x).end())
#define Size(x) ((long long)(x).size())
#define Lin(s) getline(cin, (s))
// Yes,No
void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }
// MAX,MIN
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
// 最大公約数,最小公倍数
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
// 切り上げ除算
long long cutup(long long a, long long b) { return (a + b - 1) / b; }
// 累乗
template <typename t> constexpr t my_pow(t a, long long b) {
if (b == 0)
return 1;
if (a == 0)
return 0;
t x = 1;
while (b > 0) {
if (b & 1LL)
x *= a;
a *= a;
b >>= 1LL;
}
return x;
}
#define pow my_pow
// chmin,chmax
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// 組み合わせ O(r)
template <typename t> constexpr t nCr(t n, long long r) {
if (r == 0)
return 1;
if (n == 0)
return 0;
if (n < r)
return 0;
t x = 1;
for (long long i = 1; i <= r; i++) {
x *= n - i + 1;
x /= i;
}
return x;
}
// main関数
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> C(26);
rep(i, n) C[s[i] - 'a'].insert(i + 1);
rep(i, q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
C[s[b - 1] - 'a'].erase(b);
C[c - 'a'].insert(b);
s[b - 1] = c;
} else {
int l, r;
cin >> l >> r;
int res = 0;
rep(j, 26) {
if (C[j].lower_bound(l) != C[j].upper_bound(r))
res++;
}
cout << res << "\n";
}
}
} | replace | 137 | 138 | 137 | 138 | TLE | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f; // モノイドに対して二項演算を行う関数オブジェクト
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; --k) {
seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
vector<SegmentTree<int>> seg(26,
SegmentTree<int>(
n, [](int a, int b) { return a + b; }, 0));
for (int i = 0; i < n; ++i) {
int foo = s[i] - 'a';
seg[foo].update(i, 1);
}
for (int i = 0; i < q; ++i) {
int query;
cin >> query;
if (query == 1) {
int a;
char c;
cin >> a >> c;
--a;
int foo = s[a] - 'a';
int bar = c - 'a';
seg[foo].update(a, 0);
seg[bar].update(a, 1);
s[i] = c;
} else {
int a, b;
cin >> a >> b;
--a;
int ans = 0;
for (int j = 0; j < 26; ++j) {
if (seg[j].query(a, b) > 0)
++ans;
}
cout << ans << "\n";
}
}
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f; // モノイドに対して二項演算を行う関数オブジェクト
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; --k) {
seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
vector<SegmentTree<int>> seg(26,
SegmentTree<int>(
n, [](int a, int b) { return a + b; }, 0));
for (int i = 0; i < n; ++i) {
int foo = s[i] - 'a';
seg[foo].update(i, 1);
}
for (int i = 0; i < q; ++i) {
int query;
cin >> query;
if (query == 1) {
int a;
char c;
cin >> a >> c;
--a;
int foo = s[a] - 'a';
int bar = c - 'a';
seg[foo].update(a, 0);
seg[bar].update(a, 1);
s[a] = c;
} else {
int a, b;
cin >> a >> b;
--a;
int ans = 0;
for (int j = 0; j < 26; ++j) {
if (seg[j].query(a, b) > 0)
++ans;
}
cout << ans << "\n";
}
}
} | replace | 115 | 116 | 115 | 116 | 0 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
// info[i] := 文字 ('a' + i) がどの位置にあるか
// これを各文字について、二分木で持っておく
vector<set<int>> info(26);
for (int i = 0; i < n; ++i) {
int id = s[i] - 'a';
info[id].insert(i);
}
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int query;
cin >> query;
if (query == 1) {
int pos;
char c;
cin >> pos >> c;
--pos;
if (s[pos] == c)
continue;
info[s[pos]].erase(pos);
int id = c - 'a';
info[id].insert(pos);
s[pos] = c;
}
else { // query == 2
int left, right;
cin >> left >> right;
--left;
int res = 0;
for (int j = 0; j < 26; ++j) {
auto it = info[j].lower_bound(left);
if (it == info[j].end())
continue; // 虚無を指す場合は continue
int pos = *it;
if (pos < right)
++res;
}
cout << res << "\n";
}
}
} | #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
// info[i] := 文字 ('a' + i) がどの位置にあるか
// これを各文字について、二分木で持っておく
vector<set<int>> info(26);
for (int i = 0; i < n; ++i) {
int id = s[i] - 'a';
info[id].insert(i);
}
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int query;
cin >> query;
if (query == 1) {
int pos;
char c;
cin >> pos >> c;
--pos;
if (s[pos] == c)
continue;
info[(s[pos] - 'a')].erase(pos);
int id = c - 'a';
info[id].insert(pos);
s[pos] = c;
}
else { // query == 2
int left, right;
cin >> left >> right;
--left;
int res = 0;
for (int j = 0; j < 26; ++j) {
auto it = info[j].lower_bound(left);
if (it == info[j].end())
continue; // 虚無を指す場合は continue
int pos = *it;
if (pos < right)
++res;
}
cout << res << "\n";
}
}
} | replace | 32 | 33 | 32 | 33 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, f, n) for (ll i = (f); (i) < (n); i++)
#define repe(i, f, n) for (ll i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;
ll INF = 1e+18;
using G = vector<map<int, int>>;
int main() {
int N;
cin >> N;
vector<char> ori(N);
rep(i, 0, N) cin >> ori[i];
map<char, set<int>> m;
rep(i, 0, N) { m[ori[i]].insert(i + 1); }
int Q;
cin >> Q;
rep(i, 0, Q) {
int type;
cin >> type;
if (type == 1) {
int index;
char c;
cin >> index >> c;
m[c].insert(index);
char rc = ori[index];
m[rc].erase(index);
ori[index] = c;
} else {
ll l, r;
cin >> l >> r;
if (l > r) {
ll tmp = l;
l = r;
r = tmp;
}
ll ans = 0;
rep(i, 0, 26) {
if (m[(char)(97 + i)].size() == 0)
continue;
ll posi = *m[(char)(97 + i)].lower_bound(l);
if (posi >= l && posi <= r)
++ans;
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
#define rep(i, f, n) for (ll i = (f); (i) < (n); i++)
#define repe(i, f, n) for (ll i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;
ll INF = 1e+18;
using G = vector<map<int, int>>;
int main() {
int N;
cin >> N;
vector<char> ori(N);
rep(i, 0, N) cin >> ori[i];
map<char, set<int>> m;
rep(i, 0, N) { m[ori[i]].insert(i + 1); }
int Q;
cin >> Q;
rep(i, 0, Q) {
int type;
cin >> type;
if (type == 1) {
int index;
char c;
cin >> index >> c;
m[c].insert(index);
char rc = ori[index - 1];
if (rc != c)
m[rc].erase(index);
ori[index - 1] = c;
} else {
ll l, r;
cin >> l >> r;
if (l > r) {
ll tmp = l;
l = r;
r = tmp;
}
ll ans = 0;
rep(i, 0, 26) {
if (m[(char)(97 + i)].size() == 0)
continue;
ll posi = *m[(char)(97 + i)].lower_bound(l);
if (posi >= l && posi <= r)
++ans;
}
cout << ans << endl;
}
}
} | replace | 27 | 30 | 27 | 31 | 0 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long int lli;
typedef pair<lli, lli> Pii;
const int INF = 10 ^ 9 + 11555;
int main() {
// vector
// vc.push_buck(x)
// sort(vc.begin(),vc.end())
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
vector<set<int>> is(26);
rep(i, n) is[s[i] - 'a'].insert(i);
int temp[3];
char temp2;
int ans;
rep(i, q) {
cin >> temp[0];
if (temp[0] == 2) {
cin >> temp[1] >> temp[2];
ans = 0;
rep(j, 26) {
auto it = is[j].lower_bound(temp[1] - 1);
if (it != is[j].end() && *it <= temp[2] - 1)
ans++;
}
cout << ans << endl;
} else {
cin >> temp[1] >> temp2;
temp[1]--;
is[s[temp[1]] - 'a'].erase(temp[1]);
s[i] = temp2;
is[temp2 - 'a'].insert(temp[1]);
}
}
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long int lli;
typedef pair<lli, lli> Pii;
const int INF = 10 ^ 9 + 11555;
int main() {
// vector
// vc.push_buck(x)
// sort(vc.begin(),vc.end())
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
vector<set<int>> is(26);
rep(i, n) is[s[i] - 'a'].insert(i);
int temp[3];
char temp2;
int ans;
rep(i, q) {
cin >> temp[0];
if (temp[0] == 2) {
cin >> temp[1] >> temp[2];
ans = 0;
rep(j, 26) {
auto it = is[j].lower_bound(temp[1] - 1);
if (it != is[j].end() && *it <= temp[2] - 1)
ans++;
}
cout << ans << endl;
} else {
cin >> temp[1] >> temp2;
temp[1]--;
is[s[temp[1]] - 'a'].erase(temp[1]);
s[temp[1]] = temp2;
is[temp2 - 'a'].insert(temp[1]);
}
}
return 0;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
template <typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;
template <typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;
template <typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP2(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define RREP(i, s, e) for (int i = (int)(s); i >= (int)(e); --i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(), ie = (c).end(); i != ie; ++i)
#define ALL(c) (c).begin(), (c).end()
const double PI = acos(-1);
const ll MOD = 1e9 + 7;
int n, Q;
string s;
int main() {
cin >> n >> s >> Q;
V<set<int>> v(26, set<int>());
REP(i, n) { v[s[i] - 'a'].insert(i); }
REP(q, Q) {
int mode;
cin >> mode;
if (mode == 1) {
int i;
char c;
cin >> i >> c;
i--;
if (s[i] == c)
continue;
v[s[i] - 'a'].erase(i);
v[c - 'a'].insert(i);
s[i] = c;
} else if (mode == 2) {
int l, r;
cin >> l >> r;
l--;
r--;
int cnt = 0;
REP(i, 26) {
set<int> u = v[i];
auto iter = u.lower_bound(l);
if (iter != u.end() && *iter <= r)
cnt++;
}
printf("%d\n", cnt);
}
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long;
template <typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;
template <typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;
template <typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP2(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define RREP(i, s, e) for (int i = (int)(s); i >= (int)(e); --i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(), ie = (c).end(); i != ie; ++i)
#define ALL(c) (c).begin(), (c).end()
const double PI = acos(-1);
const ll MOD = 1e9 + 7;
int n, Q;
string s;
int main() {
cin >> n >> s >> Q;
V<set<int>> v(26, set<int>());
REP(i, n) { v[s[i] - 'a'].insert(i); }
REP(q, Q) {
int mode;
cin >> mode;
if (mode == 1) {
int i;
char c;
cin >> i >> c;
i--;
if (s[i] == c)
continue;
v[s[i] - 'a'].erase(i);
v[c - 'a'].insert(i);
s[i] = c;
} else if (mode == 2) {
int l, r;
cin >> l >> r;
l--;
r--;
int cnt = 0;
REP(i, 26) {
auto iter = v[i].lower_bound(l);
if (iter != v[i].end() && *iter <= r)
cnt++;
}
printf("%d\n", cnt);
}
}
return 0;
}
| replace | 60 | 63 | 60 | 62 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
struct BIT {
vector<int> node;
int sz;
BIT(int sz) : sz(sz) { node.resize(sz, 0); }
int sum(int i) {
int res = 0;
for (i--; i >= 0; i = (i & (i + 1)) - 1)
res += node[i];
return res;
}
void add(int i, int x) {
for (; i < sz; i |= i + 1)
node[i] += x;
}
int sum(int l, int r) { return sum(r) - sum(l); }
};
signed main() {
int N;
string S;
cin >> N >> S;
vector<BIT> bits;
for (int i = 0; i < 26; i++)
bits.push_back(BIT(N));
for (int i = 0; i < N; i++) {
bits[S[i] - 'a'].add(i, 1);
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
int q;
cin >> q;
if (q == 1) {
int index;
char c;
cin >> index >> c;
index--;
bits[S[index] - 'a'].add(index, -1);
bits[c - 'a'].add(index, 1);
S[index] = c;
}
if (q == 2) {
int l, r;
cin >> l >> r;
l--, r--;
int ans = 0;
for (BIT it : bits) {
ans += min(it.sum(l, r + 1), 1);
}
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
struct BIT {
vector<int> node;
int sz;
BIT(int sz) : sz(sz) { node.resize(sz, 0); }
int sum(int i) {
int res = 0;
for (i--; i >= 0; i = (i & (i + 1)) - 1)
res += node[i];
return res;
}
void add(int i, int x) {
for (; i < sz; i |= i + 1)
node[i] += x;
}
int sum(int l, int r) { return sum(r) - sum(l); }
};
signed main() {
int N;
string S;
cin >> N >> S;
vector<BIT> bits;
for (int i = 0; i < 26; i++)
bits.push_back(BIT(N));
for (int i = 0; i < N; i++) {
bits[S[i] - 'a'].add(i, 1);
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
int q;
cin >> q;
if (q == 1) {
int index;
char c;
cin >> index >> c;
index--;
bits[S[index] - 'a'].add(index, -1);
bits[c - 'a'].add(index, 1);
S[index] = c;
}
if (q == 2) {
int l, r;
cin >> l >> r;
l--, r--;
int ans = 0;
for (auto &it : bits) {
ans += min(it.sum(l, r + 1), 1);
}
cout << ans << endl;
}
}
return 0;
}
| replace | 50 | 51 | 50 | 51 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;
ll N, Q;
vector<char> vch;
set<ll> S[26];
signed main() {
cin >> N;
vch.resize(N);
for (int i = 0; i < N; i++) {
cin >> vch[i];
S[vch[i] - 'a'].insert(i);
}
/*
for(int i=0;i<26;i++) {
for(set<ll>::iterator it=S[i].begin();it!=S[i].end();it++) {
cout << i << " " << *it << endl;
}
}
*/
cin >> Q;
for (int i = 0; i < Q; i++) {
int q;
cin >> q;
if (q == 1) {
int id;
char c;
cin >> id >> c;
id--;
S[vch[id] - 'a'].erase(id);
vch[id] = c;
S[vch[id] - 'a'].insert(id);
} else {
int l, r, ans = 0;
cin >> l >> r;
l--;
r--;
for (int i = 0; i < 26; i++) {
if (S[i].empty() || S[i].lower_bound(l) == S[i].end())
continue;
for (set<ll>::iterator it = S[i].begin(); it != S[i].end(); it++) {
// cout << i << " " << *it << " " << *S[i].lower_bound(l) << endl;
}
if (*S[i].lower_bound(l) <= r) {
ans++;
// cout << i << " " << l << " " << *S[i].lower_bound(l) << " " << r
// << endl;
}
}
cout << ans << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;
ll N, Q;
vector<char> vch;
set<ll> S[26];
signed main() {
cin >> N;
vch.resize(N);
for (int i = 0; i < N; i++) {
cin >> vch[i];
S[vch[i] - 'a'].insert(i);
}
/*
for(int i=0;i<26;i++) {
for(set<ll>::iterator it=S[i].begin();it!=S[i].end();it++) {
cout << i << " " << *it << endl;
}
}
*/
cin >> Q;
for (int i = 0; i < Q; i++) {
int q;
cin >> q;
if (q == 1) {
int id;
char c;
cin >> id >> c;
id--;
S[vch[id] - 'a'].erase(id);
vch[id] = c;
S[vch[id] - 'a'].insert(id);
} else {
int l, r, ans = 0;
cin >> l >> r;
l--;
r--;
for (int i = 0; i < 26; i++) {
if (S[i].empty() || S[i].lower_bound(l) == S[i].end())
continue;
// for(set<ll>::iterator it=S[i].begin();it!=S[i].end();it++) {
// cout << i << " " << *it << " " << *S[i].lower_bound(l) << endl;
//}
if (*S[i].lower_bound(l) <= r) {
ans++;
// cout << i << " " << l << " " << *S[i].lower_bound(l) << " " << r
// << endl;
}
}
cout << ans << endl;
}
}
return 0;
} | replace | 43 | 46 | 43 | 46 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
if (x > n) {
return x % n;
} else if (x < 0) {
return x % n + n;
} else
return x;
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return (b);
}
else {
return (gcd(b, a % b));
}
}
int main() {
ll n, q;
cin >> n;
string s;
cin >> s;
cin >> q;
ll t = s.size();
vector<set<ll>> vs(26);
rep(i, 0, t) {
ll tmp = s[i] - 'a';
vs[tmp].insert(i);
}
rep(i, 0, q) {
ll a;
cin >> a;
if (a == 1) {
ll m;
char c;
cin >> m >> c;
vs[s[m - 1] - 'a'].erase(m - 1);
vs[c - 'a'].insert(m - 1);
s[m - 1] = c;
} else {
ll l, r;
cin >> l >> r;
l--;
r--;
ll tmp = 0;
rep(j, 0, 26) {
auto it = lower_bound(vs[j].begin(), vs[j].end(), l);
if (*it <= r && it != vs[j].end()) {
tmp++;
}
}
cout << tmp << endl;
}
}
}
| #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
if (x > n) {
return x % n;
} else if (x < 0) {
return x % n + n;
} else
return x;
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return (b);
}
else {
return (gcd(b, a % b));
}
}
int main() {
ll n, q;
cin >> n;
string s;
cin >> s;
cin >> q;
ll t = s.size();
vector<set<ll>> vs(26);
rep(i, 0, t) {
ll tmp = s[i] - 'a';
vs[tmp].insert(i);
}
rep(i, 0, q) {
ll a;
cin >> a;
if (a == 1) {
ll m;
char c;
cin >> m >> c;
vs[s[m - 1] - 'a'].erase(m - 1);
vs[c - 'a'].insert(m - 1);
s[m - 1] = c;
} else {
ll l, r;
cin >> l >> r;
l--;
r--;
ll tmp = 0;
rep(j, 0, 26) {
auto it = vs[j].lower_bound(l);
if (it != vs[j].end() && *it <= r) {
tmp++;
}
}
cout << tmp << endl;
}
}
}
| replace | 67 | 69 | 67 | 69 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
int main() {
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
set<int> data[26];
rep(i, n) {
int v = s[i] - 'a';
data[v].insert(i);
}
rep(i, q) {
int id;
cin >> id;
if (id == 1) {
int iq;
char cq;
cin >> iq >> cq;
iq--;
int v = s[iq] - 'a';
data[v].erase(iq);
s[iq] = cq;
v = cq - 'a';
data[v].insert(iq);
} else {
int lq, rq;
cin >> lq >> rq;
lq--;
rq--;
int cnt = 0;
rep(j, 26) {
auto it = lower_bound(data[j].begin(), data[j].end(), lq);
if (it != data[j].end() && *it <= rq) {
cnt++;
}
}
cout << cnt << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
int main() {
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
set<int> data[26];
rep(i, n) {
int v = s[i] - 'a';
data[v].insert(i);
}
rep(i, q) {
int id;
cin >> id;
if (id == 1) {
int iq;
char cq;
cin >> iq >> cq;
iq--;
int v = s[iq] - 'a';
data[v].erase(iq);
s[iq] = cq;
v = cq - 'a';
data[v].insert(iq);
} else {
int lq, rq;
cin >> lq >> rq;
lq--;
rq--;
int cnt = 0;
rep(j, 26) {
auto it = data[j].lower_bound(lq);
if (it != data[j].end() && *it <= rq) {
cnt++;
}
}
cout << cnt << endl;
}
}
return 0;
}
| replace | 42 | 43 | 42 | 43 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
int main() {
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
vector<int> data[26];
rep(i, n) {
int v = s[i] - 'a';
data[v].push_back(i);
}
rep(i, q) {
int id;
cin >> id;
if (id == 1) {
int iq;
char cq;
cin >> iq >> cq;
iq--;
int v = s[iq] - 'a';
auto it = lower_bound(data[v].begin(), data[v].end(), iq);
data[v].erase(it);
v = cq - 'a';
it = upper_bound(data[v].begin(), data[v].end(), iq);
data[v].insert(it, iq);
} else {
int lq, rq;
cin >> lq >> rq;
lq--;
rq--;
int cnt = 0;
rep(j, 26) {
auto it = lower_bound(data[j].begin(), data[j].end(), lq);
if (it != data[j].end() && *it <= rq) {
cnt++;
}
}
cout << cnt << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
int main() {
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
vector<int> data[26];
rep(i, n) {
int v = s[i] - 'a';
data[v].push_back(i);
}
rep(i, q) {
int id;
cin >> id;
if (id == 1) {
int iq;
char cq;
cin >> iq >> cq;
iq--;
int v = s[iq] - 'a';
auto it = lower_bound(data[v].begin(), data[v].end(), iq);
data[v].erase(it);
s[iq] = cq;
v = cq - 'a';
it = upper_bound(data[v].begin(), data[v].end(), iq);
data[v].insert(it, iq);
} else {
int lq, rq;
cin >> lq >> rq;
lq--;
rq--;
int cnt = 0;
rep(j, 26) {
auto it = lower_bound(data[j].begin(), data[j].end(), lq);
if (it != data[j].end() && *it <= rq) {
cnt++;
}
}
cout << cnt << endl;
}
}
return 0;
}
| insert | 33 | 33 | 33 | 34 | 0 | |
p02763 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define quickIO() \
{ \
cin.tie(0); \
cout.sync_with_stdio(false); \
}
#define reach(i, a) for (auto i : a)
#define rep(i, n) for (int i = 0; i < ((int)n); i++)
#define REP(i, n) for (int i = 0; i <= ((int)n); i++)
#define srep(i, a, n) for (int i = a; i < ((int)n); i++)
#define SREP(i, a, n) for (int i = a; i <= ((int)n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define pb push_back
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "[";
rep(i, vec.size()) os << (i ? ", " : "") << vec[i];
os << "]";
return os;
}
template <typename T> istream &operator>>(istream &is, pair<T, T> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> ostream &operator<<(ostream &os, pair<T, T> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> bool operator<(vector<T> &a, vector<T> &b) {
rep(i, a.size()) {
if (i == b.size())
return false;
if (a[i] < b[i])
return true;
else if (a[i] > b[i])
return false;
}
return false;
}
template <typename T> bool operator>(vector<T> &a, vector<T> &b) {
rep(i, a.size()) {
if (i == b.size())
return true;
if (a[i] < b[i])
return false;
else if (a[i] > b[i])
return true;
}
return false;
}
int bitcnt(ll x) {
x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) + (x & 0x5555555555555555);
x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) + (x & 0x3333333333333333);
x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) + (x & 0x0F0F0F0F0F0F0F0F);
x = ((x & 0xFF00FF00FF00FF00) >> 8) + (x & 0x00FF00FF00FF00FF);
x = ((x & 0xFFFF0000FFFF0000) >> 16) + (x & 0x0000FFFF0000FFFF);
x = ((x & 0xFFFFFFFF00000000) >> 32) + (x & 0x00000000FFFFFFFF);
return x;
}
int bitcnt(int x) {
x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555);
x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333);
x = ((x & 0xF0F0F0F0) >> 4) + (x & 0x0F0F0F0F);
x = ((x & 0xFF00FF00) >> 8) + (x & 0x00FF00FF);
x = ((x & 0xFFFF0000) >> 16) + (x & 0x0000FFFF);
return x;
}
ll sqrtll(ll x) {
ll left = 0, right = x;
rep(i, 100) {
ll mid = (left + right) >> 1;
if (mid * mid <= x)
left = mid;
else
right = mid;
}
return left;
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
#define debug(x) printf("Case #%d: ", x)
#define DEBUG 0
const ld infl = 1e100;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
const ll inf = 1e15;
const int dx[] = {1, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, -1, 0};
int N;
int bit[500010][26];
void init(int n) {
N = 1;
while (n >= N) {
N <<= 1;
}
}
void add(int key, int k, int a) {
k++;
while (k <= N) {
bit[k][key] += a;
k += -k & k;
}
}
int sum(int key, int k) {
k++;
int res = 0;
while (k > 0) {
res += bit[k][key];
k -= -k & k;
}
return res;
}
int main() {
int n;
cin >> n;
init(n);
string s;
cin >> s;
rep(i, n) { add(s[i] - 'a', i, 1); }
int q;
cin >> q;
while (q--) {
int num;
cin >> num;
if (num == 1) {
int idx;
char to;
scanf("%d %c", &idx, &to);
idx--;
add(s[idx] - 'a', idx, -1);
add(to - 'a', idx, 1);
s[idx] = to;
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int res = 0;
rep(i, 26) {
if (sum(i, r) - sum(i, l - 1))
res++;
}
cout << res << endl;
}
}
return 0;
} | #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define quickIO() \
{ \
cin.tie(0); \
cout.sync_with_stdio(false); \
}
#define reach(i, a) for (auto i : a)
#define rep(i, n) for (int i = 0; i < ((int)n); i++)
#define REP(i, n) for (int i = 0; i <= ((int)n); i++)
#define srep(i, a, n) for (int i = a; i < ((int)n); i++)
#define SREP(i, a, n) for (int i = a; i <= ((int)n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define pb push_back
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "[";
rep(i, vec.size()) os << (i ? ", " : "") << vec[i];
os << "]";
return os;
}
template <typename T> istream &operator>>(istream &is, pair<T, T> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> ostream &operator<<(ostream &os, pair<T, T> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> bool operator<(vector<T> &a, vector<T> &b) {
rep(i, a.size()) {
if (i == b.size())
return false;
if (a[i] < b[i])
return true;
else if (a[i] > b[i])
return false;
}
return false;
}
template <typename T> bool operator>(vector<T> &a, vector<T> &b) {
rep(i, a.size()) {
if (i == b.size())
return true;
if (a[i] < b[i])
return false;
else if (a[i] > b[i])
return true;
}
return false;
}
int bitcnt(ll x) {
x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) + (x & 0x5555555555555555);
x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) + (x & 0x3333333333333333);
x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) + (x & 0x0F0F0F0F0F0F0F0F);
x = ((x & 0xFF00FF00FF00FF00) >> 8) + (x & 0x00FF00FF00FF00FF);
x = ((x & 0xFFFF0000FFFF0000) >> 16) + (x & 0x0000FFFF0000FFFF);
x = ((x & 0xFFFFFFFF00000000) >> 32) + (x & 0x00000000FFFFFFFF);
return x;
}
int bitcnt(int x) {
x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555);
x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333);
x = ((x & 0xF0F0F0F0) >> 4) + (x & 0x0F0F0F0F);
x = ((x & 0xFF00FF00) >> 8) + (x & 0x00FF00FF);
x = ((x & 0xFFFF0000) >> 16) + (x & 0x0000FFFF);
return x;
}
ll sqrtll(ll x) {
ll left = 0, right = x;
rep(i, 100) {
ll mid = (left + right) >> 1;
if (mid * mid <= x)
left = mid;
else
right = mid;
}
return left;
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
#define debug(x) printf("Case #%d: ", x)
#define DEBUG 0
const ld infl = 1e100;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
const ll inf = 1e15;
const int dx[] = {1, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, -1, 0};
int N;
int bit[1000010][26];
void init(int n) {
N = 1;
while (n >= N) {
N <<= 1;
}
}
void add(int key, int k, int a) {
k++;
while (k <= N) {
bit[k][key] += a;
k += -k & k;
}
}
int sum(int key, int k) {
k++;
int res = 0;
while (k > 0) {
res += bit[k][key];
k -= -k & k;
}
return res;
}
int main() {
int n;
cin >> n;
init(n);
string s;
cin >> s;
rep(i, n) { add(s[i] - 'a', i, 1); }
int q;
cin >> q;
while (q--) {
int num;
cin >> num;
if (num == 1) {
int idx;
char to;
scanf("%d %c", &idx, &to);
idx--;
add(s[idx] - 'a', idx, -1);
add(to - 'a', idx, 1);
s[idx] = to;
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int res = 0;
rep(i, 26) {
if (sum(i, r) - sum(i, l - 1))
res++;
}
cout << res << endl;
}
}
return 0;
} | replace | 131 | 132 | 131 | 132 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read() {
ll a = 0, b = getchar(), c = 1;
while (!isdigit(b))
c = b == '-' ? -1 : 1, b = getchar();
while (isdigit(b))
a = a * 10 + b - '0', b = getchar();
return a * c;
}
int n, m, a[500005], b[500005], sum[500005];
string s;
void build(int x, int l, int r) {
if (l == r) {
b[x] = a[l], sum[x] = 1 << b[x];
return;
}
int mid = l + r >> 1;
build(x * 2, l, mid);
build(x * 2 + 1, mid + 1, r);
sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
void gao(int x, int l, int r, int l1, int w) {
if (l == l1 and r == l1) {
b[x] = w, sum[x] = 1 << w;
return;
}
int mid = (l + r) >> 1;
if (l1 <= mid)
gao(x * 2, l, mid, l1, w);
else
gao(x * 2 + 1, mid + 1, r, l1, w);
sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
int query(int x, int l, int r, int l1, int r1) {
if (l >= l1 and r <= r1)
return sum[x];
int res = 0, mid = (l + r) >> 1;
if (l1 <= mid)
res |= query(x << 1, l, mid, l1, r1);
if (r1 > mid)
res |= query(x << 1 | 1, mid + 1, r, l1, r1);
return res;
}
int main() {
n = read();
cin >> s;
for (int i = 0; i < s.size(); i++)
a[i + 1] = s[i] - 'a' + 1;
build(1, 1, n);
m = read();
while (m--) {
int ab = read(), x = read(), y;
if (ab == 1) {
y = getchar();
while (y == ' ')
y = getchar();
y -= 'a' - 1;
gao(1, 1, n, x, y);
} else {
y = read();
printf("%d\n", __builtin_popcount(query(1, 1, n, x, y)));
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read() {
ll a = 0, b = getchar(), c = 1;
while (!isdigit(b))
c = b == '-' ? -1 : 1, b = getchar();
while (isdigit(b))
a = a * 10 + b - '0', b = getchar();
return a * c;
}
int n, m, a[1000005], b[1000005], sum[1000005];
string s;
void build(int x, int l, int r) {
if (l == r) {
b[x] = a[l], sum[x] = 1 << b[x];
return;
}
int mid = l + r >> 1;
build(x * 2, l, mid);
build(x * 2 + 1, mid + 1, r);
sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
void gao(int x, int l, int r, int l1, int w) {
if (l == l1 and r == l1) {
b[x] = w, sum[x] = 1 << w;
return;
}
int mid = (l + r) >> 1;
if (l1 <= mid)
gao(x * 2, l, mid, l1, w);
else
gao(x * 2 + 1, mid + 1, r, l1, w);
sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
int query(int x, int l, int r, int l1, int r1) {
if (l >= l1 and r <= r1)
return sum[x];
int res = 0, mid = (l + r) >> 1;
if (l1 <= mid)
res |= query(x << 1, l, mid, l1, r1);
if (r1 > mid)
res |= query(x << 1 | 1, mid + 1, r, l1, r1);
return res;
}
int main() {
n = read();
cin >> s;
for (int i = 0; i < s.size(); i++)
a[i + 1] = s[i] - 'a' + 1;
build(1, 1, n);
m = read();
while (m--) {
int ab = read(), x = read(), y;
if (ab == 1) {
y = getchar();
while (y == ' ')
y = getchar();
y -= 'a' - 1;
gao(1, 1, n, x, y);
} else {
y = read();
printf("%d\n", __builtin_popcount(query(1, 1, n, x, y)));
}
}
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
const int N = 5e5 + 54;
int n, q, type, x, k, s[N << 2][30], seg[N << 2], pre[N];
char S[N];
int ans, A;
void pushup(int x) {
for (int i = 0; i < 26; ++i)
s[x][i] = s[x << 1][i] + s[x << 1 | 1][i];
seg[x] = seg[x << 1] | seg[x << 1 | 1];
}
void build(int l, int r, int x) {
if (l == r) {
s[x][S[l] - 97] = 1;
seg[x] = 1 << (S[l] - 97);
pre[x] = S[l] - 97;
return;
}
int mid = l + r >> 1;
build(l, mid, x << 1);
build(mid + 1, r, x << 1 | 1);
pushup(x);
}
void modify(int l, int r, int x, int id, int k) {
if (l == r) {
int p = pre[x];
--s[x][p];
if (s[x][p] == 0)
seg[x] -= (1 << p);
++s[x][k];
if (s[x][k] == 1)
seg[x] += (1 << k);
pre[x] = k;
return;
}
int mid = l + r >> 1;
if (id <= mid)
modify(l, mid, x << 1, id, k);
else
modify(mid + 1, r, x << 1 | 1, id, k);
pushup(x);
}
int query(int l, int r, int x, int ql, int qr, int ret = 0) {
if (ql <= l && r <= qr)
return seg[x];
int mid = l + r >> 1;
if (ql <= mid)
ret |= query(l, mid, x << 1, ql, qr);
if (qr > mid)
ret |= query(mid + 1, r, x << 1 | 1, ql, qr);
return ret;
}
int main() {
std::cin >> n;
std::cin >> S + 1;
build(1, n, 1);
for (std::cin >> q; q; --q) {
std::cin >> type;
if (type & 1) {
char y[3];
std::cin >> x >> y;
modify(1, n, 1, x, y[0] - 97);
} else {
int l, r;
std::cin >> l >> r;
ans = query(1, n, 1, l, r), A = 0;
for (int i = 25; i >= 0; --i)
if (ans & (1 << i))
++A;
std::cout << A << '\n';
}
}
} | #include <bits/stdc++.h>
const int N = 5e5 + 54;
int n, q, type, x, k, s[N << 2][30], seg[N << 2], pre[N << 2];
char S[N];
int ans, A;
void pushup(int x) {
for (int i = 0; i < 26; ++i)
s[x][i] = s[x << 1][i] + s[x << 1 | 1][i];
seg[x] = seg[x << 1] | seg[x << 1 | 1];
}
void build(int l, int r, int x) {
if (l == r) {
s[x][S[l] - 97] = 1;
seg[x] = 1 << (S[l] - 97);
pre[x] = S[l] - 97;
return;
}
int mid = l + r >> 1;
build(l, mid, x << 1);
build(mid + 1, r, x << 1 | 1);
pushup(x);
}
void modify(int l, int r, int x, int id, int k) {
if (l == r) {
int p = pre[x];
--s[x][p];
if (s[x][p] == 0)
seg[x] -= (1 << p);
++s[x][k];
if (s[x][k] == 1)
seg[x] += (1 << k);
pre[x] = k;
return;
}
int mid = l + r >> 1;
if (id <= mid)
modify(l, mid, x << 1, id, k);
else
modify(mid + 1, r, x << 1 | 1, id, k);
pushup(x);
}
int query(int l, int r, int x, int ql, int qr, int ret = 0) {
if (ql <= l && r <= qr)
return seg[x];
int mid = l + r >> 1;
if (ql <= mid)
ret |= query(l, mid, x << 1, ql, qr);
if (qr > mid)
ret |= query(mid + 1, r, x << 1 | 1, ql, qr);
return ret;
}
int main() {
std::cin >> n;
std::cin >> S + 1;
build(1, n, 1);
for (std::cin >> q; q; --q) {
std::cin >> type;
if (type & 1) {
char y[3];
std::cin >> x >> y;
modify(1, n, 1, x, y[0] - 97);
} else {
int l, r;
std::cin >> l >> r;
ans = query(1, n, 1, l, r), A = 0;
for (int i = 25; i >= 0; --i)
if (ans & (1 << i))
++A;
std::cout << A << '\n';
}
}
} | replace | 2 | 3 | 2 | 3 | -11 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define MAX 25 * 1e13
#define N 200010
using namespace std;
int MOD = 1000000007;
#define MAX_Q 1001000
using namespace std;
int max_int = 1e9;
int dat[MAX_Q] = {0};
int n;
int init(int n_) {
n = 1;
while (n < n_)
n *= 2;
return 0;
}
int add(int k, char x) { // update with a point
// [a,b) is target interval
// x is the value added on interval
// k is node id
// [l,r) is current interval
k += n - 1; // first n-2 node is not leaf,last n nodes are leaf,adjust target
// position
dat[k] = 0;
dat[k] |= (1 << (x - 'a')); // update the very point
while (k > 0) { // update to the root
k = (k - 1) / 2; // find the father id
dat[k] = dat[k * 2 + 1] | dat[k * 2 + 2]; // update the sum of father node
}
return 0;
}
int query_sum(int a, int b, int k, int l,
int r) { // query sum of value with a span
if (b <= l || r <= a) { // no overlap part
return 0;
} else if (a <= l && r <= b) { // wholely overlap
return dat[k]; // current node value
} else { // partly overlap [l,r)
int res = 0;
res |= query_sum(a, b, k * 2 + 1, l, (l + r) / 2); // search left son
res |= query_sum(a, b, k * 2 + 2, (l + r) / 2, r); // search right son
return res;
}
}
int print() {
cout << "Array : ";
int pos = n - 1; // offset
for (int i = 0; i < n; i++) {
cout << dat[i + pos] << " ";
}
cout << endl;
return 0;
}
string str;
int q;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n_;
cin >> n_;
cin >> str;
cin >> q;
memset(dat, 0, sizeof(dat)); // initial all the data in the inerval tree
int tmp;
init(n_);
for (int j = 0; j < n_; j++) {
add(j, str[j]); // span: [ 0 , n )
}
char op1[100];
int t = 0;
// print();
while (t < q) {
t++;
int op;
cin >> op;
if (op == 1) {
int pt; // update by point
string val;
cin >> pt >> val;
add(pt - 1, val[0]);
// print();
continue;
}
if (op == 2) {
int l, r;
cin >> l >> r;
int ret = query_sum(l - 1, r, 0, 0, n);
int cnt = 0;
for (int i = 0; i < 26; i++) {
if ((ret >> i) & 1)
cnt++;
}
cout << cnt << endl;
continue;
}
} // end for while op
return 0;
} // end main
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define MAX 25 * 1e13
#define N 200010
using namespace std;
int MOD = 1000000007;
#define MAX_Q 2001000
using namespace std;
int max_int = 1e9;
int dat[MAX_Q] = {0};
int n;
int init(int n_) {
n = 1;
while (n < n_)
n *= 2;
return 0;
}
int add(int k, char x) { // update with a point
// [a,b) is target interval
// x is the value added on interval
// k is node id
// [l,r) is current interval
k += n - 1; // first n-2 node is not leaf,last n nodes are leaf,adjust target
// position
dat[k] = 0;
dat[k] |= (1 << (x - 'a')); // update the very point
while (k > 0) { // update to the root
k = (k - 1) / 2; // find the father id
dat[k] = dat[k * 2 + 1] | dat[k * 2 + 2]; // update the sum of father node
}
return 0;
}
int query_sum(int a, int b, int k, int l,
int r) { // query sum of value with a span
if (b <= l || r <= a) { // no overlap part
return 0;
} else if (a <= l && r <= b) { // wholely overlap
return dat[k]; // current node value
} else { // partly overlap [l,r)
int res = 0;
res |= query_sum(a, b, k * 2 + 1, l, (l + r) / 2); // search left son
res |= query_sum(a, b, k * 2 + 2, (l + r) / 2, r); // search right son
return res;
}
}
int print() {
cout << "Array : ";
int pos = n - 1; // offset
for (int i = 0; i < n; i++) {
cout << dat[i + pos] << " ";
}
cout << endl;
return 0;
}
string str;
int q;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n_;
cin >> n_;
cin >> str;
cin >> q;
memset(dat, 0, sizeof(dat)); // initial all the data in the inerval tree
int tmp;
init(n_);
for (int j = 0; j < n_; j++) {
add(j, str[j]); // span: [ 0 , n )
}
char op1[100];
int t = 0;
// print();
while (t < q) {
t++;
int op;
cin >> op;
if (op == 1) {
int pt; // update by point
string val;
cin >> pt >> val;
add(pt - 1, val[0]);
// print();
continue;
}
if (op == 2) {
int l, r;
cin >> l >> r;
int ret = query_sum(l - 1, r, 0, 0, n);
int cnt = 0;
for (int i = 0; i < 26; i++) {
if ((ret >> i) & 1)
cnt++;
}
cout << cnt << endl;
continue;
}
} // end for while op
return 0;
} // end main
| replace | 16 | 17 | 16 | 17 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define M 1000000007
#define pi ld(acos(-1.0))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, n) for (int i = 1; i < (int)(n); i++)
#define repp(i, a, b) for (int i = a; i <= (int)(b); i++)
#define Repp(i, a, b) for (int i = a; i >= (int)(b); i--)
#define cs \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define fi first
#define se second
#define ii pair<int, int>
#define bye(x) \
{ \
cout << x; \
return 0; \
}
typedef long long ll;
typedef long double ld;
using namespace std;
int main() {
cs int k, q, l, r, t, qq;
string s;
char c;
cin >> k >> s >> q;
vector<set<int>> v(26, set<int>{M});
rep(i, k) { v[s[i] - 'a'].insert(i); }
while (q--) {
cin >> qq;
qq--;
if (!qq) {
cin >> l >> c;
l--;
if (s[l] == c)
continue;
v[s[l] - 'a'].erase(l);
v[c - 'a'].insert(l);
s[l] = c;
} else {
cin >> l >> r;
l--;
r--;
t = 0;
rep(i, 26) {
if (*lower_bound(v[i].begin(), v[i].end(), l) <= r)
t++;
}
cout << t << '\n';
}
}
} | #include <bits/stdc++.h>
#define M 1000000007
#define pi ld(acos(-1.0))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, n) for (int i = 1; i < (int)(n); i++)
#define repp(i, a, b) for (int i = a; i <= (int)(b); i++)
#define Repp(i, a, b) for (int i = a; i >= (int)(b); i--)
#define cs \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define fi first
#define se second
#define ii pair<int, int>
#define bye(x) \
{ \
cout << x; \
return 0; \
}
typedef long long ll;
typedef long double ld;
using namespace std;
int main() {
cs int k, q, l, r, t, qq;
string s;
char c;
cin >> k >> s >> q;
vector<set<int>> v(26, set<int>{M});
rep(i, k) { v[s[i] - 'a'].insert(i); }
while (q--) {
cin >> qq;
qq--;
if (!qq) {
cin >> l >> c;
l--;
if (s[l] == c)
continue;
v[s[l] - 'a'].erase(l);
v[c - 'a'].insert(l);
s[l] = c;
} else {
cin >> l >> r;
l--;
r--;
t = 0;
rep(i, 26) {
auto it = v[i].lower_bound(l);
if (it != v[i].end() && *it <= r)
t++;
}
cout << t << '\n';
}
}
} | replace | 47 | 48 | 47 | 49 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
typedef long long ll;
int n, m, k;
set<int> s[30];
set<int>::iterator it1, it2;
char ch[maxn];
int main() {
scanf("%d", &n);
scanf("%s", ch + 1);
for (int i = 1; i <= n; i++)
s[int(ch[i] - 'a')].insert(i);
int q;
scanf("%d", &q);
while (q--) {
int type, x, y;
scanf("%d%d", &type, &x);
if (type == 1) {
char c[2];
scanf("%s", c);
s[int(ch[x] - 'a')].erase(x);
s[int(c[0] - 'a')].insert(x);
ch[x] = c[0];
} else {
scanf("%d", &y);
// cout << q << " " << x << ' ' << y << '\n';
int ans = 0;
for (int i = 0; i < 26; i++) {
// cout<<"i = " << i << ' ' << ans << '\n';
// for (auto val : s[i])cout << val << ' '; cout << "\n";
if (s[i].count(x) > 0 || s[i].count(y) > 0)
ans++;
else {
it1 = s[i].upper_bound(x);
it2 = s[i].upper_bound(y);
if (it1 != s[i].end() && *it1 <= y)
ans++;
}
}
printf("%d\n", ans);
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 7;
const int mod = 1e9 + 7;
typedef long long ll;
int n, m, k;
set<int> s[30];
set<int>::iterator it1, it2;
char ch[maxn];
int main() {
scanf("%d", &n);
scanf("%s", ch + 1);
for (int i = 1; i <= n; i++)
s[int(ch[i] - 'a')].insert(i);
int q;
scanf("%d", &q);
while (q--) {
int type, x, y;
scanf("%d%d", &type, &x);
if (type == 1) {
char c[2];
scanf("%s", c);
s[int(ch[x] - 'a')].erase(x);
s[int(c[0] - 'a')].insert(x);
ch[x] = c[0];
} else {
scanf("%d", &y);
// cout << q << " " << x << ' ' << y << '\n';
int ans = 0;
for (int i = 0; i < 26; i++) {
// cout<<"i = " << i << ' ' << ans << '\n';
// for (auto val : s[i])cout << val << ' '; cout << "\n";
if (s[i].count(x) > 0 || s[i].count(y) > 0)
ans++;
else {
it1 = s[i].upper_bound(x);
it2 = s[i].upper_bound(y);
if (it1 != s[i].end() && *it1 <= y)
ans++;
}
}
printf("%d\n", ans);
}
}
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <typename T> struct BIT {
int n;
vector<T> f;
BIT(int n = 200000) : n(n), f(n + 1) {}
T getsum(int p) {
T ret = 0;
for (++p; p > 0; p -= p & -p) {
ret += f[p];
}
return ret;
}
void add(int p, T x) {
for (++p; p <= n; p += p & -p) {
f[p] += x;
}
}
};
int main() {
int N;
cin >> N;
string s;
cin >> s;
int Q;
cin >> Q;
vector<BIT<long long>> tmp(26);
for (int i = 0; i < N; i++) {
tmp[s[i] - 'a'].add(i, 1);
}
for (int i = 0; i < Q; i++) {
int com;
cin >> com;
if (com == 1) {
int i;
cin >> i;
i--;
char c;
cin >> c;
tmp[s[i] - 'a'].add(i, -1);
s[i] = c;
tmp[s[i] - 'a'].add(i, 1);
}
if (com == 2) {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int i = 0; i < 26; i++) {
ans += tmp[i].getsum(r) - tmp[i].getsum(l - 1) > 0;
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
template <typename T> struct BIT {
int n;
vector<T> f;
BIT(int n = 500002) : n(n), f(n + 1) {}
T getsum(int p) {
T ret = 0;
for (++p; p > 0; p -= p & -p) {
ret += f[p];
}
return ret;
}
void add(int p, T x) {
for (++p; p <= n; p += p & -p) {
f[p] += x;
}
}
};
int main() {
int N;
cin >> N;
string s;
cin >> s;
int Q;
cin >> Q;
vector<BIT<long long>> tmp(26);
for (int i = 0; i < N; i++) {
tmp[s[i] - 'a'].add(i, 1);
}
for (int i = 0; i < Q; i++) {
int com;
cin >> com;
if (com == 1) {
int i;
cin >> i;
i--;
char c;
cin >> c;
tmp[s[i] - 'a'].add(i, -1);
s[i] = c;
tmp[s[i] - 'a'].add(i, 1);
}
if (com == 2) {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int i = 0; i < 26; i++) {
ans += tmp[i].getsum(r) - tmp[i].getsum(l - 1) > 0;
}
cout << ans << endl;
}
}
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
// memset ( a , 0 , n * sizeof(ll) ) ;
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0); // don't use if small inpts
#define endl '\n'
#define sl(n) scanf("%lld", &n)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define f(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define ms(a, b) memset((a), (b), sizeof(a))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define vec(g1) \
int temp; \
cin >> temp; \
g1, push_back(temp);
#define abs(x) ((x < 0) ? (-(x)) : (x))
#define MAX 100005
#define inf LLONG_MAX
#define MIN INT_MIN
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
int mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll powmod(ll a, ll b) {
ll res = 1;
if (a >= mod)
a %= mod;
for (; b; b >>= 1) {
if (b & 1)
res = res * a;
if (res >= mod)
res %= mod;
a = a * a;
if (a >= mod)
a %= mod;
}
return res;
}
// ll fac[200005];
// ll inverse(ll n){return powmod(n, mod-2);}
// ll nCr(ll n, ll r){return (fac[n] * inverse(fac[r]) % mod * inverse(fac[n-r])
// % mod) % mod;}
////****************************************************************************************************************************************************************************************************************////
ll segtree[4 * MAX + 1];
char a[MAX];
void build(int node, int start, int end) {
if (start == end) {
segtree[node] = (1 << (a[start] - 'a'));
return;
}
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
void update(int node, int se, int en, int idx, char val) {
if (se == en) {
a[idx] = val;
segtree[node] = (1 << (val - 'a'));
return;
}
int mid = (se + en) / 2;
if (se <= idx && idx <= mid)
update(2 * node, se, mid, idx, val);
else
update(2 * node + 1, mid + 1, en, idx, val);
segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
ll query(int node, int se, int en, int qs, int qe) {
if (qe < se || qs > en)
return 0;
if (qs <= se && qe >= en)
return segtree[node];
int mid = (se + en) / 2;
ll left = query(2 * node, se, mid, qs, qe);
ll right = query(2 * node + 1, mid + 1, en, qs, qe);
return left | right;
}
int main() {
int q;
int n;
cin >> n;
f(i, 0, n) cin >> a[i];
cin >> q;
build(1, 0, n - 1);
while (q--) {
int t;
cin >> t;
if (t == 1) {
int pos;
char val;
cin >> pos >> val;
update(1, 0, n - 1, pos - 1, val);
} else if (t == 2) {
ll l, r;
cin >> l >> r;
ll ans = query(1, 0, n - 1, l - 1, r - 1);
ans = __builtin_popcount(ans);
cout << ans << endl;
}
}
return 0;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
// memset ( a , 0 , n * sizeof(ll) ) ;
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0); // don't use if small inpts
#define endl '\n'
#define sl(n) scanf("%lld", &n)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define f(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define ms(a, b) memset((a), (b), sizeof(a))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define vec(g1) \
int temp; \
cin >> temp; \
g1, push_back(temp);
#define abs(x) ((x < 0) ? (-(x)) : (x))
#define MAX 500005
#define inf LLONG_MAX
#define MIN INT_MIN
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
int mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll powmod(ll a, ll b) {
ll res = 1;
if (a >= mod)
a %= mod;
for (; b; b >>= 1) {
if (b & 1)
res = res * a;
if (res >= mod)
res %= mod;
a = a * a;
if (a >= mod)
a %= mod;
}
return res;
}
// ll fac[200005];
// ll inverse(ll n){return powmod(n, mod-2);}
// ll nCr(ll n, ll r){return (fac[n] * inverse(fac[r]) % mod * inverse(fac[n-r])
// % mod) % mod;}
////****************************************************************************************************************************************************************************************************************////
ll segtree[4 * MAX + 1];
char a[MAX];
void build(int node, int start, int end) {
if (start == end) {
segtree[node] = (1 << (a[start] - 'a'));
return;
}
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
void update(int node, int se, int en, int idx, char val) {
if (se == en) {
a[idx] = val;
segtree[node] = (1 << (val - 'a'));
return;
}
int mid = (se + en) / 2;
if (se <= idx && idx <= mid)
update(2 * node, se, mid, idx, val);
else
update(2 * node + 1, mid + 1, en, idx, val);
segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
ll query(int node, int se, int en, int qs, int qe) {
if (qe < se || qs > en)
return 0;
if (qs <= se && qe >= en)
return segtree[node];
int mid = (se + en) / 2;
ll left = query(2 * node, se, mid, qs, qe);
ll right = query(2 * node + 1, mid + 1, en, qs, qe);
return left | right;
}
int main() {
int q;
int n;
cin >> n;
f(i, 0, n) cin >> a[i];
cin >> q;
build(1, 0, n - 1);
while (q--) {
int t;
cin >> t;
if (t == 1) {
int pos;
char val;
cin >> pos >> val;
update(1, 0, n - 1, pos - 1, val);
} else if (t == 2) {
ll l, r;
cin >> l >> r;
ll ans = query(1, 0, n - 1, l - 1, r - 1);
ans = __builtin_popcount(ans);
cout << ans << endl;
}
}
return 0;
} | replace | 54 | 55 | 54 | 55 | 0 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, ll> PL;
const ll mod = 1000000007;
const ll MOD = 1000000007;
const ll INF = 1LL << 60;
#define PI (acos(-1))
#define ALL(c) (c).begin(), (c).end()
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << "(" << pa.first << "," << pa.second << ")";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
#define dbg(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
template <typename A, size_t N, typename T>
ll lcm(ll a, ll b) {
return a / __gcd(a, b) * b;
}
bool is_prime(ll x) {
if (x == 1) {
return false;
}
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
const ll MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
struct BIT {
int n;
vector<int> bit;
BIT() { init(); }
BIT(int n) : n(n) { init(); }
void init() {
bit.clear();
bit.resize(n + 1, 0);
}
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
int sum(int x, int y) { return sum(y) - sum(x - 1); }
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
int lower_bound(int w) {
if (w <= 0)
return 0;
int x = 0, r = 1;
while (r < n)
r <<= 1;
for (int k = r; k > 0; k >>= 1) {
if (x + k <= n && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
};
struct LazySegmentTree {
// private:
ll n;
vector<ll> node, lazy;
// public:
LazySegmentTree(vector<ll> v) {
int sz = (int)v.size();
n = 1;
while (n < sz)
n *= 2;
node.resize(2 * n - 1);
lazy.resize(2 * n - 1, 0);
for (int i = 0; i < sz; i++)
node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
node[i] = node[i * 2 + 1] + node[i * 2 + 2];
}
void eval(int k, int l, int r) {
if (lazy[k] != 0) {
node[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k + 1] += lazy[k] / 2;
lazy[2 * k + 2] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(k, l, r);
if (b <= l || r <= a)
return;
if (a <= l && r <= b) {
lazy[k] += (r - l) * x;
eval(k, l, r);
} else {
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
node[k] = node[2 * k + 1] + node[2 * k + 2];
}
}
ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(k, l, r);
if (b <= l || r <= a)
return 0;
if (a <= l && r <= b)
return node[k];
ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
return vl + vr;
}
};
ll digit_sum(ll v) {
ll ret = 0;
while (v) {
ret += (v % 10);
v /= 10;
}
return ret;
}
template <typename T> struct Kruskal {
struct edge {
ll from, to;
T cost;
ll used;
edge() {}
edge(ll from, ll to, T cost) : from(from), to(to), cost(cost), used(0) {}
bool operator<(const edge &e) const { return cost < e.cost; }
};
ll n;
vector<ll> p, r;
vector<edge> edges;
Kruskal() {}
Kruskal(ll n) : n(n) {}
void init(ll n) {
r.assign(n, 1);
p.resize(n);
iota(p.begin(), p.end(), 0);
}
ll find(ll x) { return (x == p[x] ? x : p[x] = find(p[x])); }
bool same(ll x, ll y) { return find(x) == find(y); }
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (r[x] < r[y])
swap(x, y);
r[x] += r[y];
p[y] = x;
}
void add_edge(ll u, ll v, T c) { edges.emplace_back(u, v, c); }
T build() {
sort(edges.begin(), edges.end());
init(n);
T res = 0;
for (auto &e : edges) {
if (!same(e.from, e.to)) {
res += e.cost;
unite(e.from, e.to);
e.used = 1;
}
}
return res;
}
T build(ll k) {
sort(edges.begin(), edges.end());
init(n);
T res = 0;
ll cnt = 0;
for (auto &e : edges) {
if (!same(e.from, e.to)) {
res += e.cost;
unite(e.from, e.to);
e.used = 1;
cnt++;
}
if (cnt == k) {
break;
}
}
return res;
}
};
int LIS(int a[], int n) {
vector<int> A(n, 0x3f3f3f3f);
for (int i = 0; i < n; i++)
*lower_bound(A.begin(), A.end(), a[i]) = a[i];
return find(A.begin(), A.end(), 0x3f3f3f3f) - A.begin();
}
// string maze[1010];
ll maze[100][100];
ll grid_bfs(ll H, ll W, ll sx, ll sy, ll gx, ll gy) {
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<vector<ll>> dist(H, vector<ll>(W, -1));
dist[sy][sx] = 0;
queue<PL> q;
q.push({sy, sx});
while (!q.empty()) {
ll x, y;
tie(y, x) = q.front();
q.pop();
if (y == gy && x == gx) {
break;
}
for (int t = 0; t < 4; t++) {
ll nx = x + dx[t], ny = y + dy[t];
if (nx < 0 || ny < 0 || nx >= W || ny >= H || dist[ny][nx] != -1 ||
maze[ny][nx] == '#') {
continue;
}
dist[ny][nx] = dist[y][x] + 1;
q.push({ny, nx});
}
}
return dist[gy][gx];
}
vector<vector<ll>> warshall_floyd(ll n, vector<vector<ll>> g, ll INF) {
// init vector<vector<ll>> c(10,vector<ll>(10,0));
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (g[i][k] == INF || g[k][j] == INF)
continue;
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
return g;
}
struct Dijkstra {
ll n;
vector<vector<pair<ll, ll>>> Edges;
vector<ll> Dist;
vector<ll> Prev;
vector<ll> PathNum;
Dijkstra(ll n) : n(n), Edges(n), Dist(n), Prev(n), PathNum(n) {
Prev.assign(n, -1);
};
void add_edge(ll a, ll b, ll c, bool directed = true) {
if (directed) {
Edges[a].emplace_back(make_pair(b, c));
} else {
Edges[a].emplace_back(make_pair(b, c));
Edges[b].emplace_back(make_pair(a, c));
}
}
// O((E+V)logV)
void build(int start) {
priority_queue<P, vector<P>, greater<P>> queue;
fill(Dist.begin(), Dist.end(), 1e+18); // 1e18 !?!?
fill(Prev.begin(), Prev.end(), -1); // 1e18 !?!?
Dist[start] = 0;
PathNum[start] = 1;
queue.push(PL(0, start));
while (!queue.empty()) {
PL p = queue.top();
queue.pop();
int v = p.second;
if (Dist[v] < p.first)
continue;
for (int i = 0; i < Edges[v].size(); i++) {
PL e = Edges[v][i];
if (Dist[e.first] > Dist[v] + e.second) {
Dist[e.first] = Dist[v] + e.second;
queue.push(P(Dist[e.first], e.first));
Prev[e.first] = v;
PathNum[e.first] = PathNum[v];
} else if (Dist[e.first] == Dist[v] + e.second) {
PathNum[e.first] += PathNum[v];
PathNum[e.first] %= MOD;
}
}
}
}
ll dist(ll t) { return Dist[t]; }
vector<ll> get_path(ll t) {
vector<ll> path;
for (; t != -1; t = Prev[t]) {
path.push_back(t);
}
reverse(path.begin(), path.end());
return path;
}
ll get_path_num(ll t) { return PathNum[t]; }
// int solve()
// {
// ll v, e, r;
// cin >> v >> e >> r;
// Dijkstra dij(v);
// for (int i = 0; i < e; i++)
// {
// ll a, b, c;
// cin >> a >> b >> c;
// dij.add_edge(a, b, c);
// }
// dij.build(r);
// for (int i = 0; i < v; i++)
// {
// if (dij.dist(i) == 1e18)
// {
// cout << "INF" << endl;
// }
// else
// {
// cout << dij.dist(i) << endl;
// cout << dij.get_path(i) << endl;
// cout << dij.get_path_num(i) << endl;
// }
// }
// return 0;
// }
};
struct CumulativeSum2D {
int H;
int W;
vector<vector<ll>> data;
CumulativeSum2D(int H, int W)
: H(H), W(W), data(H + 1, vector<ll>(W + 1, 0LL)) {}
void add(int x, int y, ll z) { data[x + 1][y + 1] += z; }
void build() {
for (int i = 1; i < data.size(); i++) {
for (int j = 1; j < data[i].size(); j++) {
data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
}
}
}
void print() {
for (int i = 0; i <= H; i++) {
for (int j = 0; j <= W; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
ll query(int sx, int sy, int gx, int gy) {
return (data[gy][gx] - data[sy - 1][gx] - data[gy][sx - 1] +
data[sy - 1][sx - 1]);
}
};
struct LCA {
int n, h;
vector<vector<int>> par;
vector<vector<pair<int, int>>> v;
vector<ll> depth, dis;
LCA(int sz) : n(sz), v(n), depth(n), dis(n) {
h = 1;
while ((1 << h) < n)
h++;
par.assign(h, vector<int>(n, -1));
}
void add_edge(int x, int y, int z) {
v[x].push_back({y, z});
v[y].push_back({x, z});
}
void dfs(int x, int p, int d, ll di) {
par[0][x] = p;
depth[x] = d;
dis[x] = di;
for (auto to : v[x])
if (to.first != p)
dfs(to.first, x, d + 1, di + to.second);
}
void build() {
dfs(0, -1, 0, 0);
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < n; j++) {
if (par[i][j] < 0)
par[i + 1][j] = -1;
else
par[i + 1][j] = par[i][par[i][j]];
}
}
}
int lca(int u, int v) {
if (depth[u] > depth[v])
swap(u, v);
for (int i = 0; i < h; i++)
if ((depth[v] - depth[u]) >> i & 1)
v = par[i][v];
if (u == v)
return u;
for (int i = h - 1; i >= 0; i--) {
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
ll dist(int u, int v) { return dis[u] + dis[v] - 2 * dis[lca(u, v)]; }
// int solve()
// {
// ll n;
// cin >> n;
// LCA lca(n);
// for (int i = 0; i < n - 1; i++)
// {
// ll u, v, w;
// cin >> u >> v >> w;
// lca.add_edge(u, v, w);
// }
// lca.build();
// ll q;
// cin >> q;
// while (q--)
// {
// ll a, b, c;
// cin >> a >> b >> c;
// cout << (lca.dist(a, b) + lca.dist(b, c) + lca.dist(c, a)) / 2 <<
// endl;
// }
// return 0;
// }
};
// IMOS https://imoz.jp/algorithms/imos_method.html
/*名前
bitcount __builtin_popcountll
二次元累積和 CumulativeSum2D
10進数の桁和 digit_sum
(b*b+c*c)**0.5 hypot
// 文字列t ->整数 atoi(t.c_str());
// 文字列t ->long long整数 stoll(t); ローカルではつかえない
to_string()も使えないので、どうにかしたい
*/
/*実装例
DP
-部分文字列DP
https://www.hackerrank.com/contests/bbc003/challenges/bbc003-e/submissions/code/1321355726
組み合わせ
完全順列(pi!=i)
*/
/*------------------------------*/
void solve() {
ll N, q;
cin >> N;
string S;
cin >> S;
cin >> q;
vector<BIT> bit(27, BIT(27));
for (int i = 0; i < N; i++) {
bit[S[i] - 'a'].add(i + 1, 1);
}
while (q--) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
bit[S[i - 1] - 'a'].add(i, -1);
S[i - 1] = c;
bit[S[i - 1] - 'a'].add(i, 1);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
for (int k = 0; k < 26; k++) {
if (bit[k].sum(l, r) > 0) {
ans++;
}
}
cout << ans << endl;
}
}
}
int main() {
cout.precision(10);
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, ll> PL;
const ll mod = 1000000007;
const ll MOD = 1000000007;
const ll INF = 1LL << 60;
#define PI (acos(-1))
#define ALL(c) (c).begin(), (c).end()
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << "(" << pa.first << "," << pa.second << ")";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
#define dbg(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
template <typename A, size_t N, typename T>
ll lcm(ll a, ll b) {
return a / __gcd(a, b) * b;
}
bool is_prime(ll x) {
if (x == 1) {
return false;
}
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
const ll MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
struct BIT {
int n;
vector<int> bit;
BIT() { init(); }
BIT(int n) : n(n) { init(); }
void init() {
bit.clear();
bit.resize(n + 1, 0);
}
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
int sum(int x, int y) { return sum(y) - sum(x - 1); }
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
int lower_bound(int w) {
if (w <= 0)
return 0;
int x = 0, r = 1;
while (r < n)
r <<= 1;
for (int k = r; k > 0; k >>= 1) {
if (x + k <= n && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
};
struct LazySegmentTree {
// private:
ll n;
vector<ll> node, lazy;
// public:
LazySegmentTree(vector<ll> v) {
int sz = (int)v.size();
n = 1;
while (n < sz)
n *= 2;
node.resize(2 * n - 1);
lazy.resize(2 * n - 1, 0);
for (int i = 0; i < sz; i++)
node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
node[i] = node[i * 2 + 1] + node[i * 2 + 2];
}
void eval(int k, int l, int r) {
if (lazy[k] != 0) {
node[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k + 1] += lazy[k] / 2;
lazy[2 * k + 2] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(k, l, r);
if (b <= l || r <= a)
return;
if (a <= l && r <= b) {
lazy[k] += (r - l) * x;
eval(k, l, r);
} else {
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
node[k] = node[2 * k + 1] + node[2 * k + 2];
}
}
ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(k, l, r);
if (b <= l || r <= a)
return 0;
if (a <= l && r <= b)
return node[k];
ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
return vl + vr;
}
};
ll digit_sum(ll v) {
ll ret = 0;
while (v) {
ret += (v % 10);
v /= 10;
}
return ret;
}
template <typename T> struct Kruskal {
struct edge {
ll from, to;
T cost;
ll used;
edge() {}
edge(ll from, ll to, T cost) : from(from), to(to), cost(cost), used(0) {}
bool operator<(const edge &e) const { return cost < e.cost; }
};
ll n;
vector<ll> p, r;
vector<edge> edges;
Kruskal() {}
Kruskal(ll n) : n(n) {}
void init(ll n) {
r.assign(n, 1);
p.resize(n);
iota(p.begin(), p.end(), 0);
}
ll find(ll x) { return (x == p[x] ? x : p[x] = find(p[x])); }
bool same(ll x, ll y) { return find(x) == find(y); }
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (r[x] < r[y])
swap(x, y);
r[x] += r[y];
p[y] = x;
}
void add_edge(ll u, ll v, T c) { edges.emplace_back(u, v, c); }
T build() {
sort(edges.begin(), edges.end());
init(n);
T res = 0;
for (auto &e : edges) {
if (!same(e.from, e.to)) {
res += e.cost;
unite(e.from, e.to);
e.used = 1;
}
}
return res;
}
T build(ll k) {
sort(edges.begin(), edges.end());
init(n);
T res = 0;
ll cnt = 0;
for (auto &e : edges) {
if (!same(e.from, e.to)) {
res += e.cost;
unite(e.from, e.to);
e.used = 1;
cnt++;
}
if (cnt == k) {
break;
}
}
return res;
}
};
int LIS(int a[], int n) {
vector<int> A(n, 0x3f3f3f3f);
for (int i = 0; i < n; i++)
*lower_bound(A.begin(), A.end(), a[i]) = a[i];
return find(A.begin(), A.end(), 0x3f3f3f3f) - A.begin();
}
// string maze[1010];
ll maze[100][100];
ll grid_bfs(ll H, ll W, ll sx, ll sy, ll gx, ll gy) {
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<vector<ll>> dist(H, vector<ll>(W, -1));
dist[sy][sx] = 0;
queue<PL> q;
q.push({sy, sx});
while (!q.empty()) {
ll x, y;
tie(y, x) = q.front();
q.pop();
if (y == gy && x == gx) {
break;
}
for (int t = 0; t < 4; t++) {
ll nx = x + dx[t], ny = y + dy[t];
if (nx < 0 || ny < 0 || nx >= W || ny >= H || dist[ny][nx] != -1 ||
maze[ny][nx] == '#') {
continue;
}
dist[ny][nx] = dist[y][x] + 1;
q.push({ny, nx});
}
}
return dist[gy][gx];
}
vector<vector<ll>> warshall_floyd(ll n, vector<vector<ll>> g, ll INF) {
// init vector<vector<ll>> c(10,vector<ll>(10,0));
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (g[i][k] == INF || g[k][j] == INF)
continue;
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
return g;
}
struct Dijkstra {
ll n;
vector<vector<pair<ll, ll>>> Edges;
vector<ll> Dist;
vector<ll> Prev;
vector<ll> PathNum;
Dijkstra(ll n) : n(n), Edges(n), Dist(n), Prev(n), PathNum(n) {
Prev.assign(n, -1);
};
void add_edge(ll a, ll b, ll c, bool directed = true) {
if (directed) {
Edges[a].emplace_back(make_pair(b, c));
} else {
Edges[a].emplace_back(make_pair(b, c));
Edges[b].emplace_back(make_pair(a, c));
}
}
// O((E+V)logV)
void build(int start) {
priority_queue<P, vector<P>, greater<P>> queue;
fill(Dist.begin(), Dist.end(), 1e+18); // 1e18 !?!?
fill(Prev.begin(), Prev.end(), -1); // 1e18 !?!?
Dist[start] = 0;
PathNum[start] = 1;
queue.push(PL(0, start));
while (!queue.empty()) {
PL p = queue.top();
queue.pop();
int v = p.second;
if (Dist[v] < p.first)
continue;
for (int i = 0; i < Edges[v].size(); i++) {
PL e = Edges[v][i];
if (Dist[e.first] > Dist[v] + e.second) {
Dist[e.first] = Dist[v] + e.second;
queue.push(P(Dist[e.first], e.first));
Prev[e.first] = v;
PathNum[e.first] = PathNum[v];
} else if (Dist[e.first] == Dist[v] + e.second) {
PathNum[e.first] += PathNum[v];
PathNum[e.first] %= MOD;
}
}
}
}
ll dist(ll t) { return Dist[t]; }
vector<ll> get_path(ll t) {
vector<ll> path;
for (; t != -1; t = Prev[t]) {
path.push_back(t);
}
reverse(path.begin(), path.end());
return path;
}
ll get_path_num(ll t) { return PathNum[t]; }
// int solve()
// {
// ll v, e, r;
// cin >> v >> e >> r;
// Dijkstra dij(v);
// for (int i = 0; i < e; i++)
// {
// ll a, b, c;
// cin >> a >> b >> c;
// dij.add_edge(a, b, c);
// }
// dij.build(r);
// for (int i = 0; i < v; i++)
// {
// if (dij.dist(i) == 1e18)
// {
// cout << "INF" << endl;
// }
// else
// {
// cout << dij.dist(i) << endl;
// cout << dij.get_path(i) << endl;
// cout << dij.get_path_num(i) << endl;
// }
// }
// return 0;
// }
};
struct CumulativeSum2D {
int H;
int W;
vector<vector<ll>> data;
CumulativeSum2D(int H, int W)
: H(H), W(W), data(H + 1, vector<ll>(W + 1, 0LL)) {}
void add(int x, int y, ll z) { data[x + 1][y + 1] += z; }
void build() {
for (int i = 1; i < data.size(); i++) {
for (int j = 1; j < data[i].size(); j++) {
data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
}
}
}
void print() {
for (int i = 0; i <= H; i++) {
for (int j = 0; j <= W; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
ll query(int sx, int sy, int gx, int gy) {
return (data[gy][gx] - data[sy - 1][gx] - data[gy][sx - 1] +
data[sy - 1][sx - 1]);
}
};
struct LCA {
int n, h;
vector<vector<int>> par;
vector<vector<pair<int, int>>> v;
vector<ll> depth, dis;
LCA(int sz) : n(sz), v(n), depth(n), dis(n) {
h = 1;
while ((1 << h) < n)
h++;
par.assign(h, vector<int>(n, -1));
}
void add_edge(int x, int y, int z) {
v[x].push_back({y, z});
v[y].push_back({x, z});
}
void dfs(int x, int p, int d, ll di) {
par[0][x] = p;
depth[x] = d;
dis[x] = di;
for (auto to : v[x])
if (to.first != p)
dfs(to.first, x, d + 1, di + to.second);
}
void build() {
dfs(0, -1, 0, 0);
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < n; j++) {
if (par[i][j] < 0)
par[i + 1][j] = -1;
else
par[i + 1][j] = par[i][par[i][j]];
}
}
}
int lca(int u, int v) {
if (depth[u] > depth[v])
swap(u, v);
for (int i = 0; i < h; i++)
if ((depth[v] - depth[u]) >> i & 1)
v = par[i][v];
if (u == v)
return u;
for (int i = h - 1; i >= 0; i--) {
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
ll dist(int u, int v) { return dis[u] + dis[v] - 2 * dis[lca(u, v)]; }
// int solve()
// {
// ll n;
// cin >> n;
// LCA lca(n);
// for (int i = 0; i < n - 1; i++)
// {
// ll u, v, w;
// cin >> u >> v >> w;
// lca.add_edge(u, v, w);
// }
// lca.build();
// ll q;
// cin >> q;
// while (q--)
// {
// ll a, b, c;
// cin >> a >> b >> c;
// cout << (lca.dist(a, b) + lca.dist(b, c) + lca.dist(c, a)) / 2 <<
// endl;
// }
// return 0;
// }
};
// IMOS https://imoz.jp/algorithms/imos_method.html
/*名前
bitcount __builtin_popcountll
二次元累積和 CumulativeSum2D
10進数の桁和 digit_sum
(b*b+c*c)**0.5 hypot
// 文字列t ->整数 atoi(t.c_str());
// 文字列t ->long long整数 stoll(t); ローカルではつかえない
to_string()も使えないので、どうにかしたい
*/
/*実装例
DP
-部分文字列DP
https://www.hackerrank.com/contests/bbc003/challenges/bbc003-e/submissions/code/1321355726
組み合わせ
完全順列(pi!=i)
*/
/*------------------------------*/
void solve() {
ll N, q;
cin >> N;
string S;
cin >> S;
cin >> q;
vector<BIT> bit(27, BIT(N + 10));
for (int i = 0; i < N; i++) {
bit[S[i] - 'a'].add(i + 1, 1);
}
while (q--) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
bit[S[i - 1] - 'a'].add(i, -1);
S[i - 1] = c;
bit[S[i - 1] - 'a'].add(i, 1);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
for (int k = 0; k < 26; k++) {
if (bit[k].sum(l, r) > 0) {
ans++;
}
}
cout << ans << endl;
}
}
}
int main() {
cout.precision(10);
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
| replace | 655 | 656 | 655 | 656 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
class fenwick {
vector<int> t;
public:
fenwick() { t.resize(20001); }
void add(int p, int v) {
for (; p < (int)t.size(); p += p & -p) {
t[p] += v;
}
}
int query(int p) {
int ret = 0;
if (p < 0) {
return 0;
}
for (; p; p -= p & -p) {
ret += t[p];
}
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
vector<fenwick> A;
A.resize(26);
for (int i = 0; i < n; ++i) {
A[s[i] - 'a'].add(i + 1, 1);
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
A[s[i - 1] - 'a'].add(i, -1);
A[c - 'a'].add(i, 1);
s[i - 1] = c;
}
if (type == 2) {
int l, r;
cin >> l >> r;
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (A[i].query(r) - A[i].query(l - 1) > 0) {
++ans;
}
}
cout << ans << '\n';
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
class fenwick {
vector<int> t;
public:
fenwick() { t.resize(500001); }
void add(int p, int v) {
for (; p < (int)t.size(); p += p & -p) {
t[p] += v;
}
}
int query(int p) {
int ret = 0;
if (p < 0) {
return 0;
}
for (; p; p -= p & -p) {
ret += t[p];
}
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
string s;
cin >> n >> s >> q;
vector<fenwick> A;
A.resize(26);
for (int i = 0; i < n; ++i) {
A[s[i] - 'a'].add(i + 1, 1);
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
A[s[i - 1] - 'a'].add(i, -1);
A[c - 'a'].add(i, 1);
s[i - 1] = c;
}
if (type == 2) {
int l, r;
cin >> l >> r;
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (A[i].query(r) - A[i].query(l - 1) > 0) {
++ans;
}
}
cout << ans << '\n';
}
}
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INT_INF = 2147483647;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef pair<int, int> P;
#define SEG_LEN (1 << 18)
int seg[26][SEG_LEN * 2];
void add(int cha, int ind, int v) {
ind += SEG_LEN;
seg[cha][ind] += v;
while (true) {
ind /= 2;
if (ind == 0)
break;
seg[cha][ind] = seg[cha][ind * 2] + seg[cha][ind * 2 + 1];
}
}
int sum(int cha, int l, int r) {
l += SEG_LEN;
r += SEG_LEN;
int ans = 0;
while (l < r) {
if (l % 2 == 1) {
ans += seg[cha][l];
l++;
}
l /= 2;
if (r % 2 == 1) {
ans += seg[cha][r - 1];
r--;
}
r /= 2;
}
return ans;
}
int main() {
int n, q;
string s;
cin >> n >> s >> q;
rep(i, n) { add(s[i] - 'a', i + 1, 1); }
rep(j, q) {
int id;
cin >> id;
if (id == 1) {
int i;
char c;
cin >> i >> c;
add(s[i - 1] - 'a', i, -1);
add(c - 'a', i, 1);
s[i - 1] = c;
} else {
int ans = 0;
int l, r;
cin >> l >> r;
rep(k, 26) {
if (sum(k, l, r + 1))
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INT_INF = 2147483647;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef pair<int, int> P;
#define SEG_LEN (1 << 20)
int seg[26][SEG_LEN * 2];
void add(int cha, int ind, int v) {
ind += SEG_LEN;
seg[cha][ind] += v;
while (true) {
ind /= 2;
if (ind == 0)
break;
seg[cha][ind] = seg[cha][ind * 2] + seg[cha][ind * 2 + 1];
}
}
int sum(int cha, int l, int r) {
l += SEG_LEN;
r += SEG_LEN;
int ans = 0;
while (l < r) {
if (l % 2 == 1) {
ans += seg[cha][l];
l++;
}
l /= 2;
if (r % 2 == 1) {
ans += seg[cha][r - 1];
r--;
}
r /= 2;
}
return ans;
}
int main() {
int n, q;
string s;
cin >> n >> s >> q;
rep(i, n) { add(s[i] - 'a', i + 1, 1); }
rep(j, q) {
int id;
cin >> id;
if (id == 1) {
int i;
char c;
cin >> i >> c;
add(s[i - 1] - 'a', i, -1);
add(c - 'a', i, 1);
s[i - 1] = c;
} else {
int ans = 0;
int l, r;
cin >> l >> r;
rep(k, 26) {
if (sum(k, l, r + 1))
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) \
sort((c).begin(), (c).end(), greater<typeof((c).begin())>())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORL(i, a, b) for (LL i = (a); i < (b); ++i)
#define REPL(i, n) FORL(i, 0, n)
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(), (a).end()
const double EPS = 1e-10;
const double PI = acos(-1.0);
// debug func
template <typename T> void vprint(vector<T> v) {
for (auto x : v) {
cerr << x << " ";
}
cerr << endl;
}
template <typename T> void vvprint(vector<vector<T>> vv) {
REP(i, SIZE(vv)) {
REP(j, SIZE(vv[i])) { cerr << vv[i][j] << " "; }
cerr << endl;
}
}
template <typename T1, typename T2> void vpprint(vector<pair<T1, T2>> vp) {
REP(i, SIZE(vp)) { cerr << vp[i].first << ", " << vp[i].second << endl; }
}
template <typename T1, typename T2> void mprint(map<T1, T2> m) {
for (auto x : m)
cerr << x.first << ", " << x.second << endl;
}
template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
const Iterator last) {
/* Credits: Thomas Draper */
if ((first == last) || (first == k) || (last == k))
return false;
Iterator itr1 = first;
Iterator itr2 = last;
++itr1;
if (last == itr1)
return false;
itr1 = last;
--itr1;
itr1 = k;
--itr2;
while (first != itr1) {
if (*--itr1 < *itr2) {
Iterator j = k;
while (!(*itr1 < *j))
++j;
iter_swap(itr1, j);
++itr1;
++j;
itr2 = k;
rotate(itr1, j, last);
while (last != j) {
++j;
++itr2;
}
rotate(k, itr2, last);
return true;
}
}
rotate(first, k, last);
return false;
}
inline double get_time_sec(void) {
return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(
chrono::steady_clock::now().time_since_epoch())
.count()) /
1000000000;
}
template <typename T> T gcd(T a, T b) {
if (a > b)
swap(a, b);
if (a == 0)
return b;
else
return gcd(b % a, a);
}
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b)) * b; }
template <typename T> map<T, T> prime_list(T n) {
map<T, T> ret;
for (T i = 2; i * i <= n; i++) {
if (n % i == 0) {
ret[i] = 0;
while (n % i == 0) {
n /= i;
ret[i]++;
}
}
}
if (n != 1)
ret[n]++;
return ret;
}
#define MOD 1000000007
LL mypow(LL a, LL n) {
if (n == 0)
return 1;
if (n == 1)
return a % MOD;
if (n % 2 == 1)
return (a * mypow(a, n - 1)) % MOD;
LL t = mypow(a, n / 2);
return (t * t) % MOD;
}
#define FACT_SZ 100010
VLL _fact, _inv;
bool _fact_flg = true;
void _fact_init() {
_fact = VLL(FACT_SZ);
_inv = VLL(FACT_SZ);
_fact[0] = 1;
FOR(i, 1, FACT_SZ) _fact[i] = (_fact[i - 1] * i) % MOD;
_inv[FACT_SZ - 1] = mypow(_fact[FACT_SZ - 1], MOD - 2);
for (int i = FACT_SZ - 2; i >= 0; i--) {
_inv[i] = ((i + 1) * _inv[i + 1]) % MOD;
}
}
LL mycomb(LL n, LL k) {
if (_fact_flg) {
_fact_flg = false;
_fact_init();
}
if (n < k)
return 0;
return (((_fact[n] * _inv[k]) % MOD) * _inv[n - k]) % MOD;
}
VLL par, rnk, sz;
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
bool same(int x, int y) {
x = root(x);
y = root(y);
return x == y;
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (rnk[x] < rnk[y]) {
par[x] = y;
// sz[y] += sz[x];
} else {
par[y] = x;
// sz[x] += sz[y];
if (rnk[x] == rnk[y])
rnk[x]++;
}
}
#define INF 1e15
int main(void) {
LL n;
string s;
cin >> n >> s;
vector<set<int>> vs(26);
REP(i, n) { vs[s[i] - 'a'].insert(i); }
int q;
cin >> q;
REP(hoge, q) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
i--;
REP(j, 26) {
if (vs[j].count(i)) {
vs[j].erase(i);
break;
}
}
vs[c - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
l--, r--;
int cnt = 0;
REP(i, 26) {
auto it = lower_bound(ALL(vs[i]), l);
if (it == vs[i].end())
continue;
if (*it <= r)
cnt++;
}
cout << cnt << endl;
}
}
} | #include "bits/stdc++.h"
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) \
sort((c).begin(), (c).end(), greater<typeof((c).begin())>())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORL(i, a, b) for (LL i = (a); i < (b); ++i)
#define REPL(i, n) FORL(i, 0, n)
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(), (a).end()
const double EPS = 1e-10;
const double PI = acos(-1.0);
// debug func
template <typename T> void vprint(vector<T> v) {
for (auto x : v) {
cerr << x << " ";
}
cerr << endl;
}
template <typename T> void vvprint(vector<vector<T>> vv) {
REP(i, SIZE(vv)) {
REP(j, SIZE(vv[i])) { cerr << vv[i][j] << " "; }
cerr << endl;
}
}
template <typename T1, typename T2> void vpprint(vector<pair<T1, T2>> vp) {
REP(i, SIZE(vp)) { cerr << vp[i].first << ", " << vp[i].second << endl; }
}
template <typename T1, typename T2> void mprint(map<T1, T2> m) {
for (auto x : m)
cerr << x.first << ", " << x.second << endl;
}
template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
const Iterator last) {
/* Credits: Thomas Draper */
if ((first == last) || (first == k) || (last == k))
return false;
Iterator itr1 = first;
Iterator itr2 = last;
++itr1;
if (last == itr1)
return false;
itr1 = last;
--itr1;
itr1 = k;
--itr2;
while (first != itr1) {
if (*--itr1 < *itr2) {
Iterator j = k;
while (!(*itr1 < *j))
++j;
iter_swap(itr1, j);
++itr1;
++j;
itr2 = k;
rotate(itr1, j, last);
while (last != j) {
++j;
++itr2;
}
rotate(k, itr2, last);
return true;
}
}
rotate(first, k, last);
return false;
}
inline double get_time_sec(void) {
return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(
chrono::steady_clock::now().time_since_epoch())
.count()) /
1000000000;
}
template <typename T> T gcd(T a, T b) {
if (a > b)
swap(a, b);
if (a == 0)
return b;
else
return gcd(b % a, a);
}
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b)) * b; }
template <typename T> map<T, T> prime_list(T n) {
map<T, T> ret;
for (T i = 2; i * i <= n; i++) {
if (n % i == 0) {
ret[i] = 0;
while (n % i == 0) {
n /= i;
ret[i]++;
}
}
}
if (n != 1)
ret[n]++;
return ret;
}
#define MOD 1000000007
LL mypow(LL a, LL n) {
if (n == 0)
return 1;
if (n == 1)
return a % MOD;
if (n % 2 == 1)
return (a * mypow(a, n - 1)) % MOD;
LL t = mypow(a, n / 2);
return (t * t) % MOD;
}
#define FACT_SZ 100010
VLL _fact, _inv;
bool _fact_flg = true;
void _fact_init() {
_fact = VLL(FACT_SZ);
_inv = VLL(FACT_SZ);
_fact[0] = 1;
FOR(i, 1, FACT_SZ) _fact[i] = (_fact[i - 1] * i) % MOD;
_inv[FACT_SZ - 1] = mypow(_fact[FACT_SZ - 1], MOD - 2);
for (int i = FACT_SZ - 2; i >= 0; i--) {
_inv[i] = ((i + 1) * _inv[i + 1]) % MOD;
}
}
LL mycomb(LL n, LL k) {
if (_fact_flg) {
_fact_flg = false;
_fact_init();
}
if (n < k)
return 0;
return (((_fact[n] * _inv[k]) % MOD) * _inv[n - k]) % MOD;
}
VLL par, rnk, sz;
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
bool same(int x, int y) {
x = root(x);
y = root(y);
return x == y;
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (rnk[x] < rnk[y]) {
par[x] = y;
// sz[y] += sz[x];
} else {
par[y] = x;
// sz[x] += sz[y];
if (rnk[x] == rnk[y])
rnk[x]++;
}
}
#define INF 1e15
int main(void) {
LL n;
string s;
cin >> n >> s;
vector<set<int>> vs(26);
REP(i, n) { vs[s[i] - 'a'].insert(i); }
int q;
cin >> q;
REP(hoge, q) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
i--;
REP(j, 26) {
if (vs[j].count(i)) {
vs[j].erase(i);
break;
}
}
vs[c - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
l--, r--;
int cnt = 0;
REP(i, 26) {
auto it = vs[i].lower_bound(l);
if (it == vs[i].end())
continue;
if (*it <= r)
cnt++;
}
cout << cnt << endl;
}
}
} | replace | 223 | 224 | 223 | 224 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main() {
int N, Q;
string S;
cin >> N >> S >> Q;
vector<set<int>> alpha(26, set<int>());
rep(i, S.size()) { alpha[(int)(S[i] - 'a')].insert(i); }
rep(i, Q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
b--;
alpha[(int)(S[b] - 'a')].erase(b);
alpha[(int)(c - 'a')].insert(b);
S[i] = c;
} else {
int b, c;
cin >> b >> c;
b--;
c--;
int cnt = 0;
rep(j, 26) {
auto itr = alpha[j].lower_bound(b);
if (itr == alpha[j].end())
continue;
if (*itr <= c)
cnt++;
}
cout << cnt << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main() {
int N, Q;
string S;
cin >> N >> S >> Q;
vector<set<int>> alpha(26, set<int>());
rep(i, S.size()) { alpha[(int)(S[i] - 'a')].insert(i); }
rep(i, Q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
b--;
alpha[(int)(S[b] - 'a')].erase(b);
alpha[(int)(c - 'a')].insert(b);
S[b] = c;
} else {
int b, c;
cin >> b >> c;
b--;
c--;
int cnt = 0;
rep(j, 26) {
auto itr = alpha[j].lower_bound(b);
if (itr == alpha[j].end())
continue;
if (*itr <= c)
cnt++;
}
cout << cnt << endl;
}
}
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p02763 | C++ | Runtime Error | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MAXN = 1e5;
template <class T> T gcd(T a, T b) { return ((b == 0) ? a : gcd(b, a % b)); }
template <class T> struct BIT {
vector<T> bit;
BIT() { bit.assign(MAXN, 0); }
void update(int ind, T delta) {
for (; ind < MAXN; ind += (ind & (-ind))) {
bit[ind] += delta;
if (!ind)
break;
}
}
T query(int ind) {
T sum = 0;
for (;; ind -= (ind & (-ind))) {
sum += bit[ind];
if (ind <= 0)
break;
}
return sum;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int n;
cin >> n;
string s;
cin >> s;
BIT<int> bit1[26];
rep(i, s.size()) bit1[s[i] - 'a'].update(i, 1);
int q;
cin >> q;
rep(j, q) {
int t;
cin >> t;
if (t == 1) {
int pos;
char c;
cin >> pos >> c;
pos--;
bit1[s[pos] - 'a'].update(pos, -1);
s[pos] = c;
bit1[s[pos] - 'a'].update(pos, 1);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
l -= 2, r--;
rep(i, 26) {
ans += (((bit1[i].query(r) - (l >= 0 ? bit1[i].query(l) : 0))) > 0);
}
cout << ans << endl;
}
}
return 0;
}
/*
Things to look out for:
- Integer overflows
- Array bounds
- Special cases
Be careful!
*/
| #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MAXN = 5e5 + 100;
template <class T> T gcd(T a, T b) { return ((b == 0) ? a : gcd(b, a % b)); }
template <class T> struct BIT {
vector<T> bit;
BIT() { bit.assign(MAXN, 0); }
void update(int ind, T delta) {
for (; ind < MAXN; ind += (ind & (-ind))) {
bit[ind] += delta;
if (!ind)
break;
}
}
T query(int ind) {
T sum = 0;
for (;; ind -= (ind & (-ind))) {
sum += bit[ind];
if (ind <= 0)
break;
}
return sum;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int n;
cin >> n;
string s;
cin >> s;
BIT<int> bit1[26];
rep(i, s.size()) bit1[s[i] - 'a'].update(i, 1);
int q;
cin >> q;
rep(j, q) {
int t;
cin >> t;
if (t == 1) {
int pos;
char c;
cin >> pos >> c;
pos--;
bit1[s[pos] - 'a'].update(pos, -1);
s[pos] = c;
bit1[s[pos] - 'a'].update(pos, 1);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
l -= 2, r--;
rep(i, 26) {
ans += (((bit1[i].query(r) - (l >= 0 ? bit1[i].query(l) : 0))) > 0);
}
cout << ans << endl;
}
}
return 0;
}
/*
Things to look out for:
- Integer overflows
- Array bounds
- Special cases
Be careful!
*/
| replace | 20 | 21 | 20 | 21 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int N, Q;
string s;
set<int> v[26];
int main() {
cin >> N;
cin >> s;
cin >> Q;
for (int i = 0; i < N; i++)
v[s[i] - 'a'].insert(i);
for (int i = 0; i < 26; i++)
v[i].insert(N + 1);
vector<int> ans;
for (int i = 0; i < Q; i++) {
int C = 0;
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
v[s[b - 1] - 'a'].erase(b - 1);
s[b - 1] = c;
v[s[b - 1] - 'a'].insert(b - 1);
} else {
int b, c;
cin >> b >> c;
for (int j = 0; j < 26; j++) {
if ((int)c > *lower_bound(v[j].begin(), v[j].end(), b - 1)) {
C++;
}
}
ans.push_back(C);
}
}
for (auto &e : ans)
cout << e << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N, Q;
string s;
set<int> v[26];
int main() {
cin >> N;
cin >> s;
cin >> Q;
for (int i = 0; i < N; i++)
v[s[i] - 'a'].insert(i);
for (int i = 0; i < 26; i++)
v[i].insert(N + 1);
vector<int> ans;
for (int i = 0; i < Q; i++) {
int C = 0;
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
v[s[b - 1] - 'a'].erase(b - 1);
s[b - 1] = c;
v[s[b - 1] - 'a'].insert(b - 1);
} else {
int b, c;
cin >> b >> c;
for (int j = 0; j < 26; j++) {
if (c > *(v[j].lower_bound(b - 1))) {
C++;
}
}
ans.push_back(C);
}
}
for (auto &e : ans)
cout << e << endl;
return 0;
} | replace | 32 | 33 | 32 | 33 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
using P = function<OperatorMonoid(OperatorMonoid, int)>;
int sz;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f = [](int a, int b) { return a + b; };
const G g = [](int a, int b) { return a + b; };
const H h = [](int a, int b) { return a + b; };
const P p = [](int a, int b) { return a * b; };
const Monoid M1 = 0LL;
const OperatorMonoid OM0 = 0LL;
void init(int n) {
sz = 1;
while (sz < n)
sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if (lazy[k] != OM0) {
if (k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], p(lazy[k], len));
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return data[k];
} else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return M1;
} else if (a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }
Monoid operator[](const int &k) { return query(k, k + 1); }
};
int main() {
i64 N, Q;
string S;
cin >> N >> S >> Q;
vector<LazySegmentTree<i64>> seg(26);
for (i64 i = 0; i < 26; i++)
seg[i].init(N);
for (i64 i = 0; i < N; i++)
seg[S[i] - 'a'].set(i, 1);
for (i64 i = 0; i < 26; i++)
seg[i].build();
for (i64 _ = 0; _ < Q; _++) {
i64 type;
cin >> type;
if (type == 1) {
i64 i;
char c;
cin >> i >> c;
i--;
if (S[i] != c) {
seg[S[i] - 'a'].update(i, i + 1, -1);
seg[c - 'a'].update(i, i + 1, 1);
S[i] = c;
}
} else {
i64 l, r;
cin >> l >> r;
i64 ans = 0;
for (i64 i = 0; i < 26; i++) {
seg[i].build();
if (0 < seg[i].query(l - 1, r))
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
using P = function<OperatorMonoid(OperatorMonoid, int)>;
int sz;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f = [](int a, int b) { return a + b; };
const G g = [](int a, int b) { return a + b; };
const H h = [](int a, int b) { return a + b; };
const P p = [](int a, int b) { return a * b; };
const Monoid M1 = 0LL;
const OperatorMonoid OM0 = 0LL;
void init(int n) {
sz = 1;
while (sz < n)
sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if (lazy[k] != OM0) {
if (k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], p(lazy[k], len));
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return data[k];
} else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return M1;
} else if (a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }
Monoid operator[](const int &k) { return query(k, k + 1); }
};
int main() {
i64 N, Q;
string S;
cin >> N >> S >> Q;
vector<LazySegmentTree<i64>> seg(26);
for (i64 i = 0; i < 26; i++)
seg[i].init(N);
for (i64 i = 0; i < N; i++)
seg[S[i] - 'a'].set(i, 1);
for (i64 i = 0; i < 26; i++)
seg[i].build();
for (i64 _ = 0; _ < Q; _++) {
i64 type;
cin >> type;
if (type == 1) {
i64 i;
char c;
cin >> i >> c;
i--;
if (S[i] != c) {
seg[S[i] - 'a'].update(i, i + 1, -1);
seg[c - 'a'].update(i, i + 1, 1);
S[i] = c;
}
} else {
i64 l, r;
cin >> l >> r;
i64 ans = 0;
for (i64 i = 0; i < 26; i++) {
if (0 < seg[i].query(l - 1, r))
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| delete | 112 | 113 | 112 | 112 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
template <typename T> class BinaryIndexedTree {
vector<T> arr;
T initial_value;
long long size;
function<T(T, T)> operation;
public:
BinaryIndexedTree(const long long N, function<T(T, T)> func, T init) {
set_size(N);
set_initial_value(init);
initialize_array();
set_operation(func);
}
void set_size(const long long N) { this->size = N + 1; }
void set_initial_value(const T init) { this->initial_value = init; }
void initialize_array() { this->arr = vector<T>(size, initial_value); }
void initialize_array(const T init) {
set_initial_value(init);
initialize_array();
}
void set_operation(const function<T(T, T)> func) { this->operation = func; }
void update(long long idx, T value) {
while (idx <= this->size) {
this->arr[idx] = this->operation(this->arr[idx], value);
idx += (idx & -idx);
}
}
T query(long long idx) {
T ret = this->initial_value;
while (idx > 0) {
ret = this->operation(ret, this->arr[idx]);
idx -= (idx & -idx);
}
return ret;
}
};
int main() {
int N;
string S;
cin >> N >> S;
S = " " + S;
vector<BinaryIndexedTree<int>> bit(
26, BinaryIndexedTree<int>(
N + 1, [](int a, int b) { return a + b; }, 0));
for (int i = 1; i <= N; i++) {
bit[S[i] - 'a'].update(i, 1);
}
int Q;
cin >> Q;
for (int q = 0; q < Q; q++) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
bit[S[i] - 'a'].update(i, -1);
bit[c - 'a'].update(i, 1);
S[i] = c;
}
if (t == 2) {
int l, r;
int ans = 0;
cin >> l >> r;
for (BinaryIndexedTree<int> b : bit) {
if (b.query(r) - b.query(l - 1) > 0) {
ans++;
}
}
cout << ans << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> class BinaryIndexedTree {
vector<T> arr;
T initial_value;
long long size;
function<T(T, T)> operation;
public:
BinaryIndexedTree(const long long N, function<T(T, T)> func, T init) {
set_size(N);
set_initial_value(init);
initialize_array();
set_operation(func);
}
void set_size(const long long N) { this->size = N + 1; }
void set_initial_value(const T init) { this->initial_value = init; }
void initialize_array() { this->arr = vector<T>(size, initial_value); }
void initialize_array(const T init) {
set_initial_value(init);
initialize_array();
}
void set_operation(const function<T(T, T)> func) { this->operation = func; }
void update(long long idx, T value) {
while (idx <= this->size) {
this->arr[idx] = this->operation(this->arr[idx], value);
idx += (idx & -idx);
}
}
T query(long long idx) {
T ret = this->initial_value;
while (idx > 0) {
ret = this->operation(ret, this->arr[idx]);
idx -= (idx & -idx);
}
return ret;
}
};
int main() {
int N;
string S;
cin >> N >> S;
S = " " + S;
vector<BinaryIndexedTree<int>> bit(
26, BinaryIndexedTree<int>(
N + 1, [](int a, int b) { return a + b; }, 0));
for (int i = 1; i <= N; i++) {
bit[S[i] - 'a'].update(i, 1);
}
int Q;
cin >> Q;
for (int q = 0; q < Q; q++) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
bit[S[i] - 'a'].update(i, -1);
bit[c - 'a'].update(i, 1);
S[i] = c;
}
if (t == 2) {
int l, r;
int ans = 0;
cin >> l >> r;
for (BinaryIndexedTree<int> &b : bit) {
if (b.query(r) - b.query(l - 1) > 0) {
ans++;
}
}
cout << ans << endl;
}
}
}
| replace | 79 | 80 | 79 | 80 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
template <typename T> class BinaryIndexedTree {
vector<T> arr;
T initial_value;
long long size;
function<T(T, T)> operation;
public:
BinaryIndexedTree(const long long N, function<T(T, T)> func, T init) {
set_size(N);
set_initial_value(init);
initialize_array();
set_operation(func);
}
void set_size(const long long N) { this->size = N + 1; }
void set_initial_value(const T init) { this->initial_value = init; }
void initialize_array() { this->arr = vector<T>(size, initial_value); }
void initialize_array(const T init) {
set_initial_value(init);
initialize_array();
}
void set_operation(const function<T(T, T)> func) { this->operation = func; }
void update(long long idx, T value) {
while (idx <= this->size) {
this->arr[idx] = this->operation(this->arr[idx], value);
idx += (idx & -idx);
}
}
T query(long long idx) {
T ret = this->initial_value;
while (idx > 0) {
ret = this->operation(ret, this->arr[idx]);
idx -= (idx & -idx);
}
return ret;
}
};
int main() {
int N;
string S;
cin >> N >> S;
S = " " + S;
vector<BinaryIndexedTree<int>> bit(
26, BinaryIndexedTree<int>(
N + 1, [](int a, int b) { return a + b; }, 0));
for (int i = 1; i <= N; i++) {
bit[S[i] - 'a'].update(i, 1);
}
int Q;
cin >> Q;
for (int q = 0; q < Q; q++) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
bit[S[i] - 'a'].update(i, -1);
bit[c - 'a'].update(i, 1);
S[i] = c;
}
if (t == 2) {
int l, r;
int ans = 0;
cin >> l >> r;
for (auto b : bit) {
if (b.query(r) - b.query(l - 1) > 0) {
ans++;
}
}
cout << ans << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> class BinaryIndexedTree {
vector<T> arr;
T initial_value;
long long size;
function<T(T, T)> operation;
public:
BinaryIndexedTree(const long long N, function<T(T, T)> func, T init) {
set_size(N);
set_initial_value(init);
initialize_array();
set_operation(func);
}
void set_size(const long long N) { this->size = N + 1; }
void set_initial_value(const T init) { this->initial_value = init; }
void initialize_array() { this->arr = vector<T>(size, initial_value); }
void initialize_array(const T init) {
set_initial_value(init);
initialize_array();
}
void set_operation(const function<T(T, T)> func) { this->operation = func; }
void update(long long idx, T value) {
while (idx <= this->size) {
this->arr[idx] = this->operation(this->arr[idx], value);
idx += (idx & -idx);
}
}
T query(long long idx) {
T ret = this->initial_value;
while (idx > 0) {
ret = this->operation(ret, this->arr[idx]);
idx -= (idx & -idx);
}
return ret;
}
};
int main() {
int N;
string S;
cin >> N >> S;
S = " " + S;
vector<BinaryIndexedTree<int>> bit(
26, BinaryIndexedTree<int>(
N + 1, [](int a, int b) { return a + b; }, 0));
for (int i = 1; i <= N; i++) {
bit[S[i] - 'a'].update(i, 1);
}
int Q;
cin >> Q;
for (int q = 0; q < Q; q++) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
bit[S[i] - 'a'].update(i, -1);
bit[c - 'a'].update(i, 1);
S[i] = c;
}
if (t == 2) {
int l, r;
int ans = 0;
cin >> l >> r;
for (int i = 0; i < 26; i++) {
if (bit[i].query(r) - bit[i].query(l - 1) > 0) {
ans++;
}
}
cout << ans << endl;
}
}
}
| replace | 79 | 81 | 79 | 81 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N, Q;
string S;
int type[20001], idx[20001], l[20001], r[20001];
char c[20001];
set<int> st[26];
void input() {
cin >> N >> S >> Q;
for (int q = 0; q < Q; ++q) {
cin >> type[q];
if (type[q] == 1) {
cin >> idx[q] >> c[q];
} else {
cin >> l[q] >> r[q];
}
}
}
void solve() {
for (int i = 0; i < 26; ++i) {
st[i].clear();
st[i].insert(N);
}
for (int i = 0; i < N; ++i)
st[(int)(S[i] - 'a')].insert(i);
for (int q = 0; q < Q; +q) {
if (type[q] == 1) {
st[(int)(S[idx[q] - 1] - 'a')].erase(idx[q] - 1);
S[idx[q] - 1] = c[q];
st[(int)(S[idx[q] - 1] - 'a')].insert(idx[q] - 1);
} else {
int ans = 0;
for (int i = 0; i < 26; ++i) {
auto itr = st[i].lower_bound(l[q] - 1);
if (*itr <= r[q] - 1)
ans++;
}
cout << ans << endl;
}
}
return;
}
int main() {
input();
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N, Q;
string S;
int type[20001], idx[20001], l[20001], r[20001];
char c[20001];
set<int> st[26];
void input() {
cin >> N >> S >> Q;
for (int q = 0; q < Q; ++q) {
cin >> type[q];
if (type[q] == 1) {
cin >> idx[q] >> c[q];
} else {
cin >> l[q] >> r[q];
}
}
}
void solve() {
for (int i = 0; i < 26; ++i) {
st[i].clear();
st[i].insert(N);
}
for (int i = 0; i < N; ++i)
st[(int)(S[i] - 'a')].insert(i);
for (int q = 0; q < Q; ++q) {
if (type[q] == 1) {
st[(int)(S[idx[q] - 1] - 'a')].erase(idx[q] - 1);
S[idx[q] - 1] = c[q];
st[(int)(S[idx[q] - 1] - 'a')].insert(idx[q] - 1);
} else {
int ans = 0;
for (int i = 0; i < 26; ++i) {
auto itr = st[i].lower_bound(l[q] - 1);
if (*itr <= r[q] - 1)
ans++;
}
cout << ans << endl;
}
}
return;
}
int main() {
input();
solve();
return 0;
} | replace | 53 | 54 | 53 | 54 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define rrep2(i, n) for (int i = (n); i > 0; --i)
#define srep(i, s, n) for (int i = s; i < (n); ++i)
#define rsrep(i, s, n) for (int i = (n)-1; i >= s; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aall(a) (a), (a) + countof(a) // for array sorting
#define raall(a) (a), (a) + countof(a), greater<>()
#define show(x) cout << #x << " = " << (x) << endl;
#define vfind(v, a) find(all(v), a) != v.end()
#define yn(f) \
{ \
if (f) \
puts("YES"); \
else \
puts("NO"); \
}
#define yns(f) \
{ \
if (f) \
puts("Yes"); \
else \
puts("No"); \
}
#define show_ary(...) \
{ \
cout << #__VA_ARGS__ << " = "; \
for (const auto &x : (__VA_ARGS__)) { \
cout << x << " "; \
} \
cout << endl; \
}
#define show_pair(...) \
cout << #__VA_ARGS__ << " = " << endl; \
for (const auto &x : (__VA_ARGS__)) { \
cout << " " << x.fi << " : " << x.se << endl; \
}
#define out_ary(...) \
{ \
int n = (__VA_ARGS__).size(); \
rep(i, n) printf("%d%s", (__VA_ARGS__)[i], i != n - 1 ? " " : "\n"); \
}
#define argmax(v) distance((v).begin(), max_element(all(v)))
#define argmin(v) distance((v).begin(), min_element(all(v)))
#define vmax(v) *max_element(all(v))
#define vmin(v) *min_element(all(v))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<P> vpair;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<vstr> vvstr;
typedef vector<vbool> vvbool;
const ll LINF = 2000000000000000000ll;
const int INF = 1000000100;
const ll MOD = 1e9 + 7;
int main() {
int n;
string s;
int q;
cin >> n >> s >> q;
int sl = s.length();
vector<set<int>> st(26);
rep(i, sl) { st[s[i] - 'a'].insert(i); }
rep(i, q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
--b;
st[s[b] - 'a'].erase(b);
st[c - 'a'].insert(b);
s[b] = c;
} else {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
for (auto sti : st) {
auto id = sti.lower_bound(l);
if (sti.end() != id && *id < r)
ans++;
}
cout << ans << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define rrep2(i, n) for (int i = (n); i > 0; --i)
#define srep(i, s, n) for (int i = s; i < (n); ++i)
#define rsrep(i, s, n) for (int i = (n)-1; i >= s; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aall(a) (a), (a) + countof(a) // for array sorting
#define raall(a) (a), (a) + countof(a), greater<>()
#define show(x) cout << #x << " = " << (x) << endl;
#define vfind(v, a) find(all(v), a) != v.end()
#define yn(f) \
{ \
if (f) \
puts("YES"); \
else \
puts("NO"); \
}
#define yns(f) \
{ \
if (f) \
puts("Yes"); \
else \
puts("No"); \
}
#define show_ary(...) \
{ \
cout << #__VA_ARGS__ << " = "; \
for (const auto &x : (__VA_ARGS__)) { \
cout << x << " "; \
} \
cout << endl; \
}
#define show_pair(...) \
cout << #__VA_ARGS__ << " = " << endl; \
for (const auto &x : (__VA_ARGS__)) { \
cout << " " << x.fi << " : " << x.se << endl; \
}
#define out_ary(...) \
{ \
int n = (__VA_ARGS__).size(); \
rep(i, n) printf("%d%s", (__VA_ARGS__)[i], i != n - 1 ? " " : "\n"); \
}
#define argmax(v) distance((v).begin(), max_element(all(v)))
#define argmin(v) distance((v).begin(), min_element(all(v)))
#define vmax(v) *max_element(all(v))
#define vmin(v) *min_element(all(v))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<P> vpair;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<vstr> vvstr;
typedef vector<vbool> vvbool;
const ll LINF = 2000000000000000000ll;
const int INF = 1000000100;
const ll MOD = 1e9 + 7;
int main() {
int n;
string s;
int q;
cin >> n >> s >> q;
int sl = s.length();
vector<set<int>> st(26);
rep(i, sl) { st[s[i] - 'a'].insert(i); }
rep(i, q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
--b;
st[s[b] - 'a'].erase(b);
st[c - 'a'].insert(b);
s[b] = c;
} else {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
// for (auto sti : st) {
// auto id = sti.lower_bound(l);
// if (sti.end() != id && *id < r) ans++;
// }
rep(j, 26) {
auto id = st[j].lower_bound(l);
if (st[j].end() != id && *id < r)
ans++;
}
cout << ans << endl;
}
}
return 0;
} | replace | 95 | 98 | 95 | 102 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define exrep(i, a, b) for (long long i = a; i <= b; i++)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<P>> vvp;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> st(26, set<int>({MAX}));
rep(i, n) { st[s[i] - 'a'].insert(i); }
while (q--) {
int type;
cin >> type;
if (type == 1) {
int idx;
char nc;
cin >> idx >> nc;
idx--;
int pc = s[idx];
st[pc - 'a'].erase(idx);
st[nc - 'a'].insert(idx);
s[idx] = nc;
} else {
int l, r;
cin >> l >> r;
l--;
int cnt = 0;
rep(i, 26) {
if (st[i].empty())
continue;
int lidx = *lower_bound(all(st[i]), l);
int ridx = *lower_bound(all(st[i]), r);
if (ridx > lidx)
cnt++;
}
cout << cnt << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define exrep(i, a, b) for (long long i = a; i <= b; i++)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<P>> vvp;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> st(26, set<int>({MAX}));
rep(i, n) { st[s[i] - 'a'].insert(i); }
while (q--) {
int type;
cin >> type;
if (type == 1) {
int idx;
char nc;
cin >> idx >> nc;
idx--;
int pc = s[idx];
st[pc - 'a'].erase(idx);
st[nc - 'a'].insert(idx);
s[idx] = nc;
} else {
int l, r;
cin >> l >> r;
l--;
int cnt = 0;
rep(i, 26) {
if (st[i].empty())
continue;
int lidx = *st[i].lower_bound(l);
int ridx = *st[i].lower_bound(r);
if (ridx > lidx)
cnt++;
}
cout << cnt << endl;
}
}
return 0;
}
| replace | 66 | 68 | 66 | 68 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
int N;
string S;
int Q;
int qtype[20202];
int i[20202], l[20202], r[20202];
char c[20202];
set<int> char_pos[30];
vector<int> ans;
int main() {
cin >> N;
cin >> S;
cin >> Q;
for (int i = 0; i < N; i++) {
char si = S[i];
char_pos[si - 'a'].insert(i);
}
rep(j, 0, Q) {
cin >> qtype[j];
if (qtype[j] == 1) {
int i;
char c;
cin >> i >> c;
i--;
char si = S[i];
char_pos[si - 'a'].erase(i);
S[i] = c;
char_pos[c - 'a'].insert(i);
} else {
ll l, r;
cin >> l >> r;
l--;
int cnt = 0;
for (int k = 0; k < 26; k++) {
auto cp = char_pos[k];
auto lb = cp.lower_bound(l);
if (lb != cp.end() && *lb < r) {
cnt++;
}
}
cout << cnt << endl;
}
}
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
int N;
string S;
int Q;
int qtype[20202];
int i[20202], l[20202], r[20202];
char c[20202];
set<int> char_pos[30];
vector<int> ans;
int main() {
cin >> N;
cin >> S;
cin >> Q;
for (int i = 0; i < N; i++) {
char si = S[i];
char_pos[si - 'a'].insert(i);
}
rep(j, 0, Q) {
cin >> qtype[j];
if (qtype[j] == 1) {
int i;
char c;
cin >> i >> c;
i--;
char si = S[i];
char_pos[si - 'a'].erase(i);
S[i] = c;
char_pos[c - 'a'].insert(i);
} else {
ll l, r;
cin >> l >> r;
l--;
int cnt = 0;
for (int k = 0; k < 26; k++) {
auto lb = char_pos[k].lower_bound(l);
if (lb != char_pos[k].end() && *lb < r) {
cnt++;
}
}
cout << cnt << endl;
}
}
}
| replace | 65 | 68 | 65 | 67 | TLE | |
p02763 | C++ | Runtime Error | #pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
using namespace std;
inline int gcd(int a, int b) {
while (b) {
int c = a;
a = b;
b = c % b;
}
return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int mypow(int a, int b) {
if (!b)
return 1;
if (b & 1)
return mypow(a, b - 1) * a;
int memo = mypow(a, b >> 1);
return memo * memo;
}
int modpow(int a, int b, int m = mod) {
if (!b)
return 1;
if (b & 1)
return modpow(a, b - 1, m) * a % m;
int memo = modpow(a, b >> 1, m);
return memo * memo % m;
}
class BIT {
int n;
int *bit;
public:
BIT(int n) : n(n) {
bit = new int[n];
fill(bit + 1, bit + n + 1, 0);
}
void add(int a, int x) {
while (a < n) {
bit[a] += x;
a += a & -a;
}
}
int query(int a) {
int cnt = 0;
while (a > 0) {
cnt += bit[a];
a -= a & -a;
}
return cnt;
}
};
int n, q, t, a, b;
string s;
char c;
signed main() {
cin >> n >> s >> q;
vector<BIT> vec;
rep(i, 26) { vec.emplace_back(n + 1); }
rep(i, n) { vec[s[i] - 'a'].add(i + 1, 1); }
rep(i, q) {
cin >> t;
if (t == 1) {
cin >> a >> c;
vec[s[a - 1] - 'a'].add(a, -1);
vec[c - 'a'].add(a, 1);
s[a - 1] = c;
} else {
cin >> a >> b;
int ans = 0;
rep(j, 26) {
if (a > 1) {
if (vec[j].query(b) - vec[j].query(a - 1))
ans++;
} else if (vec[j].query(b)) {
ans++;
}
}
cout << ans << endl;
}
}
return 0;
} | #pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
using namespace std;
inline int gcd(int a, int b) {
while (b) {
int c = a;
a = b;
b = c % b;
}
return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int mypow(int a, int b) {
if (!b)
return 1;
if (b & 1)
return mypow(a, b - 1) * a;
int memo = mypow(a, b >> 1);
return memo * memo;
}
int modpow(int a, int b, int m = mod) {
if (!b)
return 1;
if (b & 1)
return modpow(a, b - 1, m) * a % m;
int memo = modpow(a, b >> 1, m);
return memo * memo % m;
}
class BIT {
int n;
int *bit;
public:
BIT(int n) : n(n) {
bit = new int[n + 1];
fill(bit + 1, bit + n + 1, 0);
}
void add(int a, int x) {
while (a < n) {
bit[a] += x;
a += a & -a;
}
}
int query(int a) {
int cnt = 0;
while (a > 0) {
cnt += bit[a];
a -= a & -a;
}
return cnt;
}
};
int n, q, t, a, b;
string s;
char c;
signed main() {
cin >> n >> s >> q;
vector<BIT> vec;
rep(i, 26) { vec.emplace_back(n + 1); }
rep(i, n) { vec[s[i] - 'a'].add(i + 1, 1); }
rep(i, q) {
cin >> t;
if (t == 1) {
cin >> a >> c;
vec[s[a - 1] - 'a'].add(a, -1);
vec[c - 'a'].add(a, 1);
s[a - 1] = c;
} else {
cin >> a >> b;
int ans = 0;
rep(j, 26) {
if (a > 1) {
if (vec[j].query(b) - vec[j].query(a - 1))
ans++;
} else if (vec[j].query(b)) {
ans++;
}
}
cout << ans << endl;
}
}
return 0;
} | replace | 96 | 97 | 96 | 97 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#define X first
#define Y second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
#define endl '\n'
#define fastio \
cin.sync_with_stdio(false); \
cin.tie(nullptr)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int SZ = 1 << 3;
bool tree[26][SZ << 1];
void update(int k, int x, int v) {
for (tree[k][x += SZ] = v; x >>= 1;)
tree[k][x] = tree[k][x << 1] | tree[k][x << 1 | 1];
}
bool query(int k, int L, int R, int idx = 1, int s = 0, int e = SZ - 1) {
if (e < L || R < s)
return 0;
if (L <= s && e <= R)
return tree[k][idx];
int m = s + e >> 1;
return query(k, L, R, idx << 1, s, m) |
query(k, L, R, idx << 1 | 1, m + 1, e);
}
int main() {
fastio;
int N;
cin >> N;
string S;
cin >> S;
for (int i = 0; i < N; ++i) {
update(S[i] - 97, i, 1);
}
int Q;
cin >> Q;
for (int q; Q--;) {
cin >> q;
if (q == 1) {
int a;
char b;
cin >> a >> b;
update(S[a - 1] - 97, a - 1, 0);
update(b - 97, a - 1, 1);
S[a - 1] = b;
} else {
int a, b, cnt = 0;
cin >> a >> b;
for (int i = 0; i < 26; ++i) {
cnt += query(i, a - 1, b - 1);
}
cout << cnt << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define X first
#define Y second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
#define endl '\n'
#define fastio \
cin.sync_with_stdio(false); \
cin.tie(nullptr)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int SZ = 1 << 19;
bool tree[26][SZ << 1];
void update(int k, int x, int v) {
for (tree[k][x += SZ] = v; x >>= 1;)
tree[k][x] = tree[k][x << 1] | tree[k][x << 1 | 1];
}
bool query(int k, int L, int R, int idx = 1, int s = 0, int e = SZ - 1) {
if (e < L || R < s)
return 0;
if (L <= s && e <= R)
return tree[k][idx];
int m = s + e >> 1;
return query(k, L, R, idx << 1, s, m) |
query(k, L, R, idx << 1 | 1, m + 1, e);
}
int main() {
fastio;
int N;
cin >> N;
string S;
cin >> S;
for (int i = 0; i < N; ++i) {
update(S[i] - 97, i, 1);
}
int Q;
cin >> Q;
for (int q; Q--;) {
cin >> q;
if (q == 1) {
int a;
char b;
cin >> a >> b;
update(S[a - 1] - 97, a - 1, 0);
update(b - 97, a - 1, 1);
S[a - 1] = b;
} else {
int a, b, cnt = 0;
cin >> a >> b;
for (int i = 0; i < 26; ++i) {
cnt += query(i, a - 1, b - 1);
}
cout << cnt << endl;
}
}
return 0;
} | replace | 18 | 19 | 18 | 19 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
void textIO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void fastIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
#define mx 500005
int tree[mx];
string str;
void build(int node, int s, int e) {
if (s == e) {
tree[node] = (1 << (str[s - 1] - 'a'));
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (s + e) / 2;
build(left, s, mid);
build(right, mid + 1, e);
tree[node] = tree[left] | tree[right];
return;
}
int query(int node, int s, int e, int l, int r) {
if (l > e || r < s) {
return 0;
}
if (l <= s && e <= r) {
return tree[node];
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (s + e) / 2;
return query(left, s, mid, l, r) | query(right, mid + 1, e, l, r);
}
void update(int node, int ind, char val, int s, int e) {
if (s == ind && e == ind) {
tree[node] = (1 << (val - 'a'));
return;
}
if (ind < s || ind > e) {
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (s + e) / 2;
update(left, ind, val, s, mid);
update(right, ind, val, mid + 1, e);
tree[node] = tree[left] | tree[right];
return;
}
int main() {
// textIO();
fastIO();
long long n, q;
cin >> n;
cin >> str;
build(1, 1, n);
cin >> q;
while (q--) {
long long qr, ind, l, r;
char val;
cin >> qr;
if (qr == 1) {
cin >> ind >> val;
update(1, ind, val, 1, n);
} else {
cin >> l >> r;
cout << __builtin_popcount(query(1, 1, n, l, r)) << '\n';
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void textIO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void fastIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
#define mx 500005
int tree[4 * mx];
string str;
void build(int node, int s, int e) {
if (s == e) {
tree[node] = (1 << (str[s - 1] - 'a'));
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (s + e) / 2;
build(left, s, mid);
build(right, mid + 1, e);
tree[node] = tree[left] | tree[right];
return;
}
int query(int node, int s, int e, int l, int r) {
if (l > e || r < s) {
return 0;
}
if (l <= s && e <= r) {
return tree[node];
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (s + e) / 2;
return query(left, s, mid, l, r) | query(right, mid + 1, e, l, r);
}
void update(int node, int ind, char val, int s, int e) {
if (s == ind && e == ind) {
tree[node] = (1 << (val - 'a'));
return;
}
if (ind < s || ind > e) {
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (s + e) / 2;
update(left, ind, val, s, mid);
update(right, ind, val, mid + 1, e);
tree[node] = tree[left] | tree[right];
return;
}
int main() {
// textIO();
fastIO();
long long n, q;
cin >> n;
cin >> str;
build(1, 1, n);
cin >> q;
while (q--) {
long long qr, ind, l, r;
char val;
cin >> qr;
if (qr == 1) {
cin >> ind >> val;
update(1, ind, val, 1, n);
} else {
cin >> l >> r;
cout << __builtin_popcount(query(1, 1, n, l, r)) << '\n';
}
}
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p02763 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <regex>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define LINF (LLONG_MAX / 2)
#define EPS (1.0e-8)
#define mkpr make_pair
const int MGN = 10;
const int ARY_SZ_MAX = (int)1e7;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;
struct BIT {
vi a;
vi sg; // セグメント
int N;
BIT(int n) {
N = n;
a = vi(N + 1, 0);
sg = vi(N + 1, 0);
}
// a1+a2+...+ai を求める
int sum(int i) {
int res = 0;
while (i > 0) {
res += sg[i];
i -= i & -i; // 末尾のビット値を減算
}
return res;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
// ai に x を加える
void add(int i, int x) {
while (i <= N) {
sg[i] += x;
i += i & -i; // 末尾のビット値を加算
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
string S;
cin >> S;
S = "0" + S;
vector<BIT> vbit(26, BIT(N));
FOR(i, 1, N + 1) { vbit[S[i] - 'a'].add(i, 1); }
int Q;
cin >> Q;
REP(i, Q) {
int Typ;
cin >> Typ;
if (Typ == 1) {
int I;
char C;
cin >> I >> C;
if (S[i] != C) {
vbit[S[I] - 'a'].add(I, -1);
vbit[C - 'a'].add(I, 1);
S[I] = C;
}
} else {
assert(Typ == 2);
int L, R;
cin >> L >> R;
int ans = 0;
REP(j, 26) {
if (vbit[j].sum(L, R) > 0)
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| #include "bits/stdc++.h"
#include <regex>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define LINF (LLONG_MAX / 2)
#define EPS (1.0e-8)
#define mkpr make_pair
const int MGN = 10;
const int ARY_SZ_MAX = (int)1e7;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;
struct BIT {
vi a;
vi sg; // セグメント
int N;
BIT(int n) {
N = n;
a = vi(N + 1, 0);
sg = vi(N + 1, 0);
}
// a1+a2+...+ai を求める
int sum(int i) {
int res = 0;
while (i > 0) {
res += sg[i];
i -= i & -i; // 末尾のビット値を減算
}
return res;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
// ai に x を加える
void add(int i, int x) {
while (i <= N) {
sg[i] += x;
i += i & -i; // 末尾のビット値を加算
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
string S;
cin >> S;
S = "0" + S;
vector<BIT> vbit(26, BIT(N));
FOR(i, 1, N + 1) { vbit[S[i] - 'a'].add(i, 1); }
int Q;
cin >> Q;
REP(i, Q) {
int Typ;
cin >> Typ;
if (Typ == 1) {
int I;
char C;
cin >> I >> C;
if (S[I] != C) {
vbit[S[I] - 'a'].add(I, -1);
vbit[C - 'a'].add(I, 1);
S[I] = C;
}
} else {
assert(Typ == 2);
int L, R;
cin >> L >> R;
int ans = 0;
REP(j, 26) {
if (vbit[j].sum(L, R) > 0)
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| replace | 79 | 80 | 79 | 80 | 0 | |
p02763 | C++ | Runtime Error | // Md.Fahd
// HSTU CSE18
#include <bits/stdc++.h>
#define READ_IN freopen("Input.txt", "r", stdin);
using namespace std;
const int maxe = 100005;
struct can {
int aa[26];
} tree[maxe * 3];
char ar[maxe];
void make(int node, int b, int e) {
if (b == e) {
tree[node].aa[ar[b] - 'a']++;
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
make(left, b, mid);
make(right, mid + 1, e);
for (int i = 0; i < 26; i++) {
tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
}
// tree[node].aa=tree[left].aa + tree[right].aa;
}
vector<int> ck(int node, int b, int e, int i, int j) {
if (i > e || j < b) {
vector<int> as(26, 0);
return as;
}
if (b >= i && e <= j) {
vector<int> as(26, 0);
for (int i = 0; i < 26; i++) {
as[i] = tree[node].aa[i];
}
return as;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
vector<int> p1(26, 0);
p1 = ck(left, b, mid, i, j);
vector<int> p2(26, 0);
p2 = ck(right, mid + 1, e, i, j);
vector<int> p3(26, 0);
for (int i = 0; i < 26; i++) {
p3[i] += p1[i];
p3[i] += p2[i];
}
return p3;
}
void update(int node, int b, int e, int i, int newvalue) {
if (i > e || i < b)
return;
if (b == i && e == i) {
tree[node].aa[ar[b] - 'a'] = 0;
for (int i = 0; i < 26; i++) {
tree[node].aa[i] = 0;
}
ar[b] = newvalue;
tree[node].aa[newvalue - 'a']++;
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
update(left, b, mid, i, newvalue);
update(right, mid + 1, e, i, newvalue);
for (int i = 0; i < 26; i++) {
tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
}
}
int main(void) {
// READ_IN
int ff;
cin >> ff;
string ss;
cin >> ss;
for (int i = 1; i <= ss.size(); i++) {
ar[i] = ss[i - 1];
}
int n = ss.size();
make(1, 1, n);
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) {
int index;
cin >> index;
char value;
cin >> value;
update(1, 1, n, index, value);
} else {
int l, r;
cin >> l >> r;
vector<int> aa(26, 0);
aa = ck(1, 1, n, l, r);
int cnt = 0;
for (int i = 0; i < 26; i++) {
if (aa[i] > 0)
cnt++;
}
cout << cnt << endl;
}
}
return 0;
}
| // Md.Fahd
// HSTU CSE18
#include <bits/stdc++.h>
#define READ_IN freopen("Input.txt", "r", stdin);
using namespace std;
const int maxe = 500005;
struct can {
int aa[26];
} tree[maxe * 3];
char ar[maxe];
void make(int node, int b, int e) {
if (b == e) {
tree[node].aa[ar[b] - 'a']++;
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
make(left, b, mid);
make(right, mid + 1, e);
for (int i = 0; i < 26; i++) {
tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
}
// tree[node].aa=tree[left].aa + tree[right].aa;
}
vector<int> ck(int node, int b, int e, int i, int j) {
if (i > e || j < b) {
vector<int> as(26, 0);
return as;
}
if (b >= i && e <= j) {
vector<int> as(26, 0);
for (int i = 0; i < 26; i++) {
as[i] = tree[node].aa[i];
}
return as;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
vector<int> p1(26, 0);
p1 = ck(left, b, mid, i, j);
vector<int> p2(26, 0);
p2 = ck(right, mid + 1, e, i, j);
vector<int> p3(26, 0);
for (int i = 0; i < 26; i++) {
p3[i] += p1[i];
p3[i] += p2[i];
}
return p3;
}
void update(int node, int b, int e, int i, int newvalue) {
if (i > e || i < b)
return;
if (b == i && e == i) {
tree[node].aa[ar[b] - 'a'] = 0;
for (int i = 0; i < 26; i++) {
tree[node].aa[i] = 0;
}
ar[b] = newvalue;
tree[node].aa[newvalue - 'a']++;
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
update(left, b, mid, i, newvalue);
update(right, mid + 1, e, i, newvalue);
for (int i = 0; i < 26; i++) {
tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
}
}
int main(void) {
// READ_IN
int ff;
cin >> ff;
string ss;
cin >> ss;
for (int i = 1; i <= ss.size(); i++) {
ar[i] = ss[i - 1];
}
int n = ss.size();
make(1, 1, n);
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) {
int index;
cin >> index;
char value;
cin >> value;
update(1, 1, n, index, value);
} else {
int l, r;
cin >> l >> r;
vector<int> aa(26, 0);
aa = ck(1, 1, n, l, r);
int cnt = 0;
for (int i = 0; i < 26; i++) {
if (aa[i] > 0)
cnt++;
}
cout << cnt << endl;
}
}
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
struct segtree {
int N;
vector<int> dat;
segtree(int N_) : dat(N_ * 2) {
int x = 1;
while (N_ > x) {
x *= 2;
}
N = x;
}
void update(int i, int x) {
i += N - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] = dat[i * 2 + 1] | dat[i * 2 + 2]; // 演算
}
}
int query(int a, int b) { return query_sub(a, b, 0, 0, N); }
int query_sub(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0; // モノイド
else if (a <= l && r <= b)
return dat[k];
else {
int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return vl | vr; // 演算
}
}
};
int main() {
int n;
cin >> n;
string s;
cin >> s;
segtree tr(n);
REP(i, n) { tr.update(i, 1 << (s[i] - 'a')); }
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
tr.update(b - 1, 1 << (c - 'a'));
} else {
int l, r;
cin >> l >> r;
int k = tr.query(l - 1, r);
int ans = 0;
REP(i, 26) ans += (k >> i) & 1;
cout << ans << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
struct segtree {
int N;
vector<int> dat;
segtree(int N_) : dat(N_ * 4) {
int x = 1;
while (N_ > x) {
x *= 2;
}
N = x;
}
void update(int i, int x) {
i += N - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] = dat[i * 2 + 1] | dat[i * 2 + 2]; // 演算
}
}
int query(int a, int b) { return query_sub(a, b, 0, 0, N); }
int query_sub(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0; // モノイド
else if (a <= l && r <= b)
return dat[k];
else {
int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return vl | vr; // 演算
}
}
};
int main() {
int n;
cin >> n;
string s;
cin >> s;
segtree tr(n);
REP(i, n) { tr.update(i, 1 << (s[i] - 'a')); }
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
tr.update(b - 1, 1 << (c - 'a'));
} else {
int l, r;
cin >> l >> r;
int k = tr.query(l - 1, r);
int ans = 0;
REP(i, 26) ans += (k >> i) & 1;
cout << ans << endl;
}
}
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q;
const int maxn = 5e5 + 5;
int lt[maxn];
set<int> a[26];
bool check(int k, int l, int r) {
int b;
if (a[k].lower_bound(l) == a[k].end())
return false;
b = *a[k].lower_bound(l);
// cout << "csy" << ' ' << k << ' ' << b << endl;
if (b <= r)
return true;
else
return false;
}
int calc(int l, int r) {
int ans = 0;
for (int i = 0; i < 26; i++) {
if (check(i, l, r))
ans++;
}
return ans;
}
int main() {
cin >> n;
char p;
for (int i = 1; i <= n; i++) {
p = getchar();
if (p == '\n') {
i--;
continue;
}
a[(int)p - 97].insert(i);
lt[i] = (int)p - 97;
}
cin >> q;
for (int i = 1, t, nx, ny; i <= q; i++) {
cin >> t;
if (t == 1) {
cin >> nx >> p;
ny = (int)p - 97;
if (lt[nx] == ny)
continue;
else {
a[lt[nx]].erase(a[lt[nx]].find(nx));
a[ny].insert(nx);
}
} else {
cin >> nx >> ny;
cout << calc(nx, ny) << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q;
const int maxn = 5e5 + 5;
int lt[maxn];
set<int> a[26];
bool check(int k, int l, int r) {
int b;
if (a[k].lower_bound(l) == a[k].end())
return false;
b = *a[k].lower_bound(l);
// cout << "csy" << ' ' << k << ' ' << b << endl;
if (b <= r)
return true;
else
return false;
}
int calc(int l, int r) {
int ans = 0;
for (int i = 0; i < 26; i++) {
if (check(i, l, r))
ans++;
}
return ans;
}
int main() {
cin >> n;
char p;
for (int i = 1; i <= n; i++) {
p = getchar();
if (p == '\n') {
i--;
continue;
}
a[(int)p - 97].insert(i);
lt[i] = (int)p - 97;
}
cin >> q;
for (int i = 1, t, nx, ny; i <= q; i++) {
cin >> t;
if (t == 1) {
cin >> nx >> p;
ny = (int)p - 97;
if (lt[nx] == ny)
continue;
else {
a[lt[nx]].erase(a[lt[nx]].find(nx));
a[ny].insert(nx);
lt[nx] = ny;
}
} else {
cin >> nx >> ny;
cout << calc(nx, ny) << endl;
}
}
return 0;
} | insert | 52 | 52 | 52 | 53 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i) // a ≦ i < b
#define Rrep(i, a, b) \
for (int64_t i = (a); i >= (b); --i) // reverse repeat. a から b まで減少.
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend() // 逆イテレータ
#define RANGE(a, b, c) (a).begin() + (b), (a).begin() + (c) // [b,c) イテレータ
#define INF 1000000000000000
#define MOD 1000000007
using PII = pair<int64_t, int64_t>;
using VI = vector<int64_t>;
using VVI = vector<VI>;
using VS = vector<string>;
using VP = vector<PII>;
using i64 = int64_t;
template <typename T> void invec(vector<T> &A) {
for (T &a : A)
cin >> a;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q;
string S;
cin >> N >> S >> Q;
vector<set<int>> pos(26, set<int>({1 << 30}));
rep(i, 0, N) { pos[S[i] - 'a'].insert(i); }
while (Q--) {
int q;
cin >> q;
if (q == 1) {
int i;
char c;
cin >> i >> c;
pos[S[i - 1] - 'a'].erase(i - 1);
pos[c - 'a'].insert(i - 1);
S[i - 1] = c;
} else {
int l, r;
cin >> l >> r;
int c = 0;
for (set<int> p : pos)
if (!p.empty())
if (*p.lower_bound(l - 1) < *p.lower_bound(r))
++c;
cout << c << "\n";
}
}
return 0;
}
// コンテスト前はtemplate振り返り。
// 書いて考える.場合分け.情報整理.
// 単純に分かる量から.愚直解を実装して研究.
// 境界,出力文字列 チェック.行末にスペース入れない.
// 可読性優先.高速化次点.
// まずは全探索,分割,次にDP(小さい問題から大きな問題)
// 制限を見る.境界に注意.求めたい量の変域.動かせる量.
// 偶奇,逆から,ソート,出現回数,出現位置,DP, 余事象,包除
// データ構造. 問題の特徴量.単調性→二分探索
// 存在判定:構成方法,入力の特徴
// 構築問題。極端解を試す。
// 例外を十分に含む一般化.想像力の限界
// 小さい系から例示
// 代数的処理.前処理によるクエリ高速化.
// 始めは過剰に例示・場合分けしてもいい.各場合を確実に対処.
// 自明な例から処理,除外.
// 小数のときは,精度の設定する.doubel 変数に数値を入力するときは 123. とする.
// コーナーケース。境界値。数表出力プログラム作る
// 実行エラー:vector添え字超え.0割り.
| #include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i) // a ≦ i < b
#define Rrep(i, a, b) \
for (int64_t i = (a); i >= (b); --i) // reverse repeat. a から b まで減少.
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend() // 逆イテレータ
#define RANGE(a, b, c) (a).begin() + (b), (a).begin() + (c) // [b,c) イテレータ
#define INF 1000000000000000
#define MOD 1000000007
using PII = pair<int64_t, int64_t>;
using VI = vector<int64_t>;
using VVI = vector<VI>;
using VS = vector<string>;
using VP = vector<PII>;
using i64 = int64_t;
template <typename T> void invec(vector<T> &A) {
for (T &a : A)
cin >> a;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q;
string S;
cin >> N >> S >> Q;
vector<set<int>> pos(26, set<int>({1 << 30}));
rep(i, 0, N) { pos[S[i] - 'a'].insert(i); }
while (Q--) {
int q;
cin >> q;
if (q == 1) {
int i;
char c;
cin >> i >> c;
pos[S[i - 1] - 'a'].erase(i - 1);
pos[c - 'a'].insert(i - 1);
S[i - 1] = c;
} else {
int l, r;
cin >> l >> r;
int c = 0;
rep(i, 0, 26) {
if (pos[i].empty())
continue;
if (*pos[i].lower_bound(l - 1) < *pos[i].lower_bound(r))
++c;
}
cout << c << "\n";
}
}
return 0;
}
// コンテスト前はtemplate振り返り。
// 書いて考える.場合分け.情報整理.
// 単純に分かる量から.愚直解を実装して研究.
// 境界,出力文字列 チェック.行末にスペース入れない.
// 可読性優先.高速化次点.
// まずは全探索,分割,次にDP(小さい問題から大きな問題)
// 制限を見る.境界に注意.求めたい量の変域.動かせる量.
// 偶奇,逆から,ソート,出現回数,出現位置,DP, 余事象,包除
// データ構造. 問題の特徴量.単調性→二分探索
// 存在判定:構成方法,入力の特徴
// 構築問題。極端解を試す。
// 例外を十分に含む一般化.想像力の限界
// 小さい系から例示
// 代数的処理.前処理によるクエリ高速化.
// 始めは過剰に例示・場合分けしてもいい.各場合を確実に対処.
// 自明な例から処理,除外.
// 小数のときは,精度の設定する.doubel 変数に数値を入力するときは 123. とする.
// コーナーケース。境界値。数表出力プログラム作る
// 実行エラー:vector添え字超え.0割り.
| replace | 48 | 52 | 48 | 54 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// 最大公約数
ll gcd(ll x, ll y) {
ll tmp = 0;
if (x < y) {
tmp = x;
x = y;
y = tmp;
}
while (y > 0) {
ll r = x % y;
x = y;
y = r;
}
return x;
}
// 最大公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
const int MAX = 1e6 + 1;
const ll MOD = 1e9 + 7;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 階乗
ll kaijo(ll k) {
ll sum = 1;
for (ll i = 1; i <= k; ++i) {
sum *= i;
sum %= 1000000000 + 7;
}
return sum;
}
long long modpow(long long a, long long n, long long MOD) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
// sieve(MAX);でベクトルIsPrimeに「MAXまでの数値の素数の真偽」を格納する。
struct Sieve {
int n;
vector<int> f, primes;
Sieve(int n = 1) : n(n), f(n + 1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; ++i) {
if (f[i])
continue;
primes.push_back(i);
f[i] = i;
for (ll j = i * i; j <= n; j += i) {
if (!f[j])
f[j] = i;
}
}
}
bool isPrime(int x) { return f[x] == x; }
vector<int> factorList(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
vector<P> factor(int x) {
vector<int> fl = factorList(x);
if (fl.size() == 0)
return {};
vector<P> res(1, P(fl[0], 0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
};
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
ll count(int n, int a) {
ll bunshi = 1;
for (int i = 0; i < a; i++) {
bunshi *= (n - i);
bunshi %= MOD;
}
ll bunbo = 1;
for (int i = 1; i < a + 1; i++) {
bunbo *= i;
bunbo %= MOD;
}
bunbo = modpow(bunbo, MOD - 2, MOD);
// cout<<(bunshi*bunbo)%MOD<<endl;
return (bunshi * bunbo) % MOD;
}
// 約数列挙。約数をvector<ll>で返す。計算量はsqrt(n)。
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
// ここから開始
const ll INF = 1e18;
int main() {
int N;
string s;
int Q;
cin >> N >> s >> Q;
set<int> st[26];
for (int i = 0; i < N; i++) {
st[s[i] - 'a'].insert(i);
}
int q[Q][3];
for (int i = 0; i < Q; i++) {
int f;
cin >> f;
if (f == 1) {
q[i][0] = f;
int fx;
cin >> fx;
fx--;
char fc;
cin >> fc;
int cc = fc - 'a';
q[i][1] = fx, q[i][2] = cc;
} else {
q[i][0] = f;
cin >> q[i][1] >> q[i][2];
q[i][1]--;
q[i][2]--;
}
}
for (int i = 0; i < Q; i++) {
int f = q[i][0];
if (f == 1) {
int cnum = q[i][2];
int fi = q[i][1];
st[s[fi] - 'a'].erase(fi);
st[cnum].insert(fi);
s[fi] = (char)(cnum + 'a');
} else {
int fl = q[i][1];
int fr = q[i][2];
int ans = 0;
for (int j = 0; j < 26; j++) {
auto x = lower_bound(st[j].begin(), st[j].end(), fl);
if (x != st[j].end()) {
if (*x <= fr)
ans++;
}
}
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// 最大公約数
ll gcd(ll x, ll y) {
ll tmp = 0;
if (x < y) {
tmp = x;
x = y;
y = tmp;
}
while (y > 0) {
ll r = x % y;
x = y;
y = r;
}
return x;
}
// 最大公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
const int MAX = 1e6 + 1;
const ll MOD = 1e9 + 7;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 階乗
ll kaijo(ll k) {
ll sum = 1;
for (ll i = 1; i <= k; ++i) {
sum *= i;
sum %= 1000000000 + 7;
}
return sum;
}
long long modpow(long long a, long long n, long long MOD) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
// sieve(MAX);でベクトルIsPrimeに「MAXまでの数値の素数の真偽」を格納する。
struct Sieve {
int n;
vector<int> f, primes;
Sieve(int n = 1) : n(n), f(n + 1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; ++i) {
if (f[i])
continue;
primes.push_back(i);
f[i] = i;
for (ll j = i * i; j <= n; j += i) {
if (!f[j])
f[j] = i;
}
}
}
bool isPrime(int x) { return f[x] == x; }
vector<int> factorList(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
vector<P> factor(int x) {
vector<int> fl = factorList(x);
if (fl.size() == 0)
return {};
vector<P> res(1, P(fl[0], 0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
};
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
ll count(int n, int a) {
ll bunshi = 1;
for (int i = 0; i < a; i++) {
bunshi *= (n - i);
bunshi %= MOD;
}
ll bunbo = 1;
for (int i = 1; i < a + 1; i++) {
bunbo *= i;
bunbo %= MOD;
}
bunbo = modpow(bunbo, MOD - 2, MOD);
// cout<<(bunshi*bunbo)%MOD<<endl;
return (bunshi * bunbo) % MOD;
}
// 約数列挙。約数をvector<ll>で返す。計算量はsqrt(n)。
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
// ここから開始
const ll INF = 1e18;
int main() {
int N;
string s;
int Q;
cin >> N >> s >> Q;
set<int> st[26];
for (int i = 0; i < N; i++) {
st[s[i] - 'a'].insert(i);
}
int q[Q][3];
for (int i = 0; i < Q; i++) {
int f;
cin >> f;
if (f == 1) {
q[i][0] = f;
int fx;
cin >> fx;
fx--;
char fc;
cin >> fc;
int cc = fc - 'a';
q[i][1] = fx, q[i][2] = cc;
} else {
q[i][0] = f;
cin >> q[i][1] >> q[i][2];
q[i][1]--;
q[i][2]--;
}
}
for (int i = 0; i < Q; i++) {
int f = q[i][0];
if (f == 1) {
int cnum = q[i][2];
int fi = q[i][1];
st[s[fi] - 'a'].erase(fi);
st[cnum].insert(fi);
s[fi] = (char)(cnum + 'a');
} else {
int fl = q[i][1];
int fr = q[i][2];
int ans = 0;
for (int j = 0; j < 26; j++) {
auto x = st[j].lower_bound(fl);
if (x != st[j].end()) {
if (*x <= fr)
ans++;
}
}
cout << ans << endl;
}
}
return 0;
}
| replace | 276 | 277 | 276 | 277 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
const int N = 1e6 + 50;
int node[N][28], num[N], u, v, type, ql, qr, p, n, q;
int alpha[30];
char s[N], ch;
void update(int o, int L, int R) {
int M = L + (R - L) / 2;
if (L == R) {
node[o][u]--;
node[o][v]++;
} else {
// int cnt = 0;
if (p <= M)
update(o * 2, L, M);
else
update(o * 2 + 1, M + 1, R);
for (int i = 0; i < 26; i++) {
node[o][i] = node[o * 2][i] + node[o * 2 + 1][i];
}
}
}
void query(int o, int L, int R) {
int M = L + (R - L) / 2;
if (ql <= L && R <= qr) {
for (int i = 0; i < 26; i++)
alpha[i] += node[o][i];
return;
}
if (ql <= M)
query(o * 2, L, M);
if (M < qr)
query(o * 2 + 1, M + 1, R);
}
int main() {
cin >> n;
scanf("%s", s);
for (int i = 1; i <= n; i++) {
num[i] = s[i - 1] - 'a';
u = 27;
v = num[i];
p = i;
update(1, 1, n);
}
cin >> q;
while (q--) {
cin >> type;
if (type == 2) {
scanf("%d%d", &ql, &qr);
memset(alpha, 0, sizeof(alpha));
query(1, 1, n);
int ret = 0;
for (int i = 0; i < 26; i++) {
if (alpha[i] > 0)
ret++;
}
cout << ret << endl;
} else {
cin >> ql >> ch;
u = num[ql];
v = ch - 'a';
num[ql] = v;
p = ql;
update(1, 1, n);
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 50;
int node[N][28], num[N], u, v, type, ql, qr, p, n, q;
int alpha[30];
char s[N], ch;
void update(int o, int L, int R) {
int M = L + (R - L) / 2;
if (L == R) {
node[o][u]--;
node[o][v]++;
} else {
// int cnt = 0;
if (p <= M)
update(o * 2, L, M);
else
update(o * 2 + 1, M + 1, R);
for (int i = 0; i < 26; i++) {
node[o][i] = node[o * 2][i] + node[o * 2 + 1][i];
}
}
}
void query(int o, int L, int R) {
int M = L + (R - L) / 2;
if (ql <= L && R <= qr) {
for (int i = 0; i < 26; i++)
alpha[i] += node[o][i];
return;
}
if (ql <= M)
query(o * 2, L, M);
if (M < qr)
query(o * 2 + 1, M + 1, R);
}
int main() {
cin >> n;
scanf("%s", s);
for (int i = 1; i <= n; i++) {
num[i] = s[i - 1] - 'a';
u = 27;
v = num[i];
p = i;
update(1, 1, n);
}
cin >> q;
while (q--) {
cin >> type;
if (type == 2) {
scanf("%d%d", &ql, &qr);
memset(alpha, 0, sizeof(alpha));
query(1, 1, n);
int ret = 0;
for (int i = 0; i < 26; i++) {
if (alpha[i] > 0)
ret++;
}
cout << ret << endl;
} else {
cin >> ql >> ch;
u = num[ql];
v = ch - 'a';
num[ql] = v;
p = ql;
update(1, 1, n);
}
}
return 0;
}
| replace | 2 | 5 | 2 | 3 | -11 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;
template <typename T> bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T> bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
signed main() {
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> v(26);
for (int i = 0; i < n; ++i)
v[s[i] - 'a'].insert(i);
for (int i = 0; i < q; ++i) {
int type;
cin >> type;
if (type == 1) {
int idx;
char c;
cin >> idx >> c;
--idx;
v[s[i] - 'a'].erase(idx);
v[c - 'a'].insert(idx);
s[idx] = c;
} else {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
for (int j = 0; j < 26; ++j)
ans += (v[j].lower_bound(l) != v[j].lower_bound(r));
cout << ans << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;
template <typename T> bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T> bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
signed main() {
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> v(26);
for (int i = 0; i < n; ++i)
v[s[i] - 'a'].insert(i);
for (int i = 0; i < q; ++i) {
int type;
cin >> type;
if (type == 1) {
int idx;
char c;
cin >> idx >> c;
--idx;
v[s[idx] - 'a'].erase(idx);
v[c - 'a'].insert(idx);
s[idx] = c;
} else {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
for (int j = 0; j < 26; ++j)
ans += (v[j].lower_bound(l) != v[j].lower_bound(r));
cout << ans << endl;
}
}
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;
/*
18 26
*/
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> a(26);
for (int i = 0; i < n; i++) {
a[s[i] - 'a'].insert(i);
}
for (int i = 0; i < q; i++) {
int type;
cin >> type;
if (type == 1) {
int index;
char new_c;
cin >> index >> new_c;
index--;
a[s[index] - 'a'].erase(index);
s[index] = new_c;
a[s[index] - 'a'].insert(index);
} else {
int l, r;
cin >> l >> r;
l--, r--;
int cnt = 0;
for (int j = 0; j < 26; j++) {
auto lower_it = lower_bound(a[j].begin(), a[j].end(), l);
auto upper_it = upper_bound(a[j].begin(), a[j].end(), r);
if (lower_it != upper_it)
cnt++;
}
cout << cnt << endl;
}
}
} | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;
/*
18 26
*/
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> a(26);
for (int i = 0; i < n; i++) {
a[s[i] - 'a'].insert(i);
}
for (int i = 0; i < q; i++) {
int type;
cin >> type;
if (type == 1) {
int index;
char new_c;
cin >> index >> new_c;
index--;
a[s[index] - 'a'].erase(index);
s[index] = new_c;
a[s[index] - 'a'].insert(index);
} else {
int l, r;
cin >> l >> r;
l--, r--;
int cnt = 0;
for (int j = 0; j < 26; j++) {
auto lower_it = a[j].lower_bound(l);
auto upper_it = a[j].upper_bound(r);
if (lower_it != upper_it)
cnt++;
}
cout << cnt << endl;
}
}
} | replace | 48 | 50 | 48 | 50 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n;
string s;
int q;
set<int> occ[26];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
cin >> n >> s >> q;
for (int i = 0; i < n; i++)
occ[(int)s[i] - (int)'a'].insert(i + 1);
for (int _ = 0; _ < q; _++) {
int type;
cin >> type;
if (type == 1) {
int x;
char y;
cin >> x >> y;
if (s[x - 1] != y) {
occ[(int)s[x - 1] - (int)'a'].erase(x);
occ[(int)y - (int)'a'].insert(x);
s[x - 1] = y;
}
} else {
int x, y;
cin >> x >> y;
int ans = 0;
for (int i = 0; i < 26; i++) {
auto l = lower_bound(occ[i].begin(), occ[i].end(), x);
if (l != occ[i].end() && *l <= y)
ans++;
}
cout << ans << "\n";
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n;
string s;
int q;
set<int> occ[26];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
cin >> n >> s >> q;
for (int i = 0; i < n; i++)
occ[(int)s[i] - (int)'a'].insert(i + 1);
for (int _ = 0; _ < q; _++) {
int type;
cin >> type;
if (type == 1) {
int x;
char y;
cin >> x >> y;
if (s[x - 1] != y) {
occ[(int)s[x - 1] - (int)'a'].erase(x);
occ[(int)y - (int)'a'].insert(x);
s[x - 1] = y;
}
} else {
int x, y;
cin >> x >> y;
int ans = 0;
for (int i = 0; i < 26; i++) {
auto l = occ[i].lower_bound(x);
if (l != occ[i].end() && *l <= y)
ans++;
}
cout << ans << "\n";
}
}
return 0;
}
| replace | 35 | 36 | 35 | 36 | TLE | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
ll N, Q;
string S;
set<int> st[26];
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> S >> Q;
FOR(i, 0, N) {
int k = int(S[i] - 'a');
st[k].insert(i);
}
FOR(i, 0, Q) {
int kind;
cin >> kind;
if (kind == 1) {
int j;
char c;
cin >> j >> c;
j--;
c = int(c - 'a');
st[int(S[j] - 'a')].erase(j);
st[int(c)].insert(j);
S[j] = char(c);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
FOR(j, 0, 26) {
auto p = st[j].lower_bound(l);
if (p != st[j].end() && *p <= r)
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| #include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
ll N, Q;
string S;
set<int> st[26];
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> S >> Q;
FOR(i, 0, N) {
int k = int(S[i] - 'a');
st[k].insert(i);
}
FOR(i, 0, Q) {
int kind;
cin >> kind;
if (kind == 1) {
int j;
char c;
cin >> j >> c;
j--;
c = int(c - 'a');
st[int(S[j] - 'a')].erase(j);
st[int(c)].insert(j);
S[j] = char(c + 'a');
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
FOR(j, 0, 26) {
auto p = st[j].lower_bound(l);
if (p != st[j].end() && *p <= r)
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| replace | 61 | 62 | 61 | 62 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// BIT
template <typename T> struct BIT {
int N;
int max_2beki;
vector<T> data;
// 初期化 1-indexedでデータを管理する 0で初期化
BIT(int size) {
N = ++size;
data.assign(N, 0);
max_2beki = 1;
while (max_2beki * 2 <= N)
max_2beki *= 2;
}
// [0,k](閉区間)の総和 閉区間に注意!
T sum(int k) {
if (k < 0)
return 0; // k<0のとき0を返す
T ret = 0;
for (++k; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
// [l,r](閉区間)の総和
inline T sum(int l, int r) { return sum(r) - sum(l - 1); }
// 一点取得 更新はできないことに注意
inline T operator[](int k) { return sum(k) - sum(k - 1); }
// data[k] += x;
void add(int k, T x) {
for (++k; k < N; k += k & -k)
data[k] += x;
}
// imos法 [l,r]にxを加算
void imos(int l, int r, T x) {
add(l, x);
add(r + 1, -x);
}
// lower_bound sum(i)がval以上となる最小のi
int lower_bound(T w) {
if (w <= 0)
return 0;
int x = 0;
for (int k = max_2beki; k > 0; k /= 2) {
if (x + k <= N - 1 && data[x + k] < w) {
w -= data[x + k];
x += k;
}
}
return x;
}
// upper_bound sum(i)がvalより大きくなる最小のi
int upper_bound(T w) {
if (w < 0)
return 0;
int x = 0;
for (int k = max_2beki; k > 0; k /= 2) {
if (x + k <= N - 1 && data[x + k] <= w) {
w -= data[x + k];
x += k;
}
}
return x;
}
};
int main() {
int n;
cin >> n;
vector<BIT<int>> bit(26, BIT<int>(n + 10));
string s;
cin >> s;
for (int i = 0; i < n; i++) {
int p = s[i] - 'a';
bit[p].add(i, 1);
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
if (x == 1) {
int i;
cin >> i;
i--;
char c;
cin >> c;
int p = c - 'a';
bit[p].add(i, 1);
bit[s[i]].add(i, -1);
s[i] = c;
} else {
int l, r;
cin >> l >> r;
l--, r--;
int res = 0;
for (int i = 0; i < 26; i++) {
if (bit[i].sum(l, r)) {
res++;
}
}
cout << res << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
// BIT
template <typename T> struct BIT {
int N;
int max_2beki;
vector<T> data;
// 初期化 1-indexedでデータを管理する 0で初期化
BIT(int size) {
N = ++size;
data.assign(N, 0);
max_2beki = 1;
while (max_2beki * 2 <= N)
max_2beki *= 2;
}
// [0,k](閉区間)の総和 閉区間に注意!
T sum(int k) {
if (k < 0)
return 0; // k<0のとき0を返す
T ret = 0;
for (++k; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
// [l,r](閉区間)の総和
inline T sum(int l, int r) { return sum(r) - sum(l - 1); }
// 一点取得 更新はできないことに注意
inline T operator[](int k) { return sum(k) - sum(k - 1); }
// data[k] += x;
void add(int k, T x) {
for (++k; k < N; k += k & -k)
data[k] += x;
}
// imos法 [l,r]にxを加算
void imos(int l, int r, T x) {
add(l, x);
add(r + 1, -x);
}
// lower_bound sum(i)がval以上となる最小のi
int lower_bound(T w) {
if (w <= 0)
return 0;
int x = 0;
for (int k = max_2beki; k > 0; k /= 2) {
if (x + k <= N - 1 && data[x + k] < w) {
w -= data[x + k];
x += k;
}
}
return x;
}
// upper_bound sum(i)がvalより大きくなる最小のi
int upper_bound(T w) {
if (w < 0)
return 0;
int x = 0;
for (int k = max_2beki; k > 0; k /= 2) {
if (x + k <= N - 1 && data[x + k] <= w) {
w -= data[x + k];
x += k;
}
}
return x;
}
};
int main() {
int n;
cin >> n;
vector<BIT<int>> bit(26, BIT<int>(n + 10));
string s;
cin >> s;
for (int i = 0; i < n; i++) {
int p = s[i] - 'a';
bit[p].add(i, 1);
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
if (x == 1) {
int i;
cin >> i;
i--;
char c;
cin >> c;
int p = c - 'a';
bit[p].add(i, 1);
bit[s[i] - 'a'].add(i, -1);
s[i] = c;
} else {
int l, r;
cin >> l >> r;
l--, r--;
int res = 0;
for (int i = 0; i < 26; i++) {
if (bit[i].sum(l, r)) {
res++;
}
}
cout << res << endl;
}
}
} | replace | 96 | 97 | 96 | 97 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 500005;
#define M 1000000007
#define double long double
#define BINF 1000000000000001
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 5000004
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
int n;
int t[26][2 * N];
void build() {
for (int j = 0; j < 26; j++)
for (int i = n - 1; i > 0; --i)
t[j][i] = t[j][i << 1] + t[j][i << 1 | 1];
}
void modify(int x, int p, int value) {
for (t[x][p += n] = value; p > 1; p >>= 1)
t[x][p >> 1] = t[x][p] + t[x][p ^ 1];
}
int query(int x, int l, int r) {
r++;
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res += t[x][l++];
if (r & 1)
res += t[x][--r];
}
return res;
}
#undef int
int main() {
#define int long long int
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("optput.txt", "w", stdout);
#endif
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++)
t[s[i] - 'a'][n + i] = 1;
build();
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
if (x == 1) {
int pos;
char c;
cin >> pos >> c;
pos--;
modify(s[pos] - 'a', pos, 0);
modify(c - 'a', pos, 1);
s[pos] = c;
} else {
int l, r;
cin >> l >> r;
l--, r--;
int ans = 0;
for (int i = 0; i < 26; i++)
ans += (query(i, l, r) > 0);
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 500005;
#define M 1000000007
#define double long double
#define BINF 1000000000000001
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 5000004
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
int n;
int t[26][2 * N];
void build() {
for (int j = 0; j < 26; j++)
for (int i = n - 1; i > 0; --i)
t[j][i] = t[j][i << 1] + t[j][i << 1 | 1];
}
void modify(int x, int p, int value) {
for (t[x][p += n] = value; p > 1; p >>= 1)
t[x][p >> 1] = t[x][p] + t[x][p ^ 1];
}
int query(int x, int l, int r) {
r++;
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res += t[x][l++];
if (r & 1)
res += t[x][--r];
}
return res;
}
#undef int
int main() {
#define int long long int
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++)
t[s[i] - 'a'][n + i] = 1;
build();
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
if (x == 1) {
int pos;
char c;
cin >> pos >> c;
pos--;
modify(s[pos] - 'a', pos, 0);
modify(c - 'a', pos, 1);
s[pos] = c;
} else {
int l, r;
cin >> l >> r;
l--, r--;
int ans = 0;
for (int i = 0; i < 26; i++)
ans += (query(i, l, r) > 0);
cout << ans << endl;
}
}
return 0;
}
| delete | 49 | 54 | 49 | 49 | -11 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main(void) {
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> is(26);
rep(i, s.size()) { is[s[i] - 'a'].insert(i); }
rep(qi, q) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
--i;
is[s[i] - 'a'].erase(i);
s[i] = c;
is[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
--l;
--r;
int ans = 0;
rep(i, 26) {
auto itr = lower_bound(is[i].begin(), is[i].end(), l);
if (itr != is[i].end() && *itr <= r)
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main(void) {
int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> is(26);
rep(i, s.size()) { is[s[i] - 'a'].insert(i); }
rep(qi, q) {
int t;
cin >> t;
if (t == 1) {
int i;
char c;
cin >> i >> c;
--i;
is[s[i] - 'a'].erase(i);
s[i] = c;
is[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
--l;
--r;
int ans = 0;
rep(i, 26) {
auto itr = is[i].lower_bound(l);
if (itr != is[i].end() && *itr <= r)
ans++;
}
cout << ans << endl;
}
}
return 0;
}
| replace | 29 | 30 | 29 | 30 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 200005;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};
LL fac[SIZE], finv[SIZE], inv[SIZE];
void combInit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < SIZE; ++i) {
fac[i] = (fac[i - 1] * i) % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = (finv[i - 1] * inv[i]) % MOD;
}
}
LL comb(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}
vector<int> Div(int n) {
vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.pb(i);
if (i * i != n)
ret.pb(n / i);
}
}
sort(all(ret));
return ret;
}
int chcnt[1000005][26];
string s;
int ch[26];
struct SegmentTree {
private:
int n;
vector<int> v;
public:
SegmentTree(string str) {
int sz = str.size();
n = 1;
while (n < sz)
n <<= 1;
v.resize(2 * n - 1, INF);
for (int i = 0; i < sz; ++i) {
chcnt[i + n - 1][str[i] - 'a'] = 1;
}
for (int i = n - 2; i >= 0; --i) {
for (int j = 0; j < 26; ++j) {
chcnt[i][j] = chcnt[i * 2 + 1][j] + chcnt[i * 2 + 2][j];
}
}
}
void Update(int x, int v) {
int t = s[x];
s[x] = v;
x += n - 1;
chcnt[x][t - 'a'] = 0;
chcnt[x][v - 'a'] = 1;
while (x > 0) {
x = (x - 1) / 2;
chcnt[x][t - 'a']--;
chcnt[x][v - 'a']++;
}
}
void query(int a, int b, int k, int l, int r = -1) {
if (r < 0) {
r = n;
}
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
for (int i = 0; i < 26; ++i) {
ch[i] += chcnt[k][i];
}
return;
}
query(a, b, 2 * k + 1, l, (l + r) / 2);
query(a, b, 2 * k + 2, (l + r) / 2, r);
return;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
cin >> s;
SegmentTree seg(s);
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
if (a == 1) {
char c;
cin >> c;
seg.Update(b - 1, c);
} else {
int c;
cin >> c;
seg.query(b - 1, c, 0, 0, -1);
int cnt = 0;
for (int i = 0; i < 26; ++i) {
if (ch[i] != 0) {
++cnt;
ch[i] = 0;
}
}
cout << cnt << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 200005;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};
LL fac[SIZE], finv[SIZE], inv[SIZE];
void combInit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < SIZE; ++i) {
fac[i] = (fac[i - 1] * i) % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = (finv[i - 1] * inv[i]) % MOD;
}
}
LL comb(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}
vector<int> Div(int n) {
vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.pb(i);
if (i * i != n)
ret.pb(n / i);
}
}
sort(all(ret));
return ret;
}
int chcnt[1 << 20][26];
string s;
int ch[26];
struct SegmentTree {
private:
int n;
vector<int> v;
public:
SegmentTree(string str) {
int sz = str.size();
n = 1;
while (n < sz)
n <<= 1;
v.resize(2 * n - 1, INF);
for (int i = 0; i < sz; ++i) {
chcnt[i + n - 1][str[i] - 'a'] = 1;
}
for (int i = n - 2; i >= 0; --i) {
for (int j = 0; j < 26; ++j) {
chcnt[i][j] = chcnt[i * 2 + 1][j] + chcnt[i * 2 + 2][j];
}
}
}
void Update(int x, int v) {
int t = s[x];
s[x] = v;
x += n - 1;
chcnt[x][t - 'a'] = 0;
chcnt[x][v - 'a'] = 1;
while (x > 0) {
x = (x - 1) / 2;
chcnt[x][t - 'a']--;
chcnt[x][v - 'a']++;
}
}
void query(int a, int b, int k, int l, int r = -1) {
if (r < 0) {
r = n;
}
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
for (int i = 0; i < 26; ++i) {
ch[i] += chcnt[k][i];
}
return;
}
query(a, b, 2 * k + 1, l, (l + r) / 2);
query(a, b, 2 * k + 2, (l + r) / 2, r);
return;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
cin >> s;
SegmentTree seg(s);
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
if (a == 1) {
char c;
cin >> c;
seg.Update(b - 1, c);
} else {
int c;
cin >> c;
seg.query(b - 1, c, 0, 0, -1);
int cnt = 0;
for (int i = 0; i < 26; ++i) {
if (ch[i] != 0) {
++cnt;
ch[i] = 0;
}
}
cout << cnt << endl;
}
}
return 0;
}
| replace | 60 | 61 | 60 | 61 | -11 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif
#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))
const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;
inline int cmp(double x, double y = 0) {
if (fabs(x - y) < EPS)
return 0;
return x > y ? 1 : -1;
}
ll exp(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
const int maxn = 2e5 + 10;
ll fat[maxn], inv[maxn];
ll C(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}
int bit[maxn][30];
void update(int idx, int l, int val) {
idx += 2;
while (idx < maxn) {
bit[idx][l] += val;
idx += idx & (-idx);
}
}
vector<int> query(int idx) {
idx += 2;
vector<int> cnt(27, 0);
while (idx) {
for (int i = 0; i < 26; i++)
cnt[i] += bit[idx][i];
idx -= idx & (-idx);
}
return cnt;
}
int queryRange(int x, int y) {
auto cntY = query(y);
auto cntX = query(x - 1);
int ans = 0;
for (int i = 0; i < 26; i++) {
// cout << i << " --> " << cntY[i] << " " << cntX[i] << endl;
cntY[i] -= cntX[i];
if (cntY[i] > 0)
ans++;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
update(i + 1, s[i] - 'a', 1);
}
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
int i;
char c;
cin >> i >> c;
update(i, s[i - 1] - 'a', -1);
update(i, c - 'a', 1);
s[i - 1] = c;
} else {
int l, r;
cin >> l >> r;
cout << (queryRange(l, r)) << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif
#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))
const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;
inline int cmp(double x, double y = 0) {
if (fabs(x - y) < EPS)
return 0;
return x > y ? 1 : -1;
}
ll exp(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
const int maxn = 6e5 + 10;
ll fat[maxn], inv[maxn];
ll C(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}
int bit[maxn][30];
void update(int idx, int l, int val) {
idx += 2;
while (idx < maxn) {
bit[idx][l] += val;
idx += idx & (-idx);
}
}
vector<int> query(int idx) {
idx += 2;
vector<int> cnt(27, 0);
while (idx) {
for (int i = 0; i < 26; i++)
cnt[i] += bit[idx][i];
idx -= idx & (-idx);
}
return cnt;
}
int queryRange(int x, int y) {
auto cntY = query(y);
auto cntX = query(x - 1);
int ans = 0;
for (int i = 0; i < 26; i++) {
// cout << i << " --> " << cntY[i] << " " << cntX[i] << endl;
cntY[i] -= cntX[i];
if (cntY[i] > 0)
ans++;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
update(i + 1, s[i] - 'a', 1);
}
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
int i;
char c;
cin >> i >> c;
update(i, s[i - 1] - 'a', -1);
update(i, c - 'a', 1);
s[i - 1] = c;
} else {
int l, r;
cin >> l >> r;
cout << (queryRange(l, r)) << endl;
}
}
return 0;
} | replace | 39 | 40 | 39 | 40 | 0 | |
p02763 | C++ | Time Limit Exceeded | /********************************
* AUTHOR: ARNAB SEN *
* NICK : arnab1729 *
* INSTITUTION: IIEST, SHIBPUR *
********************************/
#include <bits/stdc++.h>
using namespace std;
typedef string STR;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef priority_queue<int> PQI;
typedef vector<LL> VLL;
typedef vector<ULL> VULL;
typedef vector<STR> VS;
typedef vector<char> VC;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<STR, int> PSI;
typedef pair<int, STR> PIS;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<STR, int> MSI;
typedef map<char, int> MCI;
typedef map<int, STR> MIS;
typedef set<STR> SS;
typedef set<int> SI;
typedef set<LL> SLL;
#define FF first
#define SS second
#define PB push_back
#define PF push_front
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define dec(n) cout << fixed << setprecision(n);
#define f(i, n) for (LL i = 0; i < (LL)n; i++)
#define fr(i, n) for (LL i = (LL)(n - 1); i >= 0; i--)
#define fa(i, a, n) for (LL i = (LL)a; i < (LL)n; i++)
#define fra(i, a, n) for (LL i = (LL)(n - 1); i >= a; i--)
#define fsort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.rbegin(), a.rend())
#define fast \
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); \
srand(time(NULL))
template <class T> T maxT(T a, T b) { return (a > b ? a : b); }
template <class T> T minT(T a, T b) { return (a < b ? a : b); }
template <class T> void setmax(T &a, T b) { a = (a > b ? a : b); }
template <class T> void setmin(T &a, T b) { a = (a < b ? a : b); }
void fileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
}
void ofileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void clock_out() {
cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define fio() fileio()
#define ofio ofileio()
#define clock() clock_out()
#else
#define debug(...) 1
#define fio() 1
#define ofio() ofileio()
#define clock() 1
#endif
const LL MOD = 1000000007;
const LL SIZE = 100000;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LD PI = acos(-1);
const LL MAXN = numeric_limits<LL>::max();
void solve() {
int n;
cin >> n;
string s;
cin >> s;
set<int> pos[27];
for (int i = 0; i < n; i++) {
pos[s[i] - 'a'].insert(i);
}
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
int p;
char c;
cin >> p >> c;
p--;
pos[s[p] - 'a'].erase(p);
pos[c - 'a'].insert(p);
s[p] = c;
debug(s);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int i = 0; i < 26; i++) {
if (pos[i].size() == 0)
continue;
if (*pos[i].rbegin() < l)
continue;
int low = *lower_bound(pos[i].begin(), pos[i].end(), l);
if (low >= l && low <= r) {
ans++;
}
}
cout << ans << "\n";
}
}
}
int main() {
fast;
// fio();
LL t = 1;
// cin>>t;
while (t--) {
solve();
}
clock();
return 0;
}
| /********************************
* AUTHOR: ARNAB SEN *
* NICK : arnab1729 *
* INSTITUTION: IIEST, SHIBPUR *
********************************/
#include <bits/stdc++.h>
using namespace std;
typedef string STR;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef priority_queue<int> PQI;
typedef vector<LL> VLL;
typedef vector<ULL> VULL;
typedef vector<STR> VS;
typedef vector<char> VC;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<STR, int> PSI;
typedef pair<int, STR> PIS;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<STR, int> MSI;
typedef map<char, int> MCI;
typedef map<int, STR> MIS;
typedef set<STR> SS;
typedef set<int> SI;
typedef set<LL> SLL;
#define FF first
#define SS second
#define PB push_back
#define PF push_front
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define dec(n) cout << fixed << setprecision(n);
#define f(i, n) for (LL i = 0; i < (LL)n; i++)
#define fr(i, n) for (LL i = (LL)(n - 1); i >= 0; i--)
#define fa(i, a, n) for (LL i = (LL)a; i < (LL)n; i++)
#define fra(i, a, n) for (LL i = (LL)(n - 1); i >= a; i--)
#define fsort(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.rbegin(), a.rend())
#define fast \
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); \
srand(time(NULL))
template <class T> T maxT(T a, T b) { return (a > b ? a : b); }
template <class T> T minT(T a, T b) { return (a < b ? a : b); }
template <class T> void setmax(T &a, T b) { a = (a > b ? a : b); }
template <class T> void setmin(T &a, T b) { a = (a < b ? a : b); }
void fileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
}
void ofileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void clock_out() {
cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define fio() fileio()
#define ofio ofileio()
#define clock() clock_out()
#else
#define debug(...) 1
#define fio() 1
#define ofio() ofileio()
#define clock() 1
#endif
const LL MOD = 1000000007;
const LL SIZE = 100000;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LD PI = acos(-1);
const LL MAXN = numeric_limits<LL>::max();
void solve() {
int n;
cin >> n;
string s;
cin >> s;
set<int> pos[27];
for (int i = 0; i < n; i++) {
pos[s[i] - 'a'].insert(i);
}
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
int p;
char c;
cin >> p >> c;
p--;
pos[s[p] - 'a'].erase(p);
pos[c - 'a'].insert(p);
s[p] = c;
debug(s);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int i = 0; i < 26; i++) {
if (pos[i].size() == 0)
continue;
if (*pos[i].rbegin() < l)
continue;
int low = *pos[i].lower_bound(l);
if (low >= l && low <= r) {
ans++;
}
}
cout << ans << "\n";
}
}
}
int main() {
fast;
// fio();
LL t = 1;
// cin>>t;
while (t--) {
solve();
}
clock();
return 0;
}
| replace | 134 | 135 | 134 | 135 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<set<int>> is(26);
rep(i, n) { is[s[i] - 'a'].insert(i); }
int q;
cin >> q;
while (q--) {
int f;
cin >> f;
if (f == 1) {
int i;
char c;
cin >> i >> c;
i--;
is[s[i] - 'a'].erase(i);
s[i] = c;
is[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
rep(i, 26) {
auto itr = lower_bound(is[i].begin(), is[i].end(), l);
if (itr != is[i].end() && *itr <= r)
ans++;
}
printf("%d\n", ans);
}
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<set<int>> is(26);
rep(i, n) { is[s[i] - 'a'].insert(i); }
int q;
cin >> q;
while (q--) {
int f;
cin >> f;
if (f == 1) {
int i;
char c;
cin >> i >> c;
i--;
is[s[i] - 'a'].erase(i);
s[i] = c;
is[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
rep(i, 26) {
auto itr = is[i].lower_bound(l);
if (itr != is[i].end() && *itr <= r)
ans++;
}
printf("%d\n", ans);
}
}
return 0;
}
| replace | 43 | 44 | 43 | 44 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
// Def of Monoid
// Suppose that S is a set and ● is some binary opeartion S x S -> S
// then S with ● is a monoid if it satisfies the following two:
// Associativity(結合則)
// For all a,b and c in S, the equation (a ● b) ● c = a ● (b ● c) holds
// Identitiy element(単位元の存在)
// There exisits an element e in S such that for every element a in S,
// the equations e ● a = a ● e = a holds
// Eample of Monoid
//+, *, and, or, xor, min, max
// Build O(N)
// Query O(log N)
//- query(a,b) : applay operation to the range [a, b)
//- update(k,x) : change k-th element to x
//- operator[k] : return k-th element
template <typename Monoid> class SegmentTree {
private:
using F = function<Monoid(Monoid, Monoid)>;
long long sz;
vector<Monoid> seg;
const F f;
const Monoid e;
public:
SegmentTree(long long n, const F f, const Monoid &e) : f(f), e(e) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, e);
}
void set(long long k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (long long k = sz - 1; k > 0; --k) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(long long k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1)
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
Monoid query(long long a, long long b) {
Monoid L = e, R = e;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N;
cin >> N;
vector<SegmentTree<ll>> trees(
26, SegmentTree<ll>(
N + 1, [&](ll x, ll y) { return x or y; }, 0));
string S;
cin >> S;
rep(i, len(S)) trees[S[i] - 'a'].set(i, 1);
rep(i, len(S)) trees[i].build();
ll Q;
cin >> Q;
rep(_, Q) {
ll q;
cin >> q;
if (q == 1) {
ll i;
char c;
cin >> i >> c;
--i;
trees[S[i] - 'a'].update(i, 0);
trees[c - 'a'].update(i, 1);
S[i] = c;
}
if (q == 2) {
ll l, r;
cin >> l >> r;
--l;
ll ans = 0;
rep(i, 26) ans += trees[i].query(l, r);
print(ans);
}
}
return 0;
} | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
// Def of Monoid
// Suppose that S is a set and ● is some binary opeartion S x S -> S
// then S with ● is a monoid if it satisfies the following two:
// Associativity(結合則)
// For all a,b and c in S, the equation (a ● b) ● c = a ● (b ● c) holds
// Identitiy element(単位元の存在)
// There exisits an element e in S such that for every element a in S,
// the equations e ● a = a ● e = a holds
// Eample of Monoid
//+, *, and, or, xor, min, max
// Build O(N)
// Query O(log N)
//- query(a,b) : applay operation to the range [a, b)
//- update(k,x) : change k-th element to x
//- operator[k] : return k-th element
template <typename Monoid> class SegmentTree {
private:
using F = function<Monoid(Monoid, Monoid)>;
long long sz;
vector<Monoid> seg;
const F f;
const Monoid e;
public:
SegmentTree(long long n, const F f, const Monoid &e) : f(f), e(e) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, e);
}
void set(long long k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (long long k = sz - 1; k > 0; --k) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(long long k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1)
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
Monoid query(long long a, long long b) {
Monoid L = e, R = e;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N;
cin >> N;
vector<SegmentTree<ll>> trees(
26, SegmentTree<ll>(
N + 1, [&](ll x, ll y) { return x or y; }, 0));
string S;
cin >> S;
rep(i, len(S)) trees[S[i] - 'a'].set(i, 1);
rep(i, 26) trees[i].build();
ll Q;
cin >> Q;
rep(_, Q) {
ll q;
cin >> q;
if (q == 1) {
ll i;
char c;
cin >> i >> c;
--i;
trees[S[i] - 'a'].update(i, 0);
trees[c - 'a'].update(i, 1);
S[i] = c;
}
if (q == 2) {
ll l, r;
cin >> l >> r;
--l;
ll ans = 0;
rep(i, 26) ans += trees[i].query(l, r);
print(ans);
}
}
return 0;
} | replace | 201 | 202 | 201 | 202 | 0 | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES
const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18
int main() {
long n, q;
std::string s;
std::set<long> pos[26];
scanf("%ld", &n);
std::cin >> s;
scanf("%ld", &q);
for (int i = 0; i < n; i++) {
pos[s[i] - 'a'].insert(i);
}
for (int i = 0; i < q; i++) {
long x;
scanf("%ld", &x);
if (x == 1) {
long ind;
char c;
scanf("%ld %c", &ind, &c);
ind--;
// printf("%ld %c\n", ind, c);
pos[s[ind] - 'a'].erase(pos[s[ind] - 'a'].find(ind));
pos[c - 'a'].insert(ind);
/*
for(char c = 'a'; c <= 'z'; c++){
printf(" %c : (", c);
for(auto it = pos[c-'a'].begin(); it != pos[c-'a'].end(); it++){
printf("%ld ", *it);
}
printf(")\n");
}
//*/
} else {
long l, r;
scanf("%ld%ld", &l, &r);
l--;
r--;
long ans = 0;
for (int j = 0; j < 26; j++) {
if (pos[j].lower_bound(l) != pos[j].lower_bound(r + 1)) {
ans++;
}
}
printf("%ld\n", ans);
}
}
}
| #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES
const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18
int main() {
long n, q;
std::string s;
std::set<long> pos[26];
scanf("%ld", &n);
std::cin >> s;
scanf("%ld", &q);
for (int i = 0; i < n; i++) {
pos[s[i] - 'a'].insert(i);
}
for (int i = 0; i < q; i++) {
long x;
scanf("%ld", &x);
if (x == 1) {
long ind;
char c;
scanf("%ld %c", &ind, &c);
ind--;
// printf("%ld %c\n", ind, c);
pos[s[ind] - 'a'].erase(pos[s[ind] - 'a'].find(ind));
s[ind] = c;
pos[c - 'a'].insert(ind);
/*
for(char c = 'a'; c <= 'z'; c++){
printf(" %c : (", c);
for(auto it = pos[c-'a'].begin(); it != pos[c-'a'].end(); it++){
printf("%ld ", *it);
}
printf(")\n");
}
//*/
} else {
long l, r;
scanf("%ld%ld", &l, &r);
l--;
r--;
long ans = 0;
for (int j = 0; j < 26; j++) {
if (pos[j].lower_bound(l) != pos[j].lower_bound(r + 1)) {
ans++;
}
}
printf("%ld\n", ans);
}
}
}
| insert | 45 | 45 | 45 | 46 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define REP(a, b) for (int a = 0; a < (b); ++a)
#define REP1(i, n) for (int i = 1; i <= (n); ++i)
#define debug(x) cerr << #x << ": " << x << '\n'
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n", (x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n", (x) ? "Yes" : "No")
#define isPossible(x) printf("%s\n", (x) ? "Possible" : "Impossible")
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define INF (1 << 29)
// const long long INF = 1LL<<60;
#define Sp(p) cout << setprecision(25) << fixed << p << endl
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define pie 3.14159265358979323846
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
template <class T = int> T in() {
T x;
cin >> x;
return (x);
}
template <class T> void print(T &x) { cout << x << '\n'; }
const int MOD = (int)1e9 + 7;
// const int mod =(int)998244353;
const int mod = (int)1e9 + 7;
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
void COMint() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll a, ll b) {
if (a < 0)
a = -a;
if (b < 0)
b = -b;
if (b == 0)
return a;
if (a > b) {
swap(a, b);
}
return gcd(a, b % a);
}
ll lcm(ll a, ll b) {
if (a < 0)
a = -a;
if (b < 0)
b = -b;
ll g;
g = gcd(a, b);
return b / g * a;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) {
return a.second < b.second;
} else {
return a.first < b.first;
}
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) {
return a.first < b.first;
} else {
return a.second < b.second;
}
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll RS(ll N, ll P, ll m) {
if (P == 0) {
return 1;
} else {
if (P % 2 == 0) {
ll t = RS(N, P / 2, m);
return t * t % m;
} else {
return N * RS(N, P - 1, m) % m;
}
}
}
bool greater_pair(pair<ll, ll> a, pair<ll, ll> b) {
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second > b.second;
}
}
int main() {
ios::sync_with_stdio(false);
int N = in();
string S;
cin >> S;
int Q = in();
vector<set<int>> v(26);
REP(i, N) { v[S[i] - 'a'].insert(i + 1); }
REP(i, Q) {
int type = in();
if (type == 1) {
int iq;
string cq;
cin >> iq >> cq;
REP(i, 26) {
if (v[i].find(iq) != v[i].end()) {
v[i].erase(iq);
break;
}
}
v[cq[0] - 'a'].insert(iq);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
REP(i, 26) {
int temp = *lower_bound(all(v[i]), l);
if (temp >= l && temp <= r) {
ans++;
}
}
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define REP(a, b) for (int a = 0; a < (b); ++a)
#define REP1(i, n) for (int i = 1; i <= (n); ++i)
#define debug(x) cerr << #x << ": " << x << '\n'
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n", (x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n", (x) ? "Yes" : "No")
#define isPossible(x) printf("%s\n", (x) ? "Possible" : "Impossible")
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define INF (1 << 29)
// const long long INF = 1LL<<60;
#define Sp(p) cout << setprecision(25) << fixed << p << endl
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define pie 3.14159265358979323846
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
template <class T = int> T in() {
T x;
cin >> x;
return (x);
}
template <class T> void print(T &x) { cout << x << '\n'; }
const int MOD = (int)1e9 + 7;
// const int mod =(int)998244353;
const int mod = (int)1e9 + 7;
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
void COMint() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll a, ll b) {
if (a < 0)
a = -a;
if (b < 0)
b = -b;
if (b == 0)
return a;
if (a > b) {
swap(a, b);
}
return gcd(a, b % a);
}
ll lcm(ll a, ll b) {
if (a < 0)
a = -a;
if (b < 0)
b = -b;
ll g;
g = gcd(a, b);
return b / g * a;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) {
return a.second < b.second;
} else {
return a.first < b.first;
}
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) {
return a.first < b.first;
} else {
return a.second < b.second;
}
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll RS(ll N, ll P, ll m) {
if (P == 0) {
return 1;
} else {
if (P % 2 == 0) {
ll t = RS(N, P / 2, m);
return t * t % m;
} else {
return N * RS(N, P - 1, m) % m;
}
}
}
bool greater_pair(pair<ll, ll> a, pair<ll, ll> b) {
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second > b.second;
}
}
int main() {
ios::sync_with_stdio(false);
int N = in();
string S;
cin >> S;
int Q = in();
vector<set<int>> v(26);
REP(i, N) { v[S[i] - 'a'].insert(i + 1); }
REP(i, Q) {
int type = in();
if (type == 1) {
int iq;
string cq;
cin >> iq >> cq;
REP(i, 26) {
if (v[i].find(iq) != v[i].end()) {
v[i].erase(iq);
break;
}
}
v[cq[0] - 'a'].insert(iq);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
REP(i, 26) {
auto temp = v[i].lower_bound(l);
if (temp != v[i].end() && *temp <= r) {
ans++;
}
}
cout << ans << endl;
}
}
return 0;
}
| replace | 162 | 164 | 162 | 164 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string t;
cin >> t;
char s[n];
for (int i = 0; i < n; i++)
s[i] = t[i];
int q;
cin >> q;
set<int> m[26];
for (int i = 0; i < n; i++) {
m[s[i] - 'a'].insert(i);
}
for (int _ = 0; _ < q; _++) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
--i;
char prev = s[i];
m[prev - 'a'].erase(i);
m[c - 'a'].insert(i);
s[i] = c;
continue;
}
int l, r;
cin >> l >> r;
--l;
--r;
int cnt = 0;
for (int i = 0; i < 26; i++) {
char c = 'a' + i;
auto it = lower_bound(m[c - 'a'].begin(), m[c - 'a'].end(), l);
if (it != m[c - 'a'].end()) {
if (*it <= r) {
++cnt;
}
}
}
cout << cnt << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string t;
cin >> t;
char s[n];
for (int i = 0; i < n; i++)
s[i] = t[i];
int q;
cin >> q;
set<int> m[26];
for (int i = 0; i < n; i++) {
m[s[i] - 'a'].insert(i);
}
for (int _ = 0; _ < q; _++) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
--i;
char prev = s[i];
m[prev - 'a'].erase(i);
m[c - 'a'].insert(i);
s[i] = c;
continue;
}
int l, r;
cin >> l >> r;
--l;
--r;
int cnt = 0;
for (int i = 0; i < 26; i++) {
char c = 'a' + i;
auto it = m[c - 'a'].lower_bound(l);
if (it != m[c - 'a'].end()) {
if (*it <= r) {
++cnt;
}
}
}
cout << cnt << endl;
}
} | replace | 38 | 39 | 38 | 39 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
inline int tog_bit(int x, int mask) { return (1 << x) ^ mask; }
int n, q;
char s[N + 5];
int ST[N + 5];
void build(int node, int l, int r) {
if (l == r) {
ST[node] = tog_bit(s[l - 1] - 'a', 0);
return;
}
int mid = (l + r) / 2;
build(node * 2, l, mid);
build(node * 2 + 1, mid + 1, r);
ST[node] = ST[node * 2] | ST[node * 2 + 1];
return;
}
void update(int node, int l, int r, int id, char x) {
if (l == r) {
ST[node] = tog_bit(x - 'a', 0);
return;
}
int mid = (l + r) / 2;
if (id <= mid)
update(node * 2, l, mid, id, x);
else
update(node * 2 + 1, mid + 1, r, id, x);
ST[node] = ST[node * 2] | ST[node * 2 + 1];
return;
}
int query(int node, int l, int r, int ql, int qr) {
if (l >= ql && r <= qr)
return ST[node];
if (l > qr || r < ql)
return 0;
int mid = (l + r) / 2;
return query(node * 2, l, mid, ql, qr) |
query(node * 2 + 1, mid + 1, r, ql, qr);
}
int get_cnt(int x) {
int ret = 0;
for (int i = 0; i < 26; i++) {
if ((1 << i) & x)
ret++;
}
return ret;
}
int main() {
scanf("%d", &n);
scanf("%s", s);
build(1, 1, n);
scanf("%d", &q);
while (q--) {
int com;
scanf("%d", &com);
if (com == 1) {
int id;
char ub;
scanf("%d %c", &id, &ub);
update(1, 1, n, id, ub);
} else {
int l, r;
scanf("%d %d", &l, &r);
int ans = query(1, 1, n, l, r);
printf("%d\n", get_cnt(ans));
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
inline int tog_bit(int x, int mask) { return (1 << x) ^ mask; }
int n, q;
char s[N + 5];
int ST[4 * N + 5];
void build(int node, int l, int r) {
if (l == r) {
ST[node] = tog_bit(s[l - 1] - 'a', 0);
return;
}
int mid = (l + r) / 2;
build(node * 2, l, mid);
build(node * 2 + 1, mid + 1, r);
ST[node] = ST[node * 2] | ST[node * 2 + 1];
return;
}
void update(int node, int l, int r, int id, char x) {
if (l == r) {
ST[node] = tog_bit(x - 'a', 0);
return;
}
int mid = (l + r) / 2;
if (id <= mid)
update(node * 2, l, mid, id, x);
else
update(node * 2 + 1, mid + 1, r, id, x);
ST[node] = ST[node * 2] | ST[node * 2 + 1];
return;
}
int query(int node, int l, int r, int ql, int qr) {
if (l >= ql && r <= qr)
return ST[node];
if (l > qr || r < ql)
return 0;
int mid = (l + r) / 2;
return query(node * 2, l, mid, ql, qr) |
query(node * 2 + 1, mid + 1, r, ql, qr);
}
int get_cnt(int x) {
int ret = 0;
for (int i = 0; i < 26; i++) {
if ((1 << i) & x)
ret++;
}
return ret;
}
int main() {
scanf("%d", &n);
scanf("%s", s);
build(1, 1, n);
scanf("%d", &q);
while (q--) {
int com;
scanf("%d", &com);
if (com == 1) {
int id;
char ub;
scanf("%d %c", &id, &ub);
update(1, 1, n, id, ub);
} else {
int l, r;
scanf("%d %d", &l, &r);
int ans = query(1, 1, n, l, r);
printf("%d\n", get_cnt(ans));
}
}
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << '\n'
#define debug_arr(a, n) \
for (int i = 0; i < n; i++) \
cout << a[i] << " "
#define speed \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define inf 1000000000
#define mod 1000000007
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
ll power(ll a, ll b) {
ll prod = 1;
while (b) {
if (b & 1)
prod = (prod * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return prod;
}
bitset<26> t[10009];
string s;
void build(int v, int l, int r) {
if (l == r) {
bitset<26> b(0);
b[s[l] - 97] = 1;
t[v] = b;
} else {
int tm = (l + r) / 2;
build(2 * v, l, tm);
build(2 * v + 1, tm + 1, r);
t[v] = (t[2 * v] | t[2 * v + 1]);
}
}
void update(int v, int tl, int tr, int pos, char c) {
if (tl == pos && tr == pos) {
bitset<26> b(0);
b[c - 97] = 1;
t[v] = b;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(2 * v, tl, tm, pos, c);
else
update(2 * v + 1, tm + 1, tr, pos, c);
t[v] = (t[2 * v] | t[2 * v + 1]);
}
}
bitset<26> query(int v, int tl, int tr, int l, int r) {
if (l > r) {
bitset<26> b(0);
return b;
}
if (tl == l && tr == r) {
return t[v];
}
int tm = (tl + tr) / 2;
return (query(2 * v, tl, tm, l, min(r, tm)) |
query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
int main() {
int n;
cin >> n;
cin >> s;
int q;
cin >> q;
build(1, 0, n - 1);
while (q--) {
int num;
cin >> num;
if (num == 2) {
int l, r;
cin >> l >> r;
l--, r--;
cout << query(1, 0, n - 1, l, r).count() << endl;
} else {
int idx;
char c;
cin >> idx >> c;
update(1, 0, n - 1, idx - 1, c);
}
}
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << '\n'
#define debug_arr(a, n) \
for (int i = 0; i < n; i++) \
cout << a[i] << " "
#define speed \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define inf 1000000000
#define mod 1000000007
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
ll power(ll a, ll b) {
ll prod = 1;
while (b) {
if (b & 1)
prod = (prod * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return prod;
}
bitset<26> t[2000009];
string s;
void build(int v, int l, int r) {
if (l == r) {
bitset<26> b(0);
b[s[l] - 97] = 1;
t[v] = b;
} else {
int tm = (l + r) / 2;
build(2 * v, l, tm);
build(2 * v + 1, tm + 1, r);
t[v] = (t[2 * v] | t[2 * v + 1]);
}
}
void update(int v, int tl, int tr, int pos, char c) {
if (tl == pos && tr == pos) {
bitset<26> b(0);
b[c - 97] = 1;
t[v] = b;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(2 * v, tl, tm, pos, c);
else
update(2 * v + 1, tm + 1, tr, pos, c);
t[v] = (t[2 * v] | t[2 * v + 1]);
}
}
bitset<26> query(int v, int tl, int tr, int l, int r) {
if (l > r) {
bitset<26> b(0);
return b;
}
if (tl == l && tr == r) {
return t[v];
}
int tm = (tl + tr) / 2;
return (query(2 * v, tl, tm, l, min(r, tm)) |
query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
int main() {
int n;
cin >> n;
cin >> s;
int q;
cin >> q;
build(1, 0, n - 1);
while (q--) {
int num;
cin >> num;
if (num == 2) {
int l, r;
cin >> l >> r;
l--, r--;
cout << query(1, 0, n - 1, l, r).count() << endl;
} else {
int idx;
char c;
cin >> idx >> c;
update(1, 0, n - 1, idx - 1, c);
}
}
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02763 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 20, C = 26;
char a[N];
int n, q, type, pos, l, r;
char c;
set<int> s[C];
int main() {
scanf("%d %s%d", &n, a + 1, &q);
for (int i = 1; i <= n; i++)
s[a[i] - 'a'].insert(i);
while (q--) {
scanf("%d", &type);
if (type == 1) {
scanf("%d %c", &pos, &c);
s[a[pos] - 'a'].erase(pos);
s[c - 'a'].insert(pos);
a[pos] = c;
} else {
scanf("%d%d", &l, &r);
int ans = 0;
for (int i = 0; i < C; i++) {
auto it = s[i].lower_bound(l);
if (it == s[i].end() or *it > r)
continue;
ans++;
}
printf("%d\n", ans);
}
}
} | #include "bits/stdc++.h"
using namespace std;
const int N = 5e5 + 20, C = 26;
char a[N];
int n, q, type, pos, l, r;
char c;
set<int> s[C];
int main() {
scanf("%d %s%d", &n, a + 1, &q);
for (int i = 1; i <= n; i++)
s[a[i] - 'a'].insert(i);
while (q--) {
scanf("%d", &type);
if (type == 1) {
scanf("%d %c", &pos, &c);
s[a[pos] - 'a'].erase(pos);
s[c - 'a'].insert(pos);
a[pos] = c;
} else {
scanf("%d%d", &l, &r);
int ans = 0;
for (int i = 0; i < C; i++) {
auto it = s[i].lower_bound(l);
if (it == s[i].end() or *it > r)
continue;
ans++;
}
printf("%d\n", ans);
}
}
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02763 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
int t;
const int MAXN = 500000 + 5;
int n;
struct Node {
int lbit;
};
Node nodes[2 * MAXN - 1];
void init(int n_) {
// 为了简单起见,将元素扩大到2的幂
n = 1;
while (n < n_)
n *= 2;
/* for(int i=0;i<2*n-1;++i){*/
// nodes[i].letters.clear();
/*}*/
}
inline int getbit(char a) { return 1 << (a - 'a'); }
// 将第k个值更新为a
void update(int k, char a) {
// 叶子节点
k += n - 1;
nodes[k].lbit = 0;
nodes[k].lbit |= getbit(a);
// 向上更新
while (k > 0) {
k = (k - 1) / 2;
nodes[k].lbit = 0;
auto &par = nodes[k].lbit;
auto &chl = nodes[k * 2 + 1].lbit;
auto &chr = nodes[k * 2 + 2].lbit;
par = chl | chr;
}
}
// 求[a,b)的最大prefix_max
// 后面的参数是为了计算方便传入的
// k是节点的编号,l,r表示这个节点代表区间[l,r)
// 外部调用时,使用query(a,b,0,0,n);
int query(int a, int b, int k, int l, int r) {
// 如果[a,b)和[l,r)不相交,返回0
if (r <= a || b <= l)
return 0;
// 如果[a,b)完全包含[l,r),返回当前节点值
if (a <= l && r <= b)
return nodes[k].lbit;
else {
auto v1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
auto v2 = query(a, b, k * 2 + 2, (l + r) / 2, r);
return v1 | v2;
}
}
int main() {
ios::sync_with_stdio(false);
#ifdef RG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int nn;
string s;
cin >> nn;
cin >> s;
init(nn);
rep(i, 0, nn) { update(i, s[i]); }
int q;
cin >> q;
int l, r;
char c;
int ty;
rep(i, 0, q) {
cin >> ty;
if (ty == 1) {
cin >> l >> c;
update(l - 1, c);
} else {
cin >> l >> r;
auto ans = query(l - 1, r, 0, 0, n);
int aaa = 0;
for (int i = 0; i < 30; ++i) {
if (ans & (1 << i))
aaa++;
}
cout << aaa << endl;
}
}
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
int t;
const int MAXN = 1000000 + 5;
int n;
struct Node {
int lbit;
};
Node nodes[2 * MAXN - 1];
void init(int n_) {
// 为了简单起见,将元素扩大到2的幂
n = 1;
while (n < n_)
n *= 2;
/* for(int i=0;i<2*n-1;++i){*/
// nodes[i].letters.clear();
/*}*/
}
inline int getbit(char a) { return 1 << (a - 'a'); }
// 将第k个值更新为a
void update(int k, char a) {
// 叶子节点
k += n - 1;
nodes[k].lbit = 0;
nodes[k].lbit |= getbit(a);
// 向上更新
while (k > 0) {
k = (k - 1) / 2;
nodes[k].lbit = 0;
auto &par = nodes[k].lbit;
auto &chl = nodes[k * 2 + 1].lbit;
auto &chr = nodes[k * 2 + 2].lbit;
par = chl | chr;
}
}
// 求[a,b)的最大prefix_max
// 后面的参数是为了计算方便传入的
// k是节点的编号,l,r表示这个节点代表区间[l,r)
// 外部调用时,使用query(a,b,0,0,n);
int query(int a, int b, int k, int l, int r) {
// 如果[a,b)和[l,r)不相交,返回0
if (r <= a || b <= l)
return 0;
// 如果[a,b)完全包含[l,r),返回当前节点值
if (a <= l && r <= b)
return nodes[k].lbit;
else {
auto v1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
auto v2 = query(a, b, k * 2 + 2, (l + r) / 2, r);
return v1 | v2;
}
}
int main() {
ios::sync_with_stdio(false);
#ifdef RG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int nn;
string s;
cin >> nn;
cin >> s;
init(nn);
rep(i, 0, nn) { update(i, s[i]); }
int q;
cin >> q;
int l, r;
char c;
int ty;
rep(i, 0, q) {
cin >> ty;
if (ty == 1) {
cin >> l >> c;
update(l - 1, c);
} else {
cin >> l >> r;
auto ans = query(l - 1, r, 0, 0, n);
int aaa = 0;
for (int i = 0; i < 30; ++i) {
if (ans & (1 << i))
aaa++;
}
cout << aaa << endl;
}
}
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p02763 | C++ | Runtime Error | // In the name of GOD
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " :: " << x << "\n";
#define debug2(x, y) \
cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z) \
cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z \
<< " :: " << z << "\n";
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define setprecn(x) cout << fixed << setprecision(x);
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#define vll vector<ll>
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define mod 1000000007
#define inf 10000000000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repe(i, a, b) for (ll i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
ll power(ll a, ll b, ll m = mod) {
if (b == 0)
return 1;
if (b == 1)
return a;
ll c = power(a, b / 2, m);
if (b % 2 == 0)
return ((c % m) * (c % m)) % m;
return ((((c % m) * (c % m)) % m) * (a % m)) % m;
}
/********************************************************/
// d.erase(unique(all(d)), d.end());
int main() {
IOS;
ll n, k, m, i, j, c = 0, cs = 0, t;
t = 1;
string s;
cin >> s >> t;
n = s.length();
set<ll> x[26];
rep(i, n) { x[s[i] - 'a'].insert(i + 1); }
while (t--) {
char ch;
cin >> n;
// cout<<(x[0].lower_bound(7)==x[0].end());
if (n == 1) {
ll pos;
cin >> pos >> ch;
// pos--;
rep(i, 26) {
auto y = x[i].lower_bound(pos);
if (y != x[i].end() && (*y) == pos) {
x[i].erase(y);
break;
}
}
x[ch - 'a'].insert(pos);
} else {
c = 0;
cin >> j >> k;
rep(i, 26) {
auto y = x[i].lower_bound(j);
if (y != x[i].end() && (*y) <= k)
c++;
}
cout << c;
cout << "\n";
}
}
return 0;
} | // In the name of GOD
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " :: " << x << "\n";
#define debug2(x, y) \
cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z) \
cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z \
<< " :: " << z << "\n";
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define setprecn(x) cout << fixed << setprecision(x);
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#define vll vector<ll>
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define mod 1000000007
#define inf 10000000000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repe(i, a, b) for (ll i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
ll power(ll a, ll b, ll m = mod) {
if (b == 0)
return 1;
if (b == 1)
return a;
ll c = power(a, b / 2, m);
if (b % 2 == 0)
return ((c % m) * (c % m)) % m;
return ((((c % m) * (c % m)) % m) * (a % m)) % m;
}
/********************************************************/
// d.erase(unique(all(d)), d.end());
int main() {
IOS;
ll n, k, m, i, j, c = 0, cs = 0, t;
t = 1;
string s;
cin >> n >> s >> t;
n = s.length();
set<ll> x[26];
rep(i, n) { x[s[i] - 'a'].insert(i + 1); }
while (t--) {
char ch;
cin >> n;
// cout<<(x[0].lower_bound(7)==x[0].end());
if (n == 1) {
ll pos;
cin >> pos >> ch;
// pos--;
rep(i, 26) {
auto y = x[i].lower_bound(pos);
if (y != x[i].end() && (*y) == pos) {
x[i].erase(y);
break;
}
}
x[ch - 'a'].insert(pos);
} else {
c = 0;
cin >> j >> k;
rep(i, 26) {
auto y = x[i].lower_bound(j);
if (y != x[i].end() && (*y) <= k)
c++;
}
cout << c;
cout << "\n";
}
}
return 0;
} | replace | 52 | 53 | 52 | 53 | -11 | |
p02763 | C++ | Runtime Error | #include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
int N, Q, qi;
string S;
char c;
cin >> N;
cin >> S;
cin >> Q;
vector<set<int>> is(26);
for (int i = 0; i < N; i++) {
is[S[i] - 'a'].insert(i); // Sのi番目の文字の欄に何番目に出たかを記録
}
int l, r;
for (int i = 0; i < Q; i++) {
cin >> qi;
if (qi == 1) {
int ind;
char c;
cin >> ind >> c;
ind--;
is[S[ind] - 'a'].erase(ind); // S[i]がind番目に出るという情報を削除
S[i] = c; // Sの情報を書き換え
is[S[ind] - 'a'].insert(
ind); // c(=S[i]-'a')がind番目に出るという情報を追加
} else {
cin >> l >> r;
int ans = 0;
l--;
for (int i = 0; i < 26; i++) {
// i番目の文字がl何番目以降(i番目含む)に初めて出る場所を調べる
auto itr = is[i].lower_bound(l);
// それが指定範囲内にあれば
if (itr != is[i].end() && *itr < r) {
ans++;
}
}
cout << ans << endl;
}
}
return 0;
} | #include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
int N, Q, qi;
string S;
char c;
cin >> N;
cin >> S;
cin >> Q;
vector<set<int>> is(26);
for (int i = 0; i < N; i++) {
is[S[i] - 'a'].insert(i); // Sのi番目の文字の欄に何番目に出たかを記録
}
int l, r;
for (int i = 0; i < Q; i++) {
cin >> qi;
if (qi == 1) {
int ind;
char c;
cin >> ind >> c;
ind--;
is[S[ind] - 'a'].erase(ind); // S[i]がind番目に出るという情報を削除
S[ind] = c; // Sの情報を書き換え
is[S[ind] - 'a'].insert(
ind); // c(=S[i]-'a')がind番目に出るという情報を追加
} else {
cin >> l >> r;
int ans = 0;
l--;
for (int i = 0; i < 26; i++) {
// i番目の文字がl何番目以降(i番目含む)に初めて出る場所を調べる
auto itr = is[i].lower_bound(l);
// それが指定範囲内にあれば
if (itr != is[i].end() && *itr < r) {
ans++;
}
}
cout << ans << endl;
}
}
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02763 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v) \
sort(v.begin(), v.end()); \
v.erase(unique(v.begin(), v.end()), v.end())
#define int ll
using namespace std;
const ll INF = (1 << 30) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
struct Timer {
chrono::system_clock::time_point start, end;
Timer() { start = chrono::system_clock::now(); }
~Timer() {
end = chrono::system_clock::now();
auto msec =
chrono::duration_cast<chrono::milliseconds>(end - start).count();
cerr << "time : " << msec << " ms" << endl;
}
};
struct SegmentTree {
static const int MAX_N = 1 << 18;
int n, dat[2 * MAX_N - 1];
SegmentTree(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = 0;
}
void update(int k, int a) {
k += n - 1;
dat[k] = (1 << a);
while (k > 0) {
k = (k - 1) / 2;
// dat[k]=min(dat[k*2+1],dat[k*2+2]);
dat[k] = (dat[k * 2 + 1] | dat[k * 2 + 2]);
}
}
void print() {
rep(i, n * 2) {
// if(i==n-1) cout<<"//";
// cout<<dat[i]<<" ";
}
// cout<<endl;
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b) {
return dat[k];
} else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return (vr | vl);
}
}
int query(int a, int b) { return query(a, b, 0, 0, n); }
};
void solve() {
ll n, q;
cin >> n;
string s;
cin >> s >> q;
SegmentTree sg(n);
rep(i, n) { sg.update(i, s[i] - 'a'); }
// sg.print();
// cout<<"------"<<endl;
rep(i, q) {
// sg.print();
ll c;
cin >> c;
if (c == 1) {
ll a;
char b;
cin >> a >> b;
sg.update(a - 1, b - 'a');
} else {
ll a, b;
cin >> a >> b;
ll t = sg.query(a - 1, b);
ll sum = 0;
rep(j, 26) {
if (t & (1 << j))
sum++;
}
cout << sum << endl;
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// Timer TM;
solve();
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v) \
sort(v.begin(), v.end()); \
v.erase(unique(v.begin(), v.end()), v.end())
#define int ll
using namespace std;
const ll INF = (1 << 30) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
struct Timer {
chrono::system_clock::time_point start, end;
Timer() { start = chrono::system_clock::now(); }
~Timer() {
end = chrono::system_clock::now();
auto msec =
chrono::duration_cast<chrono::milliseconds>(end - start).count();
cerr << "time : " << msec << " ms" << endl;
}
};
struct SegmentTree {
static const int MAX_N = 1 << 19;
int n, dat[2 * MAX_N - 1];
SegmentTree(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = 0;
}
void update(int k, int a) {
k += n - 1;
dat[k] = (1 << a);
while (k > 0) {
k = (k - 1) / 2;
// dat[k]=min(dat[k*2+1],dat[k*2+2]);
dat[k] = (dat[k * 2 + 1] | dat[k * 2 + 2]);
}
}
void print() {
rep(i, n * 2) {
// if(i==n-1) cout<<"//";
// cout<<dat[i]<<" ";
}
// cout<<endl;
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b) {
return dat[k];
} else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return (vr | vl);
}
}
int query(int a, int b) { return query(a, b, 0, 0, n); }
};
void solve() {
ll n, q;
cin >> n;
string s;
cin >> s >> q;
SegmentTree sg(n);
rep(i, n) { sg.update(i, s[i] - 'a'); }
// sg.print();
// cout<<"------"<<endl;
rep(i, q) {
// sg.print();
ll c;
cin >> c;
if (c == 1) {
ll a;
char b;
cin >> a >> b;
sg.update(a - 1, b - 'a');
} else {
ll a, b;
cin >> a >> b;
ll t = sg.query(a - 1, b);
ll sum = 0;
rep(j, 26) {
if (t & (1 << j))
sum++;
}
cout << sum << endl;
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// Timer TM;
solve();
return 0;
}
| replace | 67 | 68 | 67 | 68 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> count(26);
rep(i, n) { count[s[i] - 'a'].insert(i); }
rep(qi, q) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
i--;
count[s[i] - 'a'].erase(i);
s[i] = c;
count[c - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
rep(i, 26) {
auto it = lower_bound(count[i].begin(), count[i].end(), l);
if (it != count[i].end() && *it <= r)
ans++;
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> count(26);
rep(i, n) { count[s[i] - 'a'].insert(i); }
rep(qi, q) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
i--;
count[s[i] - 'a'].erase(i);
s[i] = c;
count[c - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
rep(i, 26) {
auto it = count[i].lower_bound(l);
if (it != count[i].end() && *it <= r)
ans++;
}
cout << ans << endl;
}
}
} | replace | 34 | 35 | 34 | 35 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i <= (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define Eunique(v) v.erase(unique(all(v)), v.end())
#define Eback(s) s.erase(s.end() - 1, s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v), 0LL)
#define mapmin(v) v.rbegin()->first
#define mapmax(v) v.begin()->first
#define pb push_back
#define pf push_front
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK cerr << "OK\n"
#define OK1 cerr << "OK1\n"
#define OK2 cerr << "OK2\n"
#define SIZE(s) (int)s.size()
#define INF ((1LL << 62) - (1LL << 31))
#define zero(x, n) setw(x) << setfill('0') << n
#define endl '\n'
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
const double pi = acos(-1.0);
const ll mod = 1000000007;
const ll mod2 = 998244353;
template <class A, class B>
ostream &operator<<(ostream &ost, const pair<A, B> &p) {
ost << "{" << p.first << ", " << p.second << "} ";
return ost;
}
template <class T> ostream &operator<<(ostream &ost, const vector<T> &v) {
ost << "{";
for (int i = 0; i < (int)v.size(); i++) {
if (i)
ost << " ";
ost << v[i];
}
ost << "} \n";
return ost;
}
template <class A, class B>
ostream &operator<<(ostream &ost, const map<A, B> &v) {
ost << "{";
for (auto p : v) {
ost << "{" << p.first << ", " << p.second << "} ";
}
ost << "} ";
return ost;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
void YES(bool b) { cout << ((b) ? "YES\n" : "NO\n"); }
void Yes(bool b) { cout << ((b) ? "Yes\n" : "No\n"); }
void yes(bool b) { cout << ((b) ? "yes\n" : "no\n"); }
void Yay(bool b) { cout << ((b) ? "Yay!\n" : ":(\n"); }
void out() { cout << "\n"; }
template <class T, class... Args> void out(const T &x, const Args &...args) {
cout << x << " ";
out(args...);
}
ll powmod(ll a, ll b) {
ll c = 1;
while (b > 0) {
if (b & 1) {
c = a * c % mod;
}
a = a * a % mod;
b >>= 1;
}
return c;
}
ll gcd(ll x, ll y) { return __gcd(x, y); }
ll lcm(ll x, ll y) { return x / __gcd(x, y) * y; }
int main() {
ll n, q, l, r, p, ans, t;
char c;
string s;
cin >> n >> s >> q;
vector<set<ll>> is(26);
rep(i, n) is[s[i] - 'a'].insert(i);
rep(i, q) {
cin >> t;
if (t == 1) {
cin >> p >> c;
p--;
is[s[p] - 'a'].erase(p);
s[p] = c;
is[s[p] - 'a'].insert(p);
} else {
ans = 0;
cin >> l >> r;
l--;
r--;
rep(j, 26) {
auto it = lower_bound(all(is[j]), l);
if (it != is[j].end() && *it <= r)
ans++;
}
printf("%d\n", ans);
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i <= (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define Eunique(v) v.erase(unique(all(v)), v.end())
#define Eback(s) s.erase(s.end() - 1, s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v), 0LL)
#define mapmin(v) v.rbegin()->first
#define mapmax(v) v.begin()->first
#define pb push_back
#define pf push_front
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK cerr << "OK\n"
#define OK1 cerr << "OK1\n"
#define OK2 cerr << "OK2\n"
#define SIZE(s) (int)s.size()
#define INF ((1LL << 62) - (1LL << 31))
#define zero(x, n) setw(x) << setfill('0') << n
#define endl '\n'
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
const double pi = acos(-1.0);
const ll mod = 1000000007;
const ll mod2 = 998244353;
template <class A, class B>
ostream &operator<<(ostream &ost, const pair<A, B> &p) {
ost << "{" << p.first << ", " << p.second << "} ";
return ost;
}
template <class T> ostream &operator<<(ostream &ost, const vector<T> &v) {
ost << "{";
for (int i = 0; i < (int)v.size(); i++) {
if (i)
ost << " ";
ost << v[i];
}
ost << "} \n";
return ost;
}
template <class A, class B>
ostream &operator<<(ostream &ost, const map<A, B> &v) {
ost << "{";
for (auto p : v) {
ost << "{" << p.first << ", " << p.second << "} ";
}
ost << "} ";
return ost;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
void YES(bool b) { cout << ((b) ? "YES\n" : "NO\n"); }
void Yes(bool b) { cout << ((b) ? "Yes\n" : "No\n"); }
void yes(bool b) { cout << ((b) ? "yes\n" : "no\n"); }
void Yay(bool b) { cout << ((b) ? "Yay!\n" : ":(\n"); }
void out() { cout << "\n"; }
template <class T, class... Args> void out(const T &x, const Args &...args) {
cout << x << " ";
out(args...);
}
ll powmod(ll a, ll b) {
ll c = 1;
while (b > 0) {
if (b & 1) {
c = a * c % mod;
}
a = a * a % mod;
b >>= 1;
}
return c;
}
ll gcd(ll x, ll y) { return __gcd(x, y); }
ll lcm(ll x, ll y) { return x / __gcd(x, y) * y; }
int main() {
ll n, q, l, r, p, ans, t;
char c;
string s;
cin >> n >> s >> q;
vector<set<ll>> is(26);
rep(i, n) is[s[i] - 'a'].insert(i);
rep(i, q) {
cin >> t;
if (t == 1) {
cin >> p >> c;
p--;
is[s[p] - 'a'].erase(p);
s[p] = c;
is[s[p] - 'a'].insert(p);
} else {
ans = 0;
cin >> l >> r;
l--;
r--;
rep(j, 26) {
auto it = is[j].lower_bound(l);
if (it != is[j].end() && *it <= r)
ans++;
}
printf("%d\n", ans);
}
}
return 0;
} | replace | 141 | 142 | 141 | 142 | TLE | |
p02763 | C++ | Runtime Error | #define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << endl
#define debug_4(x1, x2, x3, x4) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...) \
CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index, num) for (int index = 0; index < (int)num; index++)
#define rep1(index, num) for (int index = 1; index <= (int)num; index++)
#define brep(index, num) for (int index = (int)num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = (int)num; index > 0; index--)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = (ll)1e+18 + 1;
int INF = 1e+9 + 1;
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
// 内部は1-indexedだけどmainでは0-indxedで動く
int sz; // size 葉がsz個、葉以外がsz-1個
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); // 子を用いて親を更新
}
}
void add(int k, const Monoid &x) {
k += sz;
seg[k] += x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) { // 半開区間
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
int N, Q;
string S;
scan(N >> S >> Q);
vector<SegmentTree<int>> segv;
rep(i, 26) {
SegmentTree<int> seg(
N, [](int a, int b) { return a + b; }, 0);
segv.push_back(seg);
}
rep(i, N) { segv[S[i] - 'a'].add(i, 1); }
rep(i, Q) {
int q;
scan(q);
if (q == 1) {
int k;
char c;
scan(k >> c);
k--;
segv[S[k] - 'a'].add(k, -1);
segv[c - 'a'].add(k, 1);
S[k] = 'a' + c;
} else {
int ans = 0;
int l, r;
scan(l >> r);
l--;
rep(j, 26) {
if (segv[j].query(l, r) > 0)
ans++;
}
prin(ans);
}
/*rep(j,26){
printf("%c ",'a'+j);
rep(i,N){
printf("%d",segv[j].query(i,i+1));
}kaigyo;
}*/
}
return 0;
}
| #define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << endl
#define debug_4(x1, x2, x3, x4) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5) \
cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...) \
CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index, num) for (int index = 0; index < (int)num; index++)
#define rep1(index, num) for (int index = 1; index <= (int)num; index++)
#define brep(index, num) for (int index = (int)num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = (int)num; index > 0; index--)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = (ll)1e+18 + 1;
int INF = 1e+9 + 1;
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
// 内部は1-indexedだけどmainでは0-indxedで動く
int sz; // size 葉がsz個、葉以外がsz-1個
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); // 子を用いて親を更新
}
}
void add(int k, const Monoid &x) {
k += sz;
seg[k] += x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) { // 半開区間
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
int N, Q;
string S;
scan(N >> S >> Q);
vector<SegmentTree<int>> segv;
rep(i, 26) {
SegmentTree<int> seg(
N, [](int a, int b) { return a + b; }, 0);
segv.push_back(seg);
}
rep(i, N) { segv[S[i] - 'a'].add(i, 1); }
rep(i, Q) {
int q;
scan(q);
if (q == 1) {
int k;
char c;
scan(k >> c);
k--;
segv[S[k] - 'a'].add(k, -1);
segv[c - 'a'].add(k, 1);
S[k] = c;
} else {
int ans = 0;
int l, r;
scan(l >> r);
l--;
rep(j, 26) {
if (segv[j].query(l, r) > 0)
ans++;
}
prin(ans);
}
/*rep(j,26){
printf("%c ",'a'+j);
rep(i,N){
printf("%d",segv[j].query(i,i+1));
}kaigyo;
}*/
}
return 0;
}
| replace | 119 | 120 | 119 | 120 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++
int main() {
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
vector<set<int>> sets(26);
rep(i, n) { sets[s[i] - 'a'].insert(i); }
rep(_, q) {
int a;
cin >> a;
if (a == 1) {
int i;
char c;
cin >> i >> c;
--i;
sets[s[i] - 'a'].erase(i);
s[i] = c;
sets[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
--l;
--r;
int num = 0;
rep(i, 26) {
set<int> si = sets[i];
auto it = si.lower_bound(l);
if (it != si.end() && *it <= r)
++num;
}
cout << num << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++
int main() {
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
vector<set<int>> sets(26);
rep(i, n) { sets[s[i] - 'a'].insert(i); }
rep(_, q) {
int a;
cin >> a;
if (a == 1) {
int i;
char c;
cin >> i >> c;
--i;
sets[s[i] - 'a'].erase(i);
s[i] = c;
sets[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
--l;
--r;
int num = 0;
rep(i, 26) {
auto it = sets[i].lower_bound(l);
if (it != sets[i].end() && *it <= r)
++num;
}
cout << num << endl;
}
}
return 0;
} | replace | 47 | 50 | 47 | 49 | TLE | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define RREP(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define INVREP(i, a, b) for (int i = a; i >= b; --i)
#define MOD 1000000007ll
#define BIT(i) (1ll << i)
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
} // aに更新したいものを
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// 繰り返し2乗
ll modpow(ll N, ll P, ll M = MOD) {
if (P == 0)
return 1;
if (P % 2 == 0) {
ll t = modpow(N, P / 2, M);
return t * t % M;
}
return (N * modpow(N, P - 1, M)) % M;
}
// 階乗
pair<vector<ll>, vector<ll>> fact(ll n) {
vector<ll> f(n, 1);
RREP(i, 2, n) f[i] = (f[i - 1] * i) % MOD;
vector<ll> invf(n, 1);
invf[n - 1] = modpow(f[n - 1], MOD - 2, MOD);
INVREP(i, n - 1, 2) invf[i - 1] = (invf[i] * i) % MOD;
return pair<vector<ll>, vector<ll>>(f, invf);
}
ll modmult(ll a, ll b, ll M = MOD) { return (a * b) % M; }
ll modadd(ll a, ll b, ll M = MOD) { return (a + b) % M; }
ll comb(pair<vector<ll>, vector<ll>> p, int n, int r) {
return modmult(modmult(p.first[n], p.second[r]), p.second[n - r]);
}
/*
auto a = fact(200009);
cout << comb(a,15,5) << endl;
*/
void solve() {
/* collection
ll n;
cin >> n;
vector<ll> A;
REP(i, n) cin >> A[i];
vector<pair<ll, ll>> AB;
REP(i, n) {cin >> AB[i].first;cin >> AB[i].second;}
sort(ALL(vec),greater<int>());
map<ll, ll> mp;
map<ll, ll, greater<ll>> mp;
REP(i, 100) mp[i] = i * 10;
cout << mp.lower_bound(50)->second<<endl;
cout << mp.upper_bound(50)->second << endl;
cout << next(mp.upper_bound(50))->second << endl;
cout << prev(mp.upper_bound(50))->second << endl;
*/
int n, q;
string s;
cin >> n >> s >> q;
int a = (int)'a';
set<int> chars[26];
REP(i, 26) { chars[i].insert(10000000); }
REP(i, n) { chars[(int)s[i] - a].insert(i + 1); }
REP(time, q) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
chars[int(s[i - 1]) - a].erase(i);
s[i - 1] = c;
chars[(int)c - a].insert(i);
} else {
int l, r, ans = 0;
cin >> l >> r;
REP(i, n) {
if (*(chars[i].lower_bound(l)) <= r)
ans++;
}
cout << ans << '\n';
}
}
}
int main() { solve(); }
| #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define RREP(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define INVREP(i, a, b) for (int i = a; i >= b; --i)
#define MOD 1000000007ll
#define BIT(i) (1ll << i)
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
} // aに更新したいものを
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// 繰り返し2乗
ll modpow(ll N, ll P, ll M = MOD) {
if (P == 0)
return 1;
if (P % 2 == 0) {
ll t = modpow(N, P / 2, M);
return t * t % M;
}
return (N * modpow(N, P - 1, M)) % M;
}
// 階乗
pair<vector<ll>, vector<ll>> fact(ll n) {
vector<ll> f(n, 1);
RREP(i, 2, n) f[i] = (f[i - 1] * i) % MOD;
vector<ll> invf(n, 1);
invf[n - 1] = modpow(f[n - 1], MOD - 2, MOD);
INVREP(i, n - 1, 2) invf[i - 1] = (invf[i] * i) % MOD;
return pair<vector<ll>, vector<ll>>(f, invf);
}
ll modmult(ll a, ll b, ll M = MOD) { return (a * b) % M; }
ll modadd(ll a, ll b, ll M = MOD) { return (a + b) % M; }
ll comb(pair<vector<ll>, vector<ll>> p, int n, int r) {
return modmult(modmult(p.first[n], p.second[r]), p.second[n - r]);
}
/*
auto a = fact(200009);
cout << comb(a,15,5) << endl;
*/
void solve() {
/* collection
ll n;
cin >> n;
vector<ll> A;
REP(i, n) cin >> A[i];
vector<pair<ll, ll>> AB;
REP(i, n) {cin >> AB[i].first;cin >> AB[i].second;}
sort(ALL(vec),greater<int>());
map<ll, ll> mp;
map<ll, ll, greater<ll>> mp;
REP(i, 100) mp[i] = i * 10;
cout << mp.lower_bound(50)->second<<endl;
cout << mp.upper_bound(50)->second << endl;
cout << next(mp.upper_bound(50))->second << endl;
cout << prev(mp.upper_bound(50))->second << endl;
*/
int n, q;
string s;
cin >> n >> s >> q;
int a = (int)'a';
set<int> chars[26];
REP(i, 26) { chars[i].insert(10000000); }
REP(i, n) { chars[(int)s[i] - a].insert(i + 1); }
REP(time, q) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
chars[int(s[i - 1]) - a].erase(i);
s[i - 1] = c;
chars[(int)c - a].insert(i);
} else {
int l, r, ans = 0;
cin >> l >> r;
REP(i, 26) {
if (*(chars[i].lower_bound(l)) <= r)
ans++;
}
cout << ans << '\n';
}
}
}
int main() { solve(); }
| replace | 108 | 109 | 108 | 109 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
int cnt = 0;
for (const auto &elem : a) {
if (cnt++)
cout << ' ';
cout << elem;
}
cout << endl;
}
// 解説参考.
int main() {
int n;
string s;
int q;
cin >> n >> s >> q;
set<int> st[26]; // aからzそれぞれの位置を記録.
REP(i, n) st[s[i] - 'a'].insert(i);
REP(i, 26) st[i].insert(n); // 番兵.
REP(i, q) {
int t;
cin >> t;
if (t == 1) {
int j;
char c;
cin >> j >> c;
j--;
REP(k, 26) {
if (st[k].find(j) == st[k].end()) {
} else
st[k].erase(j);
}
st[c - 'a'].insert(j);
} else {
int l, r;
cin >> l >> r;
l--, r--;
int ans = 0;
REP(j, 26) {
int p = *lower_bound(ALL(st[j]), l); // l以上の値を習得.
if (p <= r)
ans++; // 上記の値がr以下だったらok.
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
int cnt = 0;
for (const auto &elem : a) {
if (cnt++)
cout << ' ';
cout << elem;
}
cout << endl;
}
// 解説参考.
int main() {
int n;
string s;
int q;
cin >> n >> s >> q;
set<int> st[26]; // aからzそれぞれの位置を記録.
REP(i, n) st[s[i] - 'a'].insert(i);
REP(i, 26) st[i].insert(n); // 番兵.
REP(i, q) {
int t;
cin >> t;
if (t == 1) {
int j;
char c;
cin >> j >> c;
j--;
REP(k, 26) {
if (st[k].find(j) == st[k].end()) {
} else
st[k].erase(j);
}
st[c - 'a'].insert(j);
} else {
int l, r;
cin >> l >> r;
l--, r--;
int ans = 0;
REP(j, 26) {
int p = *st[j].lower_bound(l); // l以上の値を習得.
if (p <= r)
ans++; // 上記の値がr以下だったらok.
}
cout << ans << endl;
}
}
} | replace | 57 | 58 | 57 | 58 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll modulo = 1000000007ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string str;
cin >> str;
int q;
cin >> q;
vector<set<int>> vec(26, set<int>());
for (int i = 0; i < n; ++i) {
vec[str[i] - 'a'].insert(i);
}
for (int i = 0; i < q; ++i) {
int type;
cin >> type;
if (type == 1) {
char c;
int j;
cin >> j >> c;
int k = 0;
for (k = 0; k < 26; ++k) {
if (vec[k].find(j - 1) != vec[k].end())
break;
}
vec[k].erase(j - 1);
vec[c - 'a'].insert(j - 1);
} else {
int l, r;
cin >> l >> r;
int sum = 0;
for (int k = 0; k < 26; ++k) {
auto it1 = lower_bound(vec[k].begin(), vec[k].end(), l - 1);
int index = *it1;
if (it1 != vec[k].end())
if (index <= (r - 1) && index >= (l - 1))
sum++;
}
cout << sum << "\n";
}
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll modulo = 1000000007ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string str;
cin >> str;
int q;
cin >> q;
vector<set<int>> vec(26, set<int>());
for (int i = 0; i < n; ++i) {
vec[str[i] - 'a'].insert(i);
}
for (int i = 0; i < q; ++i) {
int type;
cin >> type;
if (type == 1) {
char c;
int j;
cin >> j >> c;
int k = 0;
for (k = 0; k < 26; ++k) {
if (vec[k].find(j - 1) != vec[k].end())
break;
}
vec[k].erase(j - 1);
vec[c - 'a'].insert(j - 1);
} else {
int l, r;
cin >> l >> r;
int sum = 0;
for (int k = 0; k < 26; ++k) {
auto it1 = vec[k].lower_bound(l - 1);
int index = *it1;
if (it1 != vec[k].end())
if (index <= (r - 1) && index >= (l - 1))
sum++;
}
cout << sum << "\n";
}
}
}
| replace | 40 | 41 | 40 | 41 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
int n;
string s;
int seg[4 * MAXN + 20];
int q;
void build(int pos, int l, int r) {
if (l == r) {
seg[pos] = 1 << (s[l] - 'a');
} else {
build(pos << 1, l, (l + r) >> 1);
build((pos << 1) + 1, ((l + r) >> 1) + 1, r);
seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
}
}
void upd(int pos, int l, int r, int tar) {
if (l == r) {
seg[pos] = 1 << (s[l] - 'a');
} else {
if (tar <= ((l + r) >> 1)) {
upd(pos << 1, l, ((l + r) >> 1), tar);
} else {
upd((pos << 1) + 1, ((l + r) >> 1) + 1, r, tar);
}
seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
}
}
int que(int pos, int l, int r, int ql, int qr) {
if (ql > qr)
return 0;
if (l == ql && r == qr) {
return seg[pos];
} else {
int m = (l + r) >> 1;
int a = que(pos << 1, l, m, ql, min(m, qr));
int b = que((pos << 1) + 1, m + 1, r, max(ql, m + 1), qr);
return a | b;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cin >> s;
cin >> q;
build(1, 0, n - 1);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
s[i - 1] = c;
upd(1, 0, n - 1, i - 1);
} else if (type == 2) {
int l, r;
cin >> l >> r;
cout << __builtin_popcount(que(1, 0, n - 1, l - 1, r - 1)) << "\n";
}
}
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5;
int n;
string s;
int seg[4 * MAXN + 20];
int q;
void build(int pos, int l, int r) {
if (l == r) {
seg[pos] = 1 << (s[l] - 'a');
} else {
build(pos << 1, l, (l + r) >> 1);
build((pos << 1) + 1, ((l + r) >> 1) + 1, r);
seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
}
}
void upd(int pos, int l, int r, int tar) {
if (l == r) {
seg[pos] = 1 << (s[l] - 'a');
} else {
if (tar <= ((l + r) >> 1)) {
upd(pos << 1, l, ((l + r) >> 1), tar);
} else {
upd((pos << 1) + 1, ((l + r) >> 1) + 1, r, tar);
}
seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
}
}
int que(int pos, int l, int r, int ql, int qr) {
if (ql > qr)
return 0;
if (l == ql && r == qr) {
return seg[pos];
} else {
int m = (l + r) >> 1;
int a = que(pos << 1, l, m, ql, min(m, qr));
int b = que((pos << 1) + 1, m + 1, r, max(ql, m + 1), qr);
return a | b;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cin >> s;
cin >> q;
build(1, 0, n - 1);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int i;
char c;
cin >> i >> c;
s[i - 1] = c;
upd(1, 0, n - 1, i - 1);
} else if (type == 2) {
int l, r;
cin >> l >> r;
cout << __builtin_popcount(que(1, 0, n - 1, l - 1, r - 1)) << "\n";
}
}
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX
struct segment_tree {
int seguki[530000], p = 524286;
void update(int x, int diff) {
x += p;
seguki[x] += diff;
while (x) {
x = (x - 1) / 2;
seguki[x] += diff;
}
}
int count(int l, int r) {
int diff = r - l + 1, now = l + p, size = 1;
while (true) {
if (now % 2 && size * 2 <= diff) {
now = (now - 1) / 2;
size *= 2;
} else {
int ans = seguki[now];
l += size;
if (l == r + 1)
return ans;
else
return ans + count(l, r);
}
}
}
};
int n, q;
string s;
segment_tree st[26];
signed main() {
cin >> n >> s >> q;
rep(i, n) {
s[i] -= 'a';
st[s[i]].update(i, 1);
}
rep(i, q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
--b;
c -= 'a';
st[s[b]].update(b, -1);
st[c].update(b, 1);
s[b] = c;
} else {
int l, r, cnt = 0;
cin >> l >> r;
--l, --r;
rep(j, 26) if (st[j].count(l, r) != 0) cnt++;
cout << cnt << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX
struct segment_tree {
int seguki[1048572], p = 524286;
void update(int x, int diff) {
x += p;
seguki[x] += diff;
while (x) {
x = (x - 1) / 2;
seguki[x] += diff;
}
}
int count(int l, int r) {
int diff = r - l + 1, now = l + p, size = 1;
while (true) {
if (now % 2 && size * 2 <= diff) {
now = (now - 1) / 2;
size *= 2;
} else {
int ans = seguki[now];
l += size;
if (l == r + 1)
return ans;
else
return ans + count(l, r);
}
}
}
};
int n, q;
string s;
segment_tree st[26];
signed main() {
cin >> n >> s >> q;
rep(i, n) {
s[i] -= 'a';
st[s[i]].update(i, 1);
}
rep(i, q) {
int a;
cin >> a;
if (a == 1) {
int b;
char c;
cin >> b >> c;
--b;
c -= 'a';
st[s[b]].update(b, -1);
st[c].update(b, 1);
s[b] = c;
} else {
int l, r, cnt = 0;
cin >> l >> r;
--l, --r;
rep(j, 26) if (st[j].count(l, r) != 0) cnt++;
cout << cnt << endl;
}
}
}
| replace | 19 | 20 | 19 | 20 | -11 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define mt make_tuple
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sqr(x) ((ll)(x) * (x))
int main() {
string s;
cin >> s;
vector<set<int>> v(26);
for (int i = 0; i < (int)s.size(); ++i) {
v[s[i] - 'a'].insert(i);
}
int q;
cin >> q;
while (q--) {
int instrukcija;
cin >> instrukcija;
if (instrukcija == 1) {
int pos;
char c;
cin >> pos >> c;
--pos;
v[s[pos] - 'a'].erase(pos);
s[pos] = c;
v[c - 'a'].insert(pos);
} else {
int l, r;
cin >> l >> r;
--l, --r;
int odgovor = 0;
for (int i = 0; i < 26; ++i) {
auto it = v[i].lower_bound(l);
if (it != v[i].end() && *it <= r) {
++odgovor;
}
}
cout << odgovor << '\n';
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define mt make_tuple
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sqr(x) ((ll)(x) * (x))
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<set<int>> v(26);
for (int i = 0; i < (int)s.size(); ++i) {
v[s[i] - 'a'].insert(i);
}
int q;
cin >> q;
while (q--) {
int instrukcija;
cin >> instrukcija;
if (instrukcija == 1) {
int pos;
char c;
cin >> pos >> c;
--pos;
v[s[pos] - 'a'].erase(pos);
s[pos] = c;
v[c - 'a'].insert(pos);
} else {
int l, r;
cin >> l >> r;
--l, --r;
int odgovor = 0;
for (int i = 0; i < 26; ++i) {
auto it = v[i].lower_bound(l);
if (it != v[i].end() && *it <= r) {
++odgovor;
}
}
cout << odgovor << '\n';
}
}
return 0;
} | insert | 21 | 21 | 21 | 23 | -11 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> sevec(26);
for (int i = 0; i < n; ++i) {
int now = s[i] - 'a';
sevec[now].insert(i);
}
for (int i = 0; i < q; ++i) {
int kind;
cin >> kind;
if (kind == 1) {
int iq;
char cq, charbef;
cin >> iq >> cq;
--iq;
charbef = s[iq];
s[iq] = cq;
sevec[charbef - 'a'].erase(iq);
sevec[cq - 'a'].insert(iq);
} else {
int lq, rq;
cin >> lq >> rq;
--lq;
--rq;
int ans = 0;
for (int i = 0; i < 26; ++i) {
auto ite = lower_bound(sevec[i].begin(), sevec[i].end(), lq);
if (ite != sevec[i].end()) {
int val = *ite;
if (val <= rq)
++ans;
}
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> sevec(26);
for (int i = 0; i < n; ++i) {
int now = s[i] - 'a';
sevec[now].insert(i);
}
for (int i = 0; i < q; ++i) {
int kind;
cin >> kind;
if (kind == 1) {
int iq;
char cq, charbef;
cin >> iq >> cq;
--iq;
charbef = s[iq];
s[iq] = cq;
sevec[charbef - 'a'].erase(iq);
sevec[cq - 'a'].insert(iq);
} else {
int lq, rq;
cin >> lq >> rq;
--lq;
--rq;
int ans = 0;
for (int i = 0; i < 26; ++i) {
auto ite = sevec[i].lower_bound(lq);
if (ite != sevec[i].end()) {
int val = *ite;
if (val <= rq)
++ans;
}
}
cout << ans << endl;
}
}
}
| replace | 35 | 36 | 35 | 36 | TLE | |
p02763 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define pp(...) "\033[94m [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "]\033[0m"
#define pp1(...) \
"\033[1;47;35m" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "]\033[0m"
#define dg(x) debug() << pp1(x)
#define dg1(x) debug() << pp(x)
#define dg2(x, y) debug() << pp(x) pp(y)
#define dg3(x, y, z) debug() << pp(x) pp(y) pp(z)
#define dg4(w, x, y, z) debug() << pp(w) pp(x) pp(y) pp(z)
#define int long long
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof x);
#define sz(x) (int)x.size()
template <typename T> void db(T x) { debug() << ">>> " << x; }
template <typename T> void db(T x, T y) {
debug() << ">>> " << x << " , " << y;
}
template <typename T> void db(T x, T y, T z) {
debug() << ">>> " << x << " , " << y << " , " << z;
}
template <typename T> void db(T x, T y, T z, T a) {
debug() << ">>> " << x << " , " << y << " , " << z << " , " << a;
}
const int xx = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inf = 1e18;
const int Maxn = 20010;
int BIT[xx][30];
void update(int n, int id, int pos, int delta) {
while (id <= n) {
BIT[id][pos] += delta;
id += (id & -id);
}
}
int query(int id, int pos) {
int sum = 0;
while (id > 0) {
sum += BIT[id][pos];
id -= (id & -id);
}
return sum;
}
int getdis(int l, int r) {
int ans = 0;
for (int i = 0; i < 26; i++) {
ans += (query(r, i) - query(l - 1, i)) > 0;
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int Q;
cin >> Q;
for (int i = 0; i < n; i++) {
update(n, i + 1, s[i] - 'a', 1);
}
while (Q--) {
int typ;
db("---------------");
dg1(s);
cin >> typ;
if (typ == 1) {
int id;
char ch;
cin >> id >> ch;
update(n, id, s[id - 1] - 'a', -1);
update(n, id, ch - 'a', 1);
s[id - 1] = ch;
} else {
int l, r;
cin >> l >> r;
int res = getdis(l, r);
cout << res << '\n';
}
dg1(s);
}
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define pp(...) "\033[94m [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "]\033[0m"
#define pp1(...) \
"\033[1;47;35m" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "]\033[0m"
#define dg(x) debug() << pp1(x)
#define dg1(x) debug() << pp(x)
#define dg2(x, y) debug() << pp(x) pp(y)
#define dg3(x, y, z) debug() << pp(x) pp(y) pp(z)
#define dg4(w, x, y, z) debug() << pp(w) pp(x) pp(y) pp(z)
#define int long long
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof x);
#define sz(x) (int)x.size()
template <typename T> void db(T x) { debug() << ">>> " << x; }
template <typename T> void db(T x, T y) {
debug() << ">>> " << x << " , " << y;
}
template <typename T> void db(T x, T y, T z) {
debug() << ">>> " << x << " , " << y << " , " << z;
}
template <typename T> void db(T x, T y, T z, T a) {
debug() << ">>> " << x << " , " << y << " , " << z << " , " << a;
}
const int xx = 5e5 + 10;
const int Mod = 1e9 + 7;
const int inf = 1e18;
const int Maxn = 20010;
int BIT[xx][30];
void update(int n, int id, int pos, int delta) {
while (id <= n) {
BIT[id][pos] += delta;
id += (id & -id);
}
}
int query(int id, int pos) {
int sum = 0;
while (id > 0) {
sum += BIT[id][pos];
id -= (id & -id);
}
return sum;
}
int getdis(int l, int r) {
int ans = 0;
for (int i = 0; i < 26; i++) {
ans += (query(r, i) - query(l - 1, i)) > 0;
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int Q;
cin >> Q;
for (int i = 0; i < n; i++) {
update(n, i + 1, s[i] - 'a', 1);
}
while (Q--) {
int typ;
db("---------------");
dg1(s);
cin >> typ;
if (typ == 1) {
int id;
char ch;
cin >> id >> ch;
update(n, id, s[id - 1] - 'a', -1);
update(n, id, ch - 'a', 1);
s[id - 1] = ch;
} else {
int l, r;
cin >> l >> r;
int res = getdis(l, r);
cout << res << '\n';
}
dg1(s);
}
} | replace | 70 | 71 | 70 | 71 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<set<int>> vs(26);
rep(i, n) {
char now = s[i] - 'a';
vs.at(now).insert(i);
}
int q;
cin >> q;
rep(i, q) {
int qq;
cin >> qq;
if (qq == 1) {
int j;
cin >> j;
j--;
char c;
cin >> c;
rep(k, 26) {
if (vs.at(k).count(j) == 1)
vs.at(k).erase(j);
}
vs.at(c - 'a').insert(j);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
rep(j, 26) {
auto itr = lower_bound(vs.at(j).begin(), vs.at(j).end(), l);
if (itr == vs.at(j).end() || r < *itr)
continue;
ans++;
}
cout << ans << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<set<int>> vs(26);
rep(i, n) {
char now = s[i] - 'a';
vs.at(now).insert(i);
}
int q;
cin >> q;
rep(i, q) {
int qq;
cin >> qq;
if (qq == 1) {
int j;
cin >> j;
j--;
char c;
cin >> c;
rep(k, 26) {
if (vs.at(k).count(j) == 1)
vs.at(k).erase(j);
}
vs.at(c - 'a').insert(j);
} else {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
rep(j, 26) {
auto itr = vs.at(j).lower_bound(l);
if (itr == vs.at(j).end() || r < *itr)
continue;
ans++;
}
cout << ans << endl;
}
}
return 0;
} | replace | 43 | 44 | 43 | 44 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int i, j;
int q;
cin >> q;
vector<set<int>> sets(26);
for (i = 0; i < n; i++) {
sets.at(s.at(i) - 'a').insert(i);
}
for (i = 0; i < q; i++) {
int num;
cin >> num;
if (num == 1) {
int ind;
char c;
cin >> ind >> c;
for (j = 0; j < 26; j++)
sets.at(j).erase(ind - 1);
sets.at(c - 'a').insert(ind - 1);
} else if (num == 2) {
int l, r;
cin >> l >> r;
int kind = 0;
for (j = 0; j < 26; j++) {
auto it = lower_bound(sets.at(j).begin(), sets.at(j).end(), l - 1);
if (it != sets.at(j).end() && *it <= r - 1)
kind++;
}
cout << kind << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int i, j;
int q;
cin >> q;
vector<set<int>> sets(26);
for (i = 0; i < n; i++) {
sets.at(s.at(i) - 'a').insert(i);
}
for (i = 0; i < q; i++) {
int num;
cin >> num;
if (num == 1) {
int ind;
char c;
cin >> ind >> c;
for (j = 0; j < 26; j++)
sets.at(j).erase(ind - 1);
sets.at(c - 'a').insert(ind - 1);
} else if (num == 2) {
int l, r;
cin >> l >> r;
int kind = 0;
for (j = 0; j < 26; j++) {
auto it = sets.at(j).lower_bound(l - 1);
if (it != sets.at(j).end() && *it <= r - 1)
kind++;
}
cout << kind << endl;
}
}
}
| replace | 30 | 31 | 30 | 31 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<set<int>> alpha(26);
string S;
/* function */
void op1(int i, char c) {
alpha[S[i] - 'a'].erase(i);
S[i] = c;
alpha[S[i] - 'a'].insert(i);
}
void op2(int l, int r) {
int cnt = 0;
for (int i = 0; i < 26; i++) {
auto itr = lower_bound(alpha[i].begin(), alpha[i].end(), l);
if (itr != alpha[i].end() && r >= *itr)
cnt++;
}
cout << cnt << '\n';
}
/* main */
int main() {
int N, Q;
cin >> N >> S >> Q;
for (int i = 0; i < N; i++) {
alpha[S[i] - 'a'].insert(i);
}
int op, l, r, ind;
char c;
for (int i = 0; i < Q; i++) {
cin >> op;
if (op == 2) {
cin >> l >> r;
l--, r--;
op2(l, r);
} else {
cin >> ind >> c;
ind--;
op1(ind, c);
}
}
}
| #include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<set<int>> alpha(26);
string S;
/* function */
void op1(int i, char c) {
alpha[S[i] - 'a'].erase(i);
S[i] = c;
alpha[S[i] - 'a'].insert(i);
}
void op2(int l, int r) {
int cnt = 0;
for (int i = 0; i < 26; i++) {
auto itr = alpha[i].lower_bound(l);
if (itr != alpha[i].end() && r >= *itr)
cnt++;
}
cout << cnt << '\n';
}
/* main */
int main() {
int N, Q;
cin >> N >> S >> Q;
for (int i = 0; i < N; i++) {
alpha[S[i] - 'a'].insert(i);
}
int op, l, r, ind;
char c;
for (int i = 0; i < Q; i++) {
cin >> op;
if (op == 2) {
cin >> l >> r;
l--, r--;
op2(l, r);
} else {
cin >> ind >> c;
ind--;
op1(ind, c);
}
}
}
| replace | 24 | 25 | 24 | 25 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int MAXN = 1e5 + 10, curr = 0;
int t[26][4 * 100000], a[100000];
;
void build(int v, int tl, int tr) {
if (tl == tr) {
t[curr][v] = (a[tl] == curr);
} else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[curr][v];
}
int tm = (tl + tr) / 2;
return sum(v * 2, tl, tm, l, min(r, tm)) +
sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr) {
t[curr][v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v * 2, tl, tm, pos, new_val);
else
update(v * 2 + 1, tm + 1, tr, pos, new_val);
t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
}
}
int main() {
int n;
cin >> n;
string str;
cin >> str;
int len = str.length();
for (int i = 0; i < len; ++i)
a[i] = str[i] - 'a';
for (int i = 0; i < 26; ++i) {
curr = i;
build(1, 0, len - 1);
}
int q;
cin >> q;
while (q--) {
int k;
cin >> k;
if (k == 1) {
int l;
char r;
cin >> l >> r;
--l;
curr = a[l];
update(1, 0, len - 1, l, 0);
a[l] = r - 'a';
curr = a[l];
update(1, 0, len - 1, l, 1);
}
else {
int l, r;
cin >> l >> r;
int ans = 0;
for (int i = 0; i < 26; ++i) {
curr = i;
int val = sum(1, 0, len - 1, l - 1, r - 1);
if (val > 0)
++ans;
}
cout << ans << '\n';
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int MAXN = 1e9 + 7, curr = 0;
int t[26][4 * 500100], a[4 * 500100];
;
void build(int v, int tl, int tr) {
if (tl == tr) {
t[curr][v] = (a[tl] == curr);
} else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[curr][v];
}
int tm = (tl + tr) / 2;
return sum(v * 2, tl, tm, l, min(r, tm)) +
sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr) {
t[curr][v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v * 2, tl, tm, pos, new_val);
else
update(v * 2 + 1, tm + 1, tr, pos, new_val);
t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
}
}
int main() {
int n;
cin >> n;
string str;
cin >> str;
int len = str.length();
for (int i = 0; i < len; ++i)
a[i] = str[i] - 'a';
for (int i = 0; i < 26; ++i) {
curr = i;
build(1, 0, len - 1);
}
int q;
cin >> q;
while (q--) {
int k;
cin >> k;
if (k == 1) {
int l;
char r;
cin >> l >> r;
--l;
curr = a[l];
update(1, 0, len - 1, l, 0);
a[l] = r - 'a';
curr = a[l];
update(1, 0, len - 1, l, 1);
}
else {
int l, r;
cin >> l >> r;
int ans = 0;
for (int i = 0; i < 26; ++i) {
curr = i;
int val = sum(1, 0, len - 1, l - 1, r - 1);
if (val > 0)
++ans;
}
cout << ans << '\n';
}
}
return 0;
}
| replace | 3 | 5 | 3 | 5 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 500000
set<int> s[26];
signed main() {
int n;
cin >> n;
for (int i = 0; i <= 26; i++)
s[i].insert(n);
string str;
cin >> str;
for (int i = 0; i < str.size(); i++)
s[str[i] - 'a'].insert(i);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int op;
cin >> op;
if (op == 1) {
int pos;
char ch;
cin >> pos >> ch;
pos--;
if (ch == str[pos])
continue;
s[str[pos] - 'a'].erase(pos);
str[pos] = ch;
s[ch - 'a'].insert(pos);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
l--;
r--;
for (int j = 0; j < 26; j++) {
int pos = *s[j].lower_bound(l);
if (pos <= r)
ans++;
}
cout << ans << '\n';
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 500000
set<int> s[26];
signed main() {
int n;
cin >> n;
for (int i = 0; i < 26; i++)
s[i].insert(n);
string str;
cin >> str;
for (int i = 0; i < str.size(); i++)
s[str[i] - 'a'].insert(i);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int op;
cin >> op;
if (op == 1) {
int pos;
char ch;
cin >> pos >> ch;
pos--;
if (ch == str[pos])
continue;
s[str[pos] - 'a'].erase(pos);
str[pos] = ch;
s[ch - 'a'].insert(pos);
} else {
int l, r;
cin >> l >> r;
int ans = 0;
l--;
r--;
for (int j = 0; j < 26; j++) {
int pos = *s[j].lower_bound(l);
if (pos <= r)
ans++;
}
cout << ans << '\n';
}
}
return 0;
} | replace | 8 | 9 | 8 | 9 | -11 | |
p02763 | C++ | Time Limit Exceeded | /*
* じょえチャンネル
* 高評価・チャンネル登録よろしくおねがいします!
* https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
*/
#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < (n); i++)
#define inf (int)(3e18)
#define int long long
#define mod (int)(1000000007)
#define intt long long
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
using namespace std;
// Library
// モッドパウ
int modpow(int x, int y, int m = mod) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
res %= m;
}
x = x * x % mod;
y /= 2;
}
return res;
}
int mypow(int x, int y) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
}
x = x * x;
y /= 2;
}
return res;
}
// is the number (x) a prime number?
bool prime(int x) {
for (int i = 2; i * i <= x; i++) {
if (!(x % i)) {
return false;
}
}
return true;
}
// saidai-kouyakusuu
inline int gcd(int x, int y) {
if (!y) {
return x;
}
return gcd(y, x % y);
}
// number of keta
int keta(int x) {
int ans = 0;
while (x) {
x /= 10;
ans++;
}
return ans;
}
// sum of keta
int ketasum(int x) {
int ans = 0;
while (x) {
ans += x % 10;
x /= 10;
}
return ans;
}
inline int lcm(int x, int y) {
int ans = x / gcd(x, y) * y;
return ans;
}
int twobeki(int x) {
int ans = 0;
while (1) {
if (!(x & 1)) {
ans++;
x /= 2;
} else {
break;
}
}
return ans;
}
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
#define fin(i) scanf("%lld", &i)
#define fout(i) printf("%lld\n", i)
#define fendl printf("\n")
// Library-End
int n, m, q;
string s;
set<int> basho[50];
signed main() {
cin >> n;
cin >> s;
cin >> q;
rep(i, n) {
basho[s[i] - 'a'].insert(i);
// cout<<s[i]-'a'<<' '<<i<<endl;
}
rep(i, q) {
int w;
fin(w);
if (w == 1) {
int f;
char c;
scanf("%lld %c", &f, &c);
f--;
if (s[f] != c) {
basho[s[f] - 'a'].erase(f);
s[f] = c;
basho[c - 'a'].insert(f);
}
} else {
int l, r;
fin(l);
fin(r);
l--;
r--;
int ans = 0;
rep(j, 30) {
auto ba = lower_bound(basho[j].begin(), basho[j].end(), l);
if (ba != basho[j].end()) {
if (*ba <= r)
ans++;
}
}
fout(ans);
}
}
}
| /*
* じょえチャンネル
* 高評価・チャンネル登録よろしくおねがいします!
* https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
*/
#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < (n); i++)
#define inf (int)(3e18)
#define int long long
#define mod (int)(1000000007)
#define intt long long
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
using namespace std;
// Library
// モッドパウ
int modpow(int x, int y, int m = mod) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
res %= m;
}
x = x * x % mod;
y /= 2;
}
return res;
}
int mypow(int x, int y) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
}
x = x * x;
y /= 2;
}
return res;
}
// is the number (x) a prime number?
bool prime(int x) {
for (int i = 2; i * i <= x; i++) {
if (!(x % i)) {
return false;
}
}
return true;
}
// saidai-kouyakusuu
inline int gcd(int x, int y) {
if (!y) {
return x;
}
return gcd(y, x % y);
}
// number of keta
int keta(int x) {
int ans = 0;
while (x) {
x /= 10;
ans++;
}
return ans;
}
// sum of keta
int ketasum(int x) {
int ans = 0;
while (x) {
ans += x % 10;
x /= 10;
}
return ans;
}
inline int lcm(int x, int y) {
int ans = x / gcd(x, y) * y;
return ans;
}
int twobeki(int x) {
int ans = 0;
while (1) {
if (!(x & 1)) {
ans++;
x /= 2;
} else {
break;
}
}
return ans;
}
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
#define fin(i) scanf("%lld", &i)
#define fout(i) printf("%lld\n", i)
#define fendl printf("\n")
// Library-End
int n, m, q;
string s;
set<int> basho[50];
signed main() {
cin >> n;
cin >> s;
cin >> q;
rep(i, n) {
basho[s[i] - 'a'].insert(i);
// cout<<s[i]-'a'<<' '<<i<<endl;
}
rep(i, q) {
int w;
fin(w);
if (w == 1) {
int f;
char c;
scanf("%lld %c", &f, &c);
f--;
if (s[f] != c) {
basho[s[f] - 'a'].erase(f);
s[f] = c;
basho[c - 'a'].insert(f);
}
} else {
int l, r;
fin(l);
fin(r);
l--;
r--;
int ans = 0;
rep(j, 30) {
auto ba = basho[j].lower_bound(l);
if (ba != basho[j].end()) {
if (*ba <= r)
ans++;
}
}
fout(ans);
}
}
}
| replace | 159 | 160 | 159 | 160 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define frw(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define pb push_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpii vector<pii>
#define vpiii vector<piii>
#define vpll vector<pll>
#define vplll vector<plll>
#define mod 1000000007
int modInverse(ll a, ll m) {
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
ll dp[200000];
int po(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
ll nck(ll n, ll k) {
ll ans = 1;
for (int i = 1; i <= k; ++i)
ans = ans * (n - i + 1) % mod * dp[i] % mod;
return ans;
}
int main() {
int n, q;
string s;
cin >> n >> q;
cin >> s;
vector<set<int>> v(26);
for (int i = 0; i < n; i++)
v[s[i] - 'a'].insert(i);
while (q--) {
int t;
cin >> t;
if (t == 1) {
int x;
char y;
cin >> x >> y;
v[s[x - 1] - 'a'].erase(x - 1);
s[x - 1] = y;
v[y - 'a'].insert(x - 1);
} else {
int x, y;
cin >> x >> y;
x--;
y--;
ll ans = 0;
for (int i = 0; i < 26; i++) {
auto z = v[i].lower_bound(x);
if (z != v[i].end() && *z <= y)
ans++;
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define frw(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define pb push_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpii vector<pii>
#define vpiii vector<piii>
#define vpll vector<pll>
#define vplll vector<plll>
#define mod 1000000007
int modInverse(ll a, ll m) {
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
ll dp[200000];
int po(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
ll nck(ll n, ll k) {
ll ans = 1;
for (int i = 1; i <= k; ++i)
ans = ans * (n - i + 1) % mod * dp[i] % mod;
return ans;
}
int main() {
int n, q;
string s;
cin >> n >> s >> q;
// cin>>s;
vector<set<int>> v(26);
for (int i = 0; i < n; i++)
v[s[i] - 'a'].insert(i);
while (q--) {
int t;
cin >> t;
if (t == 1) {
int x;
char y;
cin >> x >> y;
v[s[x - 1] - 'a'].erase(x - 1);
s[x - 1] = y;
v[y - 'a'].insert(x - 1);
} else {
int x, y;
cin >> x >> y;
x--;
y--;
ll ans = 0;
for (int i = 0; i < 26; i++) {
auto z = v[i].lower_bound(x);
if (z != v[i].end() && *z <= y)
ans++;
}
cout << ans << endl;
}
}
} | replace | 65 | 67 | 65 | 67 | -11 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
void query1(const int &p, const char &c, vector<set<int>> &a) {
for (int i = 0; i < 26; i++) {
if (a[i].count(p)) {
a[i].erase(p);
break;
}
}
a[c - 'a'].insert(p);
}
void query2(const int &l, const int &r, const vector<set<int>> &a) {
int cnt = 0;
for (int i = 0; i < 26; i++) {
if (lower_bound(a[i].begin(), a[i].end(), l) !=
upper_bound(a[i].begin(), a[i].end(), r))
cnt++;
}
printf("%d\n", cnt);
}
int main() {
int n;
scanf("%d", &n);
char s[n];
scanf("%s", s);
vector<set<int>> a(26);
for (int i = 0; i < n; i++)
a[s[i] - 'a'].insert(i);
int q;
scanf("%d", &q);
while (q--) {
int p;
scanf("%d", &p);
if (p == 1) {
int i;
char c;
scanf("%d %c", &i, &c);
query1(i - 1, c, a);
} else {
int l, r;
scanf("%d%d", &l, &r);
query2(l - 1, r - 1, a);
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void query1(const int &p, const char &c, vector<set<int>> &a) {
for (int i = 0; i < 26; i++) {
if (a[i].count(p)) {
a[i].erase(p);
break;
}
}
a[c - 'a'].insert(p);
}
void query2(const int &l, const int &r, const vector<set<int>> &a) {
int cnt = 0;
for (int i = 0; i < 26; i++) {
if (a[i].lower_bound(l) != a[i].upper_bound(r))
cnt++;
}
printf("%d\n", cnt);
}
int main() {
int n;
scanf("%d", &n);
char s[n];
scanf("%s", s);
vector<set<int>> a(26);
for (int i = 0; i < n; i++)
a[s[i] - 'a'].insert(i);
int q;
scanf("%d", &q);
while (q--) {
int p;
scanf("%d", &p);
if (p == 1) {
int i;
char c;
scanf("%d %c", &i, &c);
query1(i - 1, c, a);
} else {
int l, r;
scanf("%d%d", &l, &r);
query2(l - 1, r - 1, a);
}
}
return 0;
} | replace | 14 | 16 | 14 | 15 | TLE | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) \
{ \
for (auto i : x) { \
cout << i << " "; \
} \
cout << endl; \
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
// alphabetごとにsetで管理、lower_bound
int N, Q;
string S;
cin >> N >> S >> Q;
vector<set<int>> alpha(26);
rep(i, N) alpha[S[i] - 'a'].insert(i);
rep(_, Q) {
int q;
cin >> q;
if (q == 1) {
int i;
char c;
cin >> i >> c;
--i;
if (S[i] == c)
continue;
alpha[S[i] - 'a'].erase(i);
alpha[c - 'a'].insert(i);
S[i] = c;
} else {
int l, r;
cin >> l >> r;
--l;
int cnt = 0;
rep(j, 26) {
auto itr = lower_bound(alpha[j].begin(), alpha[j].end(), l);
if (itr != alpha[j].end() && *itr < r)
cnt++;
}
printf("%d\n", cnt);
}
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) \
{ \
for (auto i : x) { \
cout << i << " "; \
} \
cout << endl; \
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
// alphabetごとにsetで管理、lower_bound
int N, Q;
string S;
cin >> N >> S >> Q;
vector<set<int>> alpha(26);
rep(i, N) alpha[S[i] - 'a'].insert(i);
rep(_, Q) {
int q;
cin >> q;
if (q == 1) {
int i;
char c;
cin >> i >> c;
--i;
if (S[i] == c)
continue;
alpha[S[i] - 'a'].erase(i);
alpha[c - 'a'].insert(i);
S[i] = c;
} else {
int l, r;
cin >> l >> r;
--l;
int cnt = 0;
rep(j, 26) {
auto itr = alpha[j].lower_bound(l);
if (itr != alpha[j].end() && *itr < r)
cnt++;
}
printf("%d\n", cnt);
}
}
return 0;
} | replace | 40 | 41 | 40 | 41 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> is(26);
rep(i, n) { is[s[i] - 'a'].insert(i); }
rep(qi, n) {
int ty;
cin >> ty;
if (ty == 1) {
int i;
char c;
cin >> i >> c;
--i;
is[s[i] - 'a'].erase(i);
s[i] = c;
is[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
rep(i, 26) {
auto it = is[i].lower_bound(l);
if (it != is[i].end() && *it < r)
++ans;
}
printf("%d\n", ans);
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
vector<set<int>> is(26);
rep(i, n) { is[s[i] - 'a'].insert(i); }
rep(qi, q) {
int ty;
cin >> ty;
if (ty == 1) {
int i;
char c;
cin >> i >> c;
--i;
is[s[i] - 'a'].erase(i);
s[i] = c;
is[s[i] - 'a'].insert(i);
} else {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
rep(i, 26) {
auto it = is[i].lower_bound(l);
if (it != is[i].end() && *it < r)
++ans;
}
printf("%d\n", ans);
}
}
return 0;
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int popcount(int n) {
int res = 0;
for (int i = 0; i < 32; ++i) {
if (n & (1 << i))
res++;
}
return res;
};
int bit_length(int n) {
for (int i = 31; i >= 0; --i) {
if (n & (1 << i))
return i + 1;
}
return 1;
}
struct SegmentTree {
int n;
// cast
int ide;
int tree[1000000];
SegmentTree(int k, int id) {
ide = id;
n = k;
if (popcount(n) > 1) {
n = 1 << bit_length(n);
}
for (int i = 0; i < 2 * n; ++i) {
tree[i] = ide;
}
}
// rewrite
int func(int a, int b) { return a | b; };
void update(int i, int x) {
i += n - 1;
tree[i] = x;
while (i > 1) {
i /= 2;
tree[i] = func(tree[2 * i], tree[2 * i + 1]);
};
};
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return ide;
if (a <= l && r <= b)
return tree[k];
return func(query(a, b, k * 2, l, (l + r) / 2),
query(a, b, k * 2 + 1, (l + r) / 2, r));
};
};
int main() {
int n;
string s;
cin >> n >> s;
SegmentTree st(n, 0);
for (int i = 1; i <= n; i++) {
st.update(i, 1 << (int)(s[i - 1] - 'a'));
}
int q, type, l, r, res;
char t;
cin >> q;
rep(i, q) {
cin >> type >> l;
if (type == 1) {
cin >> t;
st.update(l, 1 << (int)(t - 'a'));
} else if (type == 2) {
cin >> r;
res = st.query(l, r + 1, 1, 1, st.n + 1);
cout << popcount(res) << "\n";
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int popcount(int n) {
int res = 0;
for (int i = 0; i < 32; ++i) {
if (n & (1 << i))
res++;
}
return res;
};
int bit_length(int n) {
for (int i = 31; i >= 0; --i) {
if (n & (1 << i))
return i + 1;
}
return 1;
}
struct SegmentTree {
int n;
// cast
int ide;
int tree[1600000];
SegmentTree(int k, int id) {
ide = id;
n = k;
if (popcount(n) > 1) {
n = 1 << bit_length(n);
}
for (int i = 0; i < 2 * n; ++i) {
tree[i] = ide;
}
}
// rewrite
int func(int a, int b) { return a | b; };
void update(int i, int x) {
i += n - 1;
tree[i] = x;
while (i > 1) {
i /= 2;
tree[i] = func(tree[2 * i], tree[2 * i + 1]);
};
};
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return ide;
if (a <= l && r <= b)
return tree[k];
return func(query(a, b, k * 2, l, (l + r) / 2),
query(a, b, k * 2 + 1, (l + r) / 2, r));
};
};
int main() {
int n;
string s;
cin >> n >> s;
SegmentTree st(n, 0);
for (int i = 1; i <= n; i++) {
st.update(i, 1 << (int)(s[i - 1] - 'a'));
}
int q, type, l, r, res;
char t;
cin >> q;
rep(i, q) {
cin >> type >> l;
if (type == 1) {
cin >> t;
st.update(l, 1 << (int)(t - 'a'));
} else if (type == 2) {
cin >> r;
res = st.query(l, r + 1, 1, 1, st.n + 1);
cout << popcount(res) << "\n";
}
}
} | replace | 28 | 29 | 28 | 29 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) \
cerr << #mp << ":" << endl; \
for (auto i = mp.begin(); i != mp.end(); ++i) { \
cerr << i->first << ":" << i->second << endl; \
}
#define dbgarr(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j] << " "; } \
cerr << endl; \
}
#define dbgdp(n, arr) \
rep(i, n) { cerr << arr[i] << " "; } \
cerr << endl;
#define dbgmint(n, arr) \
rep(i, n) { cerr << arr[i].x << " "; } \
cerr << endl;
#define dbgarrmint(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j].x << " "; } \
cerr << endl; \
}
#else
#define dbg(...) 42
#define dbgmap(...) 42
#define dbgarr(...) 42
#define dbgdp(...) 42
#define dbgmint(...) 42
#define dbgarrmint(...) 42
#endif
#define out(a) cout << a << endl
#define out2(a, b) cout << a << " " << b << endl
#define vout(v) \
rep(i, v.size()) { cout << v[i] << " "; } \
cout << endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
rep(j, v.size()) i >> v[j];
return i;
}
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << " ";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
// mint
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
ll n;
string str;
cin >> n >> str;
ll Q;
cin >> Q;
auto f = [](ll a, ll b) { return a + b; };
vector<SegmentTree<ll>> vecseg(26, SegmentTree<ll>(n, f, 0));
rep(i, n) {
vecseg[i].build();
vecseg[str[i] - 'a'].update(i, 1);
}
while (Q--) {
ll q;
cin >> q;
if (q == 1) {
ll x;
char c;
cin >> x >> c;
x--;
char cur = str[x];
vecseg[cur - 'a'].update(x, vecseg[cur - 'a'].query(x, x + 1) - 1);
vecseg[c - 'a'].update(x, vecseg[cur - 'a'].query(x, x + 1) + 1);
str[x] = c;
} else {
ll l, r;
cin >> l >> r;
l--;
r--;
ll ans = 0;
rep(i, 26) {
ll cnt = vecseg[i].query(l, r + 1);
if (cnt > 0) {
dbg(i);
ans++;
}
}
out(ans);
}
}
// dbg(del);
// dbg(add);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) \
cerr << #mp << ":" << endl; \
for (auto i = mp.begin(); i != mp.end(); ++i) { \
cerr << i->first << ":" << i->second << endl; \
}
#define dbgarr(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j] << " "; } \
cerr << endl; \
}
#define dbgdp(n, arr) \
rep(i, n) { cerr << arr[i] << " "; } \
cerr << endl;
#define dbgmint(n, arr) \
rep(i, n) { cerr << arr[i].x << " "; } \
cerr << endl;
#define dbgarrmint(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j].x << " "; } \
cerr << endl; \
}
#else
#define dbg(...) 42
#define dbgmap(...) 42
#define dbgarr(...) 42
#define dbgdp(...) 42
#define dbgmint(...) 42
#define dbgarrmint(...) 42
#endif
#define out(a) cout << a << endl
#define out2(a, b) cout << a << " " << b << endl
#define vout(v) \
rep(i, v.size()) { cout << v[i] << " "; } \
cout << endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
rep(j, v.size()) i >> v[j];
return i;
}
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << " ";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
// mint
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
ll n;
string str;
cin >> n >> str;
ll Q;
cin >> Q;
auto f = [](ll a, ll b) { return a + b; };
vector<SegmentTree<ll>> vecseg(26, SegmentTree<ll>(n, f, 0));
rep(i, 26) { vecseg[i].build(); }
rep(i, n) { vecseg[str[i] - 'a'].update(i, 1); }
while (Q--) {
ll q;
cin >> q;
if (q == 1) {
ll x;
char c;
cin >> x >> c;
x--;
char cur = str[x];
vecseg[cur - 'a'].update(x, vecseg[cur - 'a'].query(x, x + 1) - 1);
vecseg[c - 'a'].update(x, vecseg[cur - 'a'].query(x, x + 1) + 1);
str[x] = c;
} else {
ll l, r;
cin >> l >> r;
l--;
r--;
ll ans = 0;
rep(i, 26) {
ll cnt = vecseg[i].query(l, r + 1);
if (cnt > 0) {
dbg(i);
ans++;
}
}
out(ans);
}
}
// dbg(del);
// dbg(add);
return 0;
}
| replace | 293 | 297 | 293 | 295 | 0 | |
p02763 | C++ | Runtime Error | /**
* created: 22.09.2020 01:48:16
**/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
// #define endl '\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define len(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
using ll = long long;
using P = pair<int, int>;
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline void print(P a) { cout << a.first << " " << a.second << endl; }
template <typename T> T pow(T a, ll n) {
T r(1);
while (n) {
if (n & 1)
r *= a;
a *= a;
n >>= 1;
}
return r;
}
struct faster_io {
faster_io() {
cin.tie(0);
ios_base::sync_with_stdio(false);
}
} faster_io_;
// Segment Tree
// query : f[a,b), Ο(log(n))
template <typename X> struct SegTree {
using F = function<X(X, X)>;
int n;
F f;
const X ex;
vector<X> dat;
SegTree(int n_, F f_, X ex_) : n(), f(f_), ex(ex_), dat(n_ * 4, ex_) {
int x = 1;
while (n_ > x) {
x *= 2;
}
n = x;
}
void set(int i, X x) {
dat[i + n - 1] = x;
return;
}
void build() {
rrep(k, n - 1) dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
return;
}
void update(int i, X x) {
i += n - 1;
dat[i] = x; // subject to change
while (i) {
i = (i - 1) / 2;
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
return;
}
X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
private:
X query_sub(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return ex;
else if (a <= l && r <= b)
return dat[k];
else {
X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
}
};
signed main() {
int n, q;
string s;
cin >> n >> s >> q;
using X = int;
auto f = [](X x1, X x2) -> X { return x1 | x2; };
X ex = 0;
SegTree<X> seg(n, f, ex);
auto toi = [](char c) {
int d = c - 'a';
int ret = 1ll << d;
return ret;
};
rep(i, q) seg.set(i, toi(s[i]));
seg.build();
rep(i, q) {
int t;
cin >> t;
if (t == 1) {
int a;
char c;
cin >> a >> c;
a--;
seg.update(a, toi(c));
}
if (t == 2) {
int a, b;
cin >> a >> b;
a--;
cout << pcnt(seg.query(a, b)) << endl;
}
}
return 0;
} | /**
* created: 22.09.2020 01:48:16
**/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
// #define endl '\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define len(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
using ll = long long;
using P = pair<int, int>;
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline void print(P a) { cout << a.first << " " << a.second << endl; }
template <typename T> T pow(T a, ll n) {
T r(1);
while (n) {
if (n & 1)
r *= a;
a *= a;
n >>= 1;
}
return r;
}
struct faster_io {
faster_io() {
cin.tie(0);
ios_base::sync_with_stdio(false);
}
} faster_io_;
// Segment Tree
// query : f[a,b), Ο(log(n))
template <typename X> struct SegTree {
using F = function<X(X, X)>;
int n;
F f;
const X ex;
vector<X> dat;
SegTree(int n_, F f_, X ex_) : n(), f(f_), ex(ex_), dat(n_ * 4, ex_) {
int x = 1;
while (n_ > x) {
x *= 2;
}
n = x;
}
void set(int i, X x) {
dat[i + n - 1] = x;
return;
}
void build() {
rrep(k, n - 1) dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
return;
}
void update(int i, X x) {
i += n - 1;
dat[i] = x; // subject to change
while (i) {
i = (i - 1) / 2;
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
return;
}
X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
private:
X query_sub(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return ex;
else if (a <= l && r <= b)
return dat[k];
else {
X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
}
};
signed main() {
int n, q;
string s;
cin >> n >> s >> q;
using X = int;
auto f = [](X x1, X x2) -> X { return x1 | x2; };
X ex = 0;
SegTree<X> seg(n, f, ex);
auto toi = [](char c) {
int d = c - 'a';
int ret = 1ll << d;
return ret;
};
rep(i, n) seg.set(i, toi(s[i]));
seg.build();
rep(i, q) {
int t;
cin >> t;
if (t == 1) {
int a;
char c;
cin >> a >> c;
a--;
seg.update(a, toi(c));
}
if (t == 2) {
int a, b;
cin >> a >> b;
a--;
cout << pcnt(seg.query(a, b)) << endl;
}
}
return 0;
} | replace | 119 | 120 | 119 | 120 | 0 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int, int>
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;
int main() {
fast_io int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> alp(26);
rep(i, n) alp[s[i] - 'a'].insert(i);
int type, ind, l, r;
char c;
vector<int> ans;
rep(i, q) {
cin >> type;
if (type == 1) {
cin >> ind >> c;
ind--;
alp[s[ind] - 'a'].erase(ind);
alp[c - 'a'].insert(ind);
s[ind] = c;
} else {
cin >> l >> r;
l--;
r--;
int ans_tmp = 0;
rep(j, 26) {
auto lb = lower_bound(alp[j].begin(), alp[j].end(), l);
if (*lb > r || lb == alp[j].end())
continue;
ans_tmp++;
}
ans.push_back(ans_tmp);
}
}
for (int a : ans)
cout << a << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int, int>
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;
int main() {
fast_io int n, q;
string s;
cin >> n >> s >> q;
vector<set<int>> alp(26);
rep(i, n) alp[s[i] - 'a'].insert(i);
int type, ind, l, r;
char c;
vector<int> ans;
rep(i, q) {
cin >> type;
if (type == 1) {
cin >> ind >> c;
ind--;
alp[s[ind] - 'a'].erase(ind);
alp[c - 'a'].insert(ind);
s[ind] = c;
} else {
cin >> l >> r;
l--;
r--;
int ans_tmp = 0;
rep(j, 26) {
auto lb = alp[j].lower_bound(l);
if (*lb > r || lb == alp[j].end())
continue;
ans_tmp++;
}
ans.push_back(ans_tmp);
}
}
for (int a : ans)
cout << a << endl;
return 0;
} | replace | 38 | 39 | 38 | 39 | TLE | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
/*
* coder :: ATUL_PANDEY_2608
* >>> INDIA <<<
*/
using namespace std;
using namespace __gnu_pbds;
// #define part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T>
using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// end of #define
// define globals ...
const int maxx = 2e5 + 26;
// write function from here ...
int ft[maxx][26];
int n;
void update(int i, int val, int j) {
for (++i; i <= n; i += (i & -i)) {
ft[i][j] += val;
}
}
vector<int> qry(int i) {
vector<int> alp(26, 0);
for (; i; i -= (i & -i)) {
for (int j = 0; j < 26; j++) {
if (ft[i][j] > 0) {
alp[j] += ft[i][j];
}
}
}
return alp;
}
int Main() {
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
update(i, 1, s[i] - 'a');
}
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) {
int i;
char c;
cin >> i;
i--;
cin >> c;
update(i, -1, s[i] - 'a');
update(i, 1, c - 'a');
s[i] = c;
} else {
int l, r;
cin >> l >> r;
vector<int> ans1 = qry(r), ans2 = qry(l - 1);
int ans = 0;
////int alp [26];
// memset(alp,0,sizeof(alp));
for (int i = 0; i < 26; i++) {
if (ans1[i] > ans2[i])
ans++;
}
cout << ans << endl;
}
}
return 0;
}
int main() {
quick;
int t = 1;
// cin>>t;
while (t--)
Main();
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
/*
* coder :: ATUL_PANDEY_2608
* >>> INDIA <<<
*/
using namespace std;
using namespace __gnu_pbds;
// #define part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T>
using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// end of #define
// define globals ...
const int maxx = 2e6 + 26;
// write function from here ...
int ft[maxx][26];
int n;
void update(int i, int val, int j) {
for (++i; i <= n; i += (i & -i)) {
ft[i][j] += val;
}
}
vector<int> qry(int i) {
vector<int> alp(26, 0);
for (; i; i -= (i & -i)) {
for (int j = 0; j < 26; j++) {
if (ft[i][j] > 0) {
alp[j] += ft[i][j];
}
}
}
return alp;
}
int Main() {
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
update(i, 1, s[i] - 'a');
}
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) {
int i;
char c;
cin >> i;
i--;
cin >> c;
update(i, -1, s[i] - 'a');
update(i, 1, c - 'a');
s[i] = c;
} else {
int l, r;
cin >> l >> r;
vector<int> ans1 = qry(r), ans2 = qry(l - 1);
int ans = 0;
////int alp [26];
// memset(alp,0,sizeof(alp));
for (int i = 0; i < 26; i++) {
if (ans1[i] > ans2[i])
ans++;
}
cout << ans << endl;
}
}
return 0;
}
int main() {
quick;
int t = 1;
// cin>>t;
while (t--)
Main();
return 0;
}
| replace | 34 | 35 | 34 | 35 | 0 | |
p02763 | C++ | Runtime Error | // review
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
struct Query {
ll type, i, l, r;
char c;
};
const int MAXQ = 20010;
ll n;
ll q;
string s;
Query queries[MAXQ];
void solve() {
set<int> positions[26];
for (int i = 0; i < n; i++) {
positions[s[i] - 'a'].insert(i + 1);
}
for (int i = 0; i < q; i++) {
Query &query = queries[i];
if (query.type == 1) {
positions[s[i] - 'a'].erase(query.i);
positions[query.c - 'a'].insert(query.i);
s[query.i - 1] = query.c;
} else {
int cnt = 0;
for (int j = 0; j < 26; j++) {
auto iter = positions[j].lower_bound(query.l);
if (iter != positions[j].end() && *iter <= query.r) {
cnt++;
}
}
cout << cnt << endl;
}
}
}
int main() {
cin >> n >> s >> q;
for (int i = 0; i < q; i++) {
Query &query = queries[i];
cin >> query.type;
if (query.type == 1) {
cin >> query.i >> query.c;
} else {
cin >> query.l >> query.r;
}
}
solve();
}
| // review
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
struct Query {
ll type, i, l, r;
char c;
};
const int MAXQ = 20010;
ll n;
ll q;
string s;
Query queries[MAXQ];
void solve() {
set<int> positions[26];
for (int i = 0; i < n; i++) {
positions[s[i] - 'a'].insert(i + 1);
}
for (int i = 0; i < q; i++) {
Query &query = queries[i];
if (query.type == 1) {
positions[s[query.i - 1] - 'a'].erase(query.i);
positions[query.c - 'a'].insert(query.i);
s[query.i - 1] = query.c;
} else {
int cnt = 0;
for (int j = 0; j < 26; j++) {
auto iter = positions[j].lower_bound(query.l);
if (iter != positions[j].end() && *iter <= query.r) {
cnt++;
}
}
cout << cnt << endl;
}
}
}
int main() {
cin >> n >> s >> q;
for (int i = 0; i < q; i++) {
Query &query = queries[i];
cin >> query.type;
if (query.type == 1) {
cin >> query.i >> query.c;
} else {
cin >> query.l >> query.r;
}
}
solve();
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02763 | C++ | Runtime Error | #pragma region template
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
/*
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long max_value = INF, max_index = -1;
long long min_value = MAX, min_index = -1;*/
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W,
x, y, z;
typedef long long ll;
typedef pair<ll, ll> Pa;
/*
template <std::int_fast64_t Modulus> class modint {
using u64 = std::int_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {}
constexpr u64& value() noexcept { return a; }
constexpr const u64& value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint& operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint& operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint& operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint& operator/=(modint rhs) noexcept {
u64 exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
};
#pragma endregion
typedef modint<MOD> m;
m func[400001];
void funcinit(int N)
{
func[0] = 1;
for (int i = 1; i <= N; i++)
{
func[i] = func[i - 1] * i;
}
}
m comb(m n, m r)
{
if (n.a <= 0 || n.a < r.a)
{
return 0;
}
return func[n.a] / (func[r.a] * func[(n - r).a]);
}*/
string s;
set<int> pos[26];
int main() {
cin >> N >> s >> Q;
rep(i, N) { pos[s[i] - 'a'].insert(i); }
rep(i, Q) {
char c;
cin >> A >> B;
if (A == 1) {
cin >> c;
B--;
pos[s[B - 1] - 'a'].erase(B);
s[B - 1] = c;
pos[c - 'a'].insert(B);
}
if (A == 2) {
cin >> C;
int count = 0;
B--;
rep(j, 26) {
auto it = pos[j].lower_bound(B);
if (it != pos[j].end() && *it < C)
count++;
}
cout << count << endl;
}
}
}
| #pragma region template
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
/*
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long max_value = INF, max_index = -1;
long long min_value = MAX, min_index = -1;*/
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W,
x, y, z;
typedef long long ll;
typedef pair<ll, ll> Pa;
/*
template <std::int_fast64_t Modulus> class modint {
using u64 = std::int_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {}
constexpr u64& value() noexcept { return a; }
constexpr const u64& value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint& operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint& operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint& operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint& operator/=(modint rhs) noexcept {
u64 exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
};
#pragma endregion
typedef modint<MOD> m;
m func[400001];
void funcinit(int N)
{
func[0] = 1;
for (int i = 1; i <= N; i++)
{
func[i] = func[i - 1] * i;
}
}
m comb(m n, m r)
{
if (n.a <= 0 || n.a < r.a)
{
return 0;
}
return func[n.a] / (func[r.a] * func[(n - r).a]);
}*/
string s;
set<int> pos[26];
int main() {
cin >> N >> s >> Q;
rep(i, N) { pos[s[i] - 'a'].insert(i); }
rep(i, Q) {
char c;
cin >> A >> B;
if (A == 1) {
cin >> c;
B--;
pos[s[B] - 'a'].erase(B);
s[B] = c;
pos[s[B] - 'a'].insert(B);
}
if (A == 2) {
cin >> C;
int count = 0;
B--;
rep(j, 26) {
auto it = pos[j].lower_bound(B);
if (it != pos[j].end() && *it < C)
count++;
}
cout << count << endl;
}
}
}
| replace | 95 | 98 | 95 | 98 | 0 | |
p02763 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <cmath>
const double PI = 3.14159265358979323846;
// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 1e15;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
std::istream &operator>>(std::istream &in, set<int> &o) {
ll a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
// std::cout << std::bitset<8>(9);
const int mod = 1000000007;
// const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string>> PQ_ASK;
struct SegTreeMax {
ll N, dat[5 * 500000];
SegTreeMax(ll n) {
N = 1;
while (N < n)
N *= 2;
for (ll i = 0; i < 2 * N - 1; i++)
dat[i] = -INF;
}
// update k th element
void update(ll k, ll a) {
k += N - 1; // leaf
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
// max [a, b)
ll query(ll a, ll b) { return query(a, b, 0, 0, N); }
ll query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a or b <= l)
return -INF;
if (a <= l and r <= b)
return dat[k];
ll m = (l + r) / 2;
return max(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
}
};
int main() {
int n, q;
string s;
cin >> n >> s >> q;
vector<SegTreeMax> segs(26, SegTreeMax(n));
rep(i, 26) {
rep(j, n) { segs[i].update(j, 0); }
}
rep(i, q) {
char c = s[i];
segs[c - 'a'].update(i, 1);
}
rep(i, q) {
int method;
cin >> method;
if (method == 1) {
int j;
char c;
cin >> j >> c;
j--;
rep(k, 26) { segs[k].update(j, 0); }
segs[c - 'a'].update(j, 1);
} else {
int l, r;
cin >> l >> r;
l--;
int ans = 0;
rep(k, 26) {
int now = segs[k].query(l, r);
assert(now == 1 || now == 0);
ans += now;
}
cout << ans << endl;
}
}
}
| #include <bits/stdc++.h>
#include <cmath>
const double PI = 3.14159265358979323846;
// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 1e15;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
std::istream &operator>>(std::istream &in, set<int> &o) {
ll a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
// std::cout << std::bitset<8>(9);
const int mod = 1000000007;
// const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string>> PQ_ASK;
struct SegTreeMax {
ll N, dat[5 * 500000];
SegTreeMax(ll n) {
N = 1;
while (N < n)
N *= 2;
for (ll i = 0; i < 2 * N - 1; i++)
dat[i] = -INF;
}
// update k th element
void update(ll k, ll a) {
k += N - 1; // leaf
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
// max [a, b)
ll query(ll a, ll b) { return query(a, b, 0, 0, N); }
ll query(ll a, ll b, ll k, ll l, ll r) {
if (r <= a or b <= l)
return -INF;
if (a <= l and r <= b)
return dat[k];
ll m = (l + r) / 2;
return max(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
}
};
int main() {
int n, q;
string s;
cin >> n >> s >> q;
vector<SegTreeMax> segs(26, SegTreeMax(n));
rep(i, 26) {
rep(j, n) { segs[i].update(j, 0); }
}
rep(i, s.size()) {
char c = s[i];
segs[c - 'a'].update(i, 1);
}
rep(i, q) {
int method;
cin >> method;
if (method == 1) {
int j;
char c;
cin >> j >> c;
j--;
rep(k, 26) { segs[k].update(j, 0); }
segs[c - 'a'].update(j, 1);
} else {
int l, r;
cin >> l >> r;
l--;
int ans = 0;
rep(k, 26) {
int now = segs[k].query(l, r);
assert(now == 1 || now == 0);
ans += now;
}
cout << ans << endl;
}
}
}
| replace | 86 | 87 | 86 | 87 | -11 | |
p02763 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b) \
for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)
#define VIN(v) \
for (auto &elem_ : (v)) \
cin >> elem_
#define VOUT(v, sep) \
for (_loop_int idx = 0; idx < (_loop_int)v.size(); idx++) { \
cout << v[idx]; \
if (idx < (_loop_int)v.size() - 1) \
cout << sep; \
} \
cout << endl
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG2(x, y) cout << #x << ": " << x << " " << #y << ": " << y << endl
#define DEBUG_VEC(v) \
cout << #v << ":"; \
REP(debugidx, v.size()) cout << " " << v[debugidx]; \
cout << endl
#define DEBUG_ARR(v, n) \
cout << #v << ":"; \
REP(debugidx, n) cout << " " << v[debugidx]; \
cout << endl
const ll MOD = 1000000007ll;
const int IINF = numeric_limits<int>::max() / 2 - 1;
const ll LINF = numeric_limits<ll>::max() / 2 - 1;
template <class T> inline bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
// ios::sync_with_stdio(false); cin.tie(0);
int n;
string s;
cin >> n;
cin >> s;
// vector<vi> v(26);
set<int> v[26];
REP(i, n) {
// v[s[i]-'a'].emplace_back(i+1);
v[s[i] - 'a'].insert(i + 1);
}
int q;
cin >> q;
REP(lp, q) {
int com, l, r;
char a;
cin >> com;
if (com == 1) {
// 変更
cin >> l >> a;
// 削除
v[s[l - 1] - 'a'].erase(l);
s[l - 1] = a;
// 挿入
v[s[l - 1] - 'a'].insert(l);
} else {
// 出力
cin >> l >> r;
int ans = 0;
REP(i, 26) {
auto it = lower_bound(ALL(v[i]), l);
if (it != v[i].end() && *it <= r) {
ans++;
}
}
// cout<<ans<<endl;
printf("%d\n", ans);
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b) \
for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)
#define VIN(v) \
for (auto &elem_ : (v)) \
cin >> elem_
#define VOUT(v, sep) \
for (_loop_int idx = 0; idx < (_loop_int)v.size(); idx++) { \
cout << v[idx]; \
if (idx < (_loop_int)v.size() - 1) \
cout << sep; \
} \
cout << endl
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG2(x, y) cout << #x << ": " << x << " " << #y << ": " << y << endl
#define DEBUG_VEC(v) \
cout << #v << ":"; \
REP(debugidx, v.size()) cout << " " << v[debugidx]; \
cout << endl
#define DEBUG_ARR(v, n) \
cout << #v << ":"; \
REP(debugidx, n) cout << " " << v[debugidx]; \
cout << endl
const ll MOD = 1000000007ll;
const int IINF = numeric_limits<int>::max() / 2 - 1;
const ll LINF = numeric_limits<ll>::max() / 2 - 1;
template <class T> inline bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
// ios::sync_with_stdio(false); cin.tie(0);
int n;
string s;
cin >> n;
cin >> s;
// vector<vi> v(26);
set<int> v[26];
REP(i, n) {
// v[s[i]-'a'].emplace_back(i+1);
v[s[i] - 'a'].insert(i + 1);
}
int q;
cin >> q;
REP(lp, q) {
int com, l, r;
char a;
cin >> com;
if (com == 1) {
// 変更
cin >> l >> a;
// 削除
v[s[l - 1] - 'a'].erase(l);
s[l - 1] = a;
// 挿入
v[s[l - 1] - 'a'].insert(l);
} else {
// 出力
cin >> l >> r;
int ans = 0;
REP(i, 26) {
auto it = v[i].lower_bound(l);
if (it != v[i].end() && *it <= r) {
ans++;
}
}
// cout<<ans<<endl;
printf("%d\n", ans);
}
}
return 0;
} | replace | 93 | 94 | 93 | 94 | TLE | |
p02763 | C++ | Runtime Error | // https://atcoder.jp/contests/abc157/tasks/abc157_e
/*
* Author : MaxSally
*/
/******** All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/******* All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define rrep(i, j, k) for (int i = j; i >= k; --i)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(i, a) for (auto i : a)
#define forEach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert(B <= A && A <= C)
#define mp make_pair
#define pb push_back
#define inf (int)(1e9 + 7)
#define epsi 1e-9
#define PI 3.1415926535897932384626433832795
#define mod 1000000007
#define read(type) readInt<type>()
#define io \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define left tuyiuoi
#define right fgjhk
#define ss second
#define ff first
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) \
rep(i, l, r) cout << a[i] << " "; \
cout << endl
#define Flag(n) cout << "here " << n << endl
const double pi = acos(-1.0);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
const int N = 500005;
int n;
string s;
void buildTree(vector<int> &segmentTree, int left, int right, int ind) {
if (left == right) {
segmentTree[ind] = 1 << (s[left] - 'a');
return;
}
int mid = (left + right) / 2;
buildTree(segmentTree, left, mid, 2 * ind + 1);
buildTree(segmentTree, mid + 1, right, 2 * ind + 2);
segmentTree[ind] = segmentTree[2 * ind + 1] | segmentTree[2 * ind + 2];
}
int query(vector<int> &segmentTree, int left, int right, int ind, int leftQ,
int rightQ) {
if (right < left || right < leftQ || rightQ < left) {
return 0;
}
if (leftQ <= left && right <= rightQ) {
return segmentTree[ind];
}
int mid = (left + right) / 2;
int leftNode = query(segmentTree, left, mid, 2 * ind + 1, leftQ, rightQ);
int rightNode =
query(segmentTree, mid + 1, right, 2 * ind + 2, leftQ, rightQ);
return (leftNode | rightNode);
}
void update(vi &segmentTree, int left, int right, int ind, int pos, char c) {
if (pos > right || pos < left)
return;
if (left == right) {
if (pos == left) {
segmentTree[ind] -= (1 << (s[left] - 'a'));
s[left] = c;
segmentTree[ind] += (1 << (s[left] - 'a'));
}
return;
}
int mid = (left + right) / 2;
update(segmentTree, left, mid, 2 * ind + 1, pos, c);
update(segmentTree, mid + 1, right, 2 * ind + 2, pos, c);
segmentTree[ind] = segmentTree[2 * ind + 1] | segmentTree[2 * ind + 2];
}
int main() {
io;
// freopen("input.txt", "r", stdin);
cin >> n >> s;
int szTree = 2 * (n + 1);
vi segmentTree(szTree);
buildTree(segmentTree, 0, n - 1, 0);
int m;
cin >> m;
while (m--) {
int type;
cin >> type;
if (type == 1) {
int ind;
char c;
cin >> ind >> c;
ind--;
update(segmentTree, 0, n - 1, 0, ind, c);
} else {
int x, y;
cin >> x >> y;
x--;
y--;
int ans = query(segmentTree, 0, n - 1, 0, x, y);
int cnt = 0;
rep(i, 0, 'z' - 'a') {
if ((ans & (1 << i)))
cnt++;
}
cout << cnt << '\n';
}
}
return 0;
}
| // https://atcoder.jp/contests/abc157/tasks/abc157_e
/*
* Author : MaxSally
*/
/******** All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/******* All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define rrep(i, j, k) for (int i = j; i >= k; --i)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(i, a) for (auto i : a)
#define forEach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert(B <= A && A <= C)
#define mp make_pair
#define pb push_back
#define inf (int)(1e9 + 7)
#define epsi 1e-9
#define PI 3.1415926535897932384626433832795
#define mod 1000000007
#define read(type) readInt<type>()
#define io \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define left tuyiuoi
#define right fgjhk
#define ss second
#define ff first
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) \
rep(i, l, r) cout << a[i] << " "; \
cout << endl
#define Flag(n) cout << "here " << n << endl
const double pi = acos(-1.0);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
const int N = 500005;
int n;
string s;
void buildTree(vector<int> &segmentTree, int left, int right, int ind) {
if (left == right) {
segmentTree[ind] = 1 << (s[left] - 'a');
return;
}
int mid = (left + right) / 2;
buildTree(segmentTree, left, mid, 2 * ind + 1);
buildTree(segmentTree, mid + 1, right, 2 * ind + 2);
segmentTree[ind] = segmentTree[2 * ind + 1] | segmentTree[2 * ind + 2];
}
int query(vector<int> &segmentTree, int left, int right, int ind, int leftQ,
int rightQ) {
if (right < left || right < leftQ || rightQ < left) {
return 0;
}
if (leftQ <= left && right <= rightQ) {
return segmentTree[ind];
}
int mid = (left + right) / 2;
int leftNode = query(segmentTree, left, mid, 2 * ind + 1, leftQ, rightQ);
int rightNode =
query(segmentTree, mid + 1, right, 2 * ind + 2, leftQ, rightQ);
return (leftNode | rightNode);
}
void update(vi &segmentTree, int left, int right, int ind, int pos, char c) {
if (pos > right || pos < left)
return;
if (left == right) {
if (pos == left) {
segmentTree[ind] -= (1 << (s[left] - 'a'));
s[left] = c;
segmentTree[ind] += (1 << (s[left] - 'a'));
}
return;
}
int mid = (left + right) / 2;
update(segmentTree, left, mid, 2 * ind + 1, pos, c);
update(segmentTree, mid + 1, right, 2 * ind + 2, pos, c);
segmentTree[ind] = segmentTree[2 * ind + 1] | segmentTree[2 * ind + 2];
}
int main() {
io;
// freopen("input.txt", "r", stdin);
cin >> n >> s;
int szTree = 4 * (n + 1);
vi segmentTree(szTree);
buildTree(segmentTree, 0, n - 1, 0);
int m;
cin >> m;
while (m--) {
int type;
cin >> type;
if (type == 1) {
int ind;
char c;
cin >> ind >> c;
ind--;
update(segmentTree, 0, n - 1, 0, ind, c);
} else {
int x, y;
cin >> x >> y;
x--;
y--;
int ans = query(segmentTree, 0, n - 1, 0, x, y);
int cnt = 0;
rep(i, 0, 'z' - 'a') {
if ((ans & (1 << i)))
cnt++;
}
cout << cnt << '\n';
}
}
return 0;
}
| replace | 113 | 114 | 113 | 114 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.