QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#477235 | #186. Street Lamps | Dimash | 20 | 1226ms | 157140kb | C++20 | 5.5kb | 2024-07-14 00:30:33 | 2024-07-14 00:30:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 20, MOD = (int)1e9+7;
struct bit{
vector<int> t;
int n;
void init(int s){
n = s;
t.assign(s + 1,0);
}
void upd(int pos,int val,int n){
while(pos <= n){
t[pos] += val;
pos += pos & -pos;
}
}
int get(int i){
int ret = 0;
while(i >= 1){
ret += t[i];
i -= i & -i;
}
return ret;
}
int get(int l,int r){
return get(r) - get(l - 1);
}
};
vector<pair<int,int>> qr;
int n,q,a[N];
vector<int> t[N * 4],f[N];
bit b[N * 4];
void build(int v = 1,int tl = 1,int tr = n){
if(tl == tr){
t[v] = f[tl];
t[v].resize(unique(t[v].begin(),t[v].end()) - t[v].begin());
b[v].init((int)t[v].size());
}else{
int tm = (tl + tr) >> 1;
build(v+v,tl,tm);
build(v + v + 1,tm + 1,tr);
t[v].resize((int)t[v+v].size() + (int)t[v +v + 1].size());
merge(t[v + v].begin(),t[v + v].end(),t[v + v + 1].begin(),t[v + v + 1].end(),t[v].begin());
t[v].resize(unique(t[v].begin(),t[v].end()) - t[v].begin());
b[v].init((int)t[v].size());
}
}
void del(set<pair<int,int>> &st,int pos){
auto it = st.upper_bound({pos,(int)1e9});
it--;
auto [l,r] = *it;
st.erase(it);
if(l != pos){
st.insert({l,pos-1});
}
if(r != pos){
st.insert({pos+1,r});
}
}
pair<int,int> ins(set<pair<int,int>> &st,int pos){
if(st.empty()){
st.insert({pos,pos});
return {pos,pos};
}
auto it = st.upper_bound({pos,(int)1e9});
if(it == st.end() || (*it).first != pos + 1){
if(it == st.begin()){
st.insert({pos,pos});
return {pos,pos};
}
it--;
if((*it).second + 1 != pos){
st.insert({pos,pos});
return {pos,pos};
}
int L = (*it).first;
st.insert({(*it).first,pos});
st.erase(it);
return {L,pos};
}
int R = (*it).second;
if(it == st.begin()){
st.erase(it);
st.insert({pos,R});
return {pos,R};
}
it--;
if((*it).second + 1 != pos){
st.insert({pos,R});
return {pos,R};
}
int L = (*it).first;
auto it1 = it;
it1++;
st.erase(it1);
st.erase(it);
st.insert({L,R});
return {L,R};
}
void upd(int l,int r,int x,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return;
if(tl >= l && tr <= r){
int val = r;
if(t[v].empty() || t[v][0] > val) return;
int it = upper_bound(t[v].begin(),t[v].end(),val) - t[v].begin();it--;
b[v].upd(it + 1,x,(int)b[v].n);
}else{
int tm = (tl + tr) >> 1;
upd(l,r,x,v+v,tl,tm);
upd(l,r,x,v+v+1,tm+1,tr);
}
}
int res =0 ;
void get(int l,int r,int v = 1,int tl = 1,int tr = n){
int it = lower_bound(t[v].begin(),t[v].end(),r) - t[v].begin();
res += b[v].get(it + 1,b[v].n);
if(tl == tr) return;
int tm = (tl + tr) >> 1;
get(l,r,v+v,tl,tm);
get(l,r,v+v+1,tm+1,tr);
}
int re[N];
void test(){
cin >> n >> q;
for(int i = 1;i <= n;i++){
char x;
cin >> x;
a[i] = (x - '0');
}
qr.push_back({-1,-1});
for(int i = 1;i <= q;i++)
{
string tp;cin >> tp;
if(tp == "toggle"){
int x;
cin >> x;
qr.push_back({x,-1});
}else{
int l,r;
cin >> l >> r;
r--;
f[l].push_back(r);
qr.push_back({l,r});
}
}
for(int i = 1;i <= n;i++){
sort(f[i].begin(),f[i].end());
}
build();
set<pair<int,int>> o,z;
int lst = 1;
for(int i = 2;i <= n + 1;i++){
if(i == n + 1 || a[i] != a[lst]){
if(a[lst]){
o.insert({lst,i - 1});
}else{
z.insert({lst,i - 1});
}
lst = i;
}
}
for(int i = 1;i <= q;i++){
auto [L,R] = qr[i];
if(R == -1){
int x = L;
if(a[x]){
auto it = o.upper_bound({x,1e9});
it--;
int l = (*it).first,r = (*it).second;
del(o,x);
ins(z,x);
for(int j = i + 1;j <= q;j++){
if(qr[j].second != -1 && qr[j].first >= l && qr[j].first <= x && qr[j].second >= x && qr[j].second <= r){
re[j] += i;
}
}
}else{
del(z,x);
auto [l,r] = ins(o,x);
for(int j = i + 1;j <= q;j++){
if(qr[j].second != -1 && qr[j].first >= l && qr[j].first <= x && qr[j].second >= x && qr[j].second <= r){
re[j] -= i;
}
}
}
a[x] ^= 1;
}else{
res = 0;
// get(L,R);
auto it = o.upper_bound({L,1e9});
if(it != o.begin()){
it--;
if((*it).second >= R){
re[i] += i;
}
}
cout << re[i] << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 4ms
memory: 42724kb
input:
5 7 11011 query 1 2 query 1 2 query 1 6 query 3 4 toggle 3 query 3 4 query 1 6
output:
1 2 0 0 1 2
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 8ms
memory: 43080kb
input:
5 50 01001 query 1 6 toggle 3 toggle 3 toggle 2 toggle 3 toggle 2 toggle 4 query 2 6 query 2 3 query 1 3 query 3 5 toggle 3 query 2 6 query 1 5 query 2 3 query 3 6 toggle 5 toggle 1 toggle 2 toggle 4 query 1 6 query 4 5 toggle 3 query 5 6 toggle 2 query 4 6 toggle 5 toggle 5 toggle 2 query 4 5 query...
output:
0 1 7 0 4 5 0 13 5 0 13 17 10 13 5 5 6 24 0 10 0 5 0 5 6
result:
ok 25 lines
Test #3:
score: 0
Accepted
time: 3ms
memory: 44724kb
input:
10 50 1111010011 toggle 3 toggle 1 toggle 5 toggle 5 toggle 7 query 7 10 query 6 8 query 6 8 toggle 7 toggle 4 toggle 4 toggle 3 toggle 8 toggle 8 toggle 3 query 4 6 query 1 4 toggle 10 query 4 8 query 4 11 toggle 8 toggle 7 query 6 8 toggle 6 query 3 8 toggle 5 toggle 3 toggle 7 toggle 4 query 2 11...
output:
0 2 3 1 1 0 0 5 0 0 0 2 9 0 1 18 30 19 10 1 0 0 0 18
result:
ok 24 lines
Test #4:
score: 0
Accepted
time: 4ms
memory: 44520kb
input:
20 100 01110011111110011110 toggle 16 query 1 13 toggle 2 query 9 21 toggle 8 toggle 7 query 2 5 toggle 18 query 18 21 query 9 11 query 3 5 query 13 20 query 9 10 toggle 4 query 5 21 query 5 19 query 1 15 query 19 21 toggle 16 query 6 21 query 8 21 toggle 14 toggle 19 toggle 1 query 8 11 toggle 13 t...
output:
0 0 3 0 10 11 0 13 0 0 0 0 0 0 5 28 0 34 0 36 0 23 0 0 8 40 0 0 0 0 10 11 5 0 0 0 2 0 0 0 0 38 0 0 37 46
result:
ok 46 lines
Test #5:
score: 0
Accepted
time: 8ms
memory: 41120kb
input:
100 100 1001001110110100110110100110000100100001111010111000011101010010010001101010101001100000111110000010 toggle 43 toggle 13 toggle 19 query 73 101 toggle 65 query 13 59 toggle 1 query 26 101 query 79 83 query 19 101 query 9 94 query 41 91 toggle 55 toggle 87 toggle 37 query 10 31 toggle 26 quer...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 51 lines
Test #6:
score: 0
Accepted
time: 8ms
memory: 44548kb
input:
100 100 0111010000111110011111000001101011010000000000111101011110010111011010010110111110111011101010010010 query 9 10 query 13 14 query 69 70 query 43 44 toggle 82 toggle 92 toggle 6 toggle 63 query 91 92 query 61 62 toggle 70 query 51 52 query 14 15 toggle 16 toggle 2 toggle 40 toggle 98 toggle 5...
output:
0 2 3 0 9 0 0 13 0 24 16 15 17 0 0 41 44 49 50 52 43 56 61 0 64 65 60 0 69 0 76 42 65 80 0 0 60 7 0 52 87 0 96 78 80
result:
ok 45 lines
Test #7:
score: 0
Accepted
time: 4ms
memory: 42484kb
input:
100 100 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 query 1 47 query 42 81 query 18 29 query 31 37 query 4 58 query 46 59 query 32 100 query 12 98 query 13 29 query 40 73 query 68 100 query 28 75 query 55 72 query 53 61 query 2 54 query 25 71 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 100 lines
Test #8:
score: 0
Accepted
time: 4ms
memory: 44908kb
input:
100 100 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 query 11 25 query 64 67 query 3 63 query 14 60 query 49 64 query 45 70 query 7 33 query 32 97 query 21 84 query 57 61 query 9 22 query 43 60 query 14 34 query 69 99 query 32 69 query 11 13 qu...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
result:
ok 100 lines
Subtask #2:
score: 0
Time Limit Exceeded
Test #9:
score: 0
Time Limit Exceeded
input:
100 300000 1100100000000101010010100111010001100010001100111101000010111110001101101110100100100110101010110010 query 13 14 query 42 43 toggle 64 query 78 79 toggle 85 query 35 36 toggle 35 query 4 5 toggle 5 query 4 5 query 42 43 query 35 36 query 13 14 query 14 15 toggle 15 toggle 31 query 20 21 q...
output:
0 0 0 6 0 0 0 7 0 14 0 18 0 0 21 0 26 0 0 36 38 15 41 44 0 47 20 50 52 0 55 52 56 0 35 31 70 73 7 4 0 0 51 83 84 90 44 0 95 97 0 70 0 103 26 8 46 20 109 122 20 109 108 0 0 28 0 135 139 112 35 142 53 146 0 151 0 153 0 0 73 0 164 0 100 0 33 173 135 151 178 180 75 133 189 46 0 197 23 0 200 0 141 206 20...
result:
Subtask #3:
score: 0
Wrong Answer
Test #17:
score: 20
Accepted
time: 6ms
memory: 41432kb
input:
1000 1003 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
0 0 0
result:
ok 3 lines
Test #18:
score: -20
Wrong Answer
time: 9ms
memory: 44740kb
input:
1000 1003 00100001101000000001000001001000100010000010010010001001001010001010101100010001000010101100000001001111000001110000010110100000100110001000000101001110000001110001000100000011001110000011010100101000000010100110100010000000110000111100100000011000100010010100000000100000000010001001110101...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 295th lines differ - expected: '46', found: '0'
Subtask #4:
score: 0
Time Limit Exceeded
Test #30:
score: 20
Accepted
time: 11ms
memory: 44756kb
input:
1000 1003 10111011001010101101100010101100100010100110001000000001001100111110101100110100010001111101101100110111110100011000111101100100000110110010101011101001101110111100010100100000110001010001111101001010100101011111010000001110111110001011010111101100000001001110101110011111000101101100011010...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 991 lines
Test #31:
score: 0
Accepted
time: 3ms
memory: 42808kb
input:
1000 1003 01000000111001001110011100111111110011010010110000100010101101101011100011010100100100110101110101010111011100110100110000001010110001011011011010001001101000111011001000000001001100101100010101011101000000101110111011011101100001011110111011001010011101000110100011000101011101000110001011...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 701 lines
Test #32:
score: 0
Accepted
time: 10ms
memory: 44936kb
input:
1000 1003 00010001110110110000001111110000010011011011100111101001011010000111111110001111010101111011100100101000010111101111100011000111001111011011011101000101100000011101001001111110011000100011000001010001011000000010101001101111101111111101011101101000110010110111010111111001101000000100011100...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 369 lines
Test #33:
score: 0
Accepted
time: 7ms
memory: 44688kb
input:
1000 1003 11010001011001111110010101101010111101111001011101011011000001101100000111001101100001111101011101110011011100101011110001000011000011100100111101101101010001001100101111110000000011101101000101010010111101011011101110111011000100001001110111111001110101001010111111011100010001000010110010...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 19 lines
Test #34:
score: 0
Accepted
time: 1226ms
memory: 157140kb
input:
300000 300000 0101011000000100111010110000000010110110011011010010000110101000100110011001100101101001011010111100100100001011111000001111110110010110111000100111101001011110100000110111110011111010100111011101001110110111011110000010100111111101010010100011101001001000110111111101110000011111100111...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 297089 lines
Test #35:
score: -20
Time Limit Exceeded
input:
300000 300000 0101111001110000000101010111001000111110011100000111001100100011000010100110011010010000100000101000100111100110010000100100000100110100000110001000101111101111111101111111101100010010000100001111100010000000100011100011111000110000101001111001000111100101000100011010111010110100000100...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%