# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
932671 | VinhLuu | Street Lamps (APIO19_street_lamps) | C++17 | 135 ms | 38212 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// REPUTATION RATING =
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 3e5 + 5;
//const int oo = 1e9;
const int mod = 1e9 + 7;
int n, q, a[N], L[N], R[N], X[N];
string s, T[N];
namespace sub1{
int m[105][105];
void solve(){
for(int i = 1; i <= n; i ++) m[1][i] = a[i];
for(int t = 1; t <= q; t ++){
string type; cin >> type;
if(type[0] == 't'){
int x; cin >> x;
a[x] = 1 - a[x];
}else{
int l, r, ans = 0; cin >> l >> r;
for(int j = 1; j <= t; j ++){
bool ff = true;
for(int i = l; i < r; i ++) if(m[j][i] == 0){
ff = false;
break;
}
ans += ff;
}
cout << ans << "\n";
}
for(int i = 1;i <= n; i ++) m[t + 1][i] = a[i];
}
}
}
namespace sub2{
bool on[N];
int cnt[N], f[N];
void solve(){
for(int i = 1; i <= n; i ++){
if(a[i] == 1){
f[i] = 1;
on[i] = true;
}else{
on[i] = false;
f[i] = -1;
}
}
for(int t = 1; t <= q; t ++){
string type = T[t];
if(type[0] == 't'){
int x = X[t];
if(on[x]){
cnt[x] += t - f[t] + 1;
}else{
f[x] = t + 1;
}
on[x] = 1 - on[x];
}else{
int l = L[t], r = R[t], ans = 0; cin >> l >> r;
if(on[l]) cout << cnt[l] + t - f[l] + 1 << "\n";
else cout << cnt[l] << "\n";
}
}
}
}
namespace sub3{
bool on[N];
int cnt[N], f[N], st[N << 1];
int get(int l,int r){
r++;
int ret = 1;
for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
if(l & 1) ret = max(ret, st[l ++]);
if(r & 1) ret = max(ret, st[-- r]);
}
return ret;
}
void solve(){
for(int i = 1; i <= n; i ++){
if(a[i] == 1){
f[i] = 1;
on[i] = true;
}else{
on[i] = false;
f[i] = 1e9;
}
}
for(int t = 1; t <= q; t ++){
string type = T[t];
if(type[0] == 't'){
int x = X[t]; //cin >> x;
f[x] = t + 1;
}
}
for(int i = 1; i <= n; i ++) st[i + n - 1] = f[i];
for(int i = n - 1; i >= 1; i --) st[i] = max(st[i << 1], st[i << 1|1]);
for(int t = 1; t <= q; t ++){
string type = T[t];
if(type[0] == 't') continue;
int l = L[t], r = R[t], ans = 0; cin >> l >> r;
int u = get(l, r - 1);
if(u > t) cout << 0 << "\n";
else cout << t - u + 1 << "\n";
}
}
}
#define LOCAL
#ifdef LOCAL
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> q >> s;
for(int i = 1; i <= n; i ++) a[i] = s[i - 1] - '0';
if(n <= 100 && q <= 100)
sub1 :: solve();
else{
bool gg = true;
for(int i = 1; i <= q; i ++) {
cin >> T[i];
if(T[i][0] == 't') cin >> X[i];
else{
cin >> L[i] >> R[i];
if(R[i] - L[i] > 1) gg = false;
}
}
if(gg) sub2 :: solve();
else
sub3 :: solve();
}
}
#endif // LOCAL
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |