//oj.uz/problem/view/APIO19_street_lamps
// :(
#ifndef LOCAL
#define NDEBUG 1
#endif
#include<vector>
#include<cassert>
#include<set>
#include<string>
#include<algorithm>
#include<cstdio>
struct Input{
auto& operator>>(char& it){
while((it=(char)std::getchar())<=' ') {}
return *this;
}
auto& operator>>(std::string& it){
it.clear();
char ch;
do{
while((ch=(char)std::getchar())>' ') it+=ch;
}while(it.empty());
return *this;
}
template<class T, class=std::enable_if<std::is_integral<T>::value>>
auto& operator>>(T& it){
using std::getchar;
while((it=getchar())<'0') {}
it-='0';
int ch; while((ch=getchar())>='0') it=it*10+ch-'0';
return *this;
}
auto& ignore(std::size_t number){
while(number--) std::getchar();
return *this;
}
} cin;
struct Output{
auto& operator<<(char it) {
std::putchar(it); return *this;
}
auto& operator<<(int it) {
std::printf("%d", it); return *this;
}
auto& operator<<(char const* it) {
for(; *it; ++it) std::putchar(*it);
return *this;
}
} cout;
struct IntIterator{
using iterator_category=std::forward_iterator_tag;
using value_type=int;
using difference_type=ptrdiff_t;
using pointer=const int*;
using reference=const int&;
int stuff;
int operator*()const{return stuff;}
auto& operator++(){++stuff; return *this;}
bool operator==(IntIterator other) const{return stuff==other.stuff;}
bool operator!=(IntIterator other) const{return stuff!=other.stuff;}
};
/*
struct Tree{
std::vector<std::vector<int>> data;
Tree(int number): data(number, std::vector<int>(number)){}
void add(int left1, int right1, int left2, int right2, int value){
for(int i=left1; i<right1; ++i)
for(int j=left2; j<right2; ++j)
data[i][j]+=value;
}
int get(int i, int j) const{
return data[i][j];
}
void build(){
for(auto& it: data)for(auto& it: it) it=0;
}
};
*/
struct Tree{
std::vector<std::vector<int>> data, indices;
Tree(int number): indices(number){}
template<bool real>
void addSuffix(int i, int j, int value){
for(int i1=i; i1<(int)indices.size(); i1|=i1+1)
if(real){
auto const iterator=std::lower_bound(begin(indices[i1]), end(indices[i1]), j);
assert(*iterator==j);
for(int j1=
int(iterator-indices[i1].begin())
; j1<(int)data[i1].size(); j1|=j1+1)
data[i1][j1]+=value;
}else{
indices[i1].push_back(j);
}
}
template<bool real>
void add(int left1, int right1, int left2, int right2, int value){
addSuffix<real>(left1, left2, value);
addSuffix<real>(left1, right2, -value);
addSuffix<real>(right1, left2, -value);
addSuffix<real>(right1, right2, value);
}
int get(int i, int j) const{
int result{};
for(int i1=i; i1>=0; i1=(i1&(i1+1))-1)
for(int j1=
int(std::upper_bound(begin(indices[i1]), end(indices[i1]), j)-indices[i1].begin())-1
; j1>=0; j1=(j1&(j1+1))-1)
result+=data[i1][j1];
return result;
}
void build(){
data.resize(indices.size());
for(int index=0; index<(int)indices.size(); ++index){
std::sort(begin(indices[index]), end(indices[index]));
indices[index].resize(std::unique(begin(indices[index]), end(indices[index]))-indices[index].begin());
data[index].resize(indices[index].size());
}
}
};
int main(){
int number, numQuery; cin>>number>>numQuery;
std::string start; start.reserve(number); cin>>start;
struct Query{char type; int first, sec;};
std::vector<Query> queries(numQuery);
for(auto& [type, first, sec]: queries){
cin>>type;
cin.ignore(5);
cin>>first;
if(type=='q') cin>>sec;
else assert(type=='t');
--first; --sec;
}
Tree tree(number);
auto const process=[&](auto real_){
auto constexpr real=real_();
std::set<int> zeroes(IntIterator{-1}, IntIterator{number+1});
auto const toggle=[&](int index, int time){
assert(time>0);
auto [iterator, inserted]=zeroes.insert(index);
if(inserted){
tree.add<real>(*std::prev(iterator)+1, index+1, index, *std::next(iterator), time);
}else{
iterator=zeroes.erase(iterator);
tree.add<real>(*std::prev(iterator)+1, index+1, index, *iterator, -time);
}
};
for(int index=0; index<number; ++index)
if(start[index]=='1') toggle(index, 1);
for(int time=0; time<numQuery; ++time){
auto const [type, first, sec]=queries[time];
if(type=='q'){
if(real){
int const result=tree.get(first, sec-1);
cout<<(result<0 ? result+time+2: result)<<'\n';
}
}else{
toggle(first, time+2);
}
}
};
process(std::false_type{});
tree.build();
process(std::true_type{});
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
15344 KB |
Output is correct |
2 |
Correct |
404 ms |
17120 KB |
Output is correct |
3 |
Correct |
707 ms |
24096 KB |
Output is correct |
4 |
Correct |
2947 ms |
115320 KB |
Output is correct |
5 |
Correct |
2834 ms |
122664 KB |
Output is correct |
6 |
Correct |
3254 ms |
130544 KB |
Output is correct |
7 |
Correct |
209 ms |
38904 KB |
Output is correct |
8 |
Correct |
2233 ms |
153060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
640 KB |
Output is correct |
2 |
Correct |
8 ms |
640 KB |
Output is correct |
3 |
Correct |
8 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
3597 ms |
115520 KB |
Output is correct |
6 |
Correct |
3191 ms |
120996 KB |
Output is correct |
7 |
Correct |
2743 ms |
121700 KB |
Output is correct |
8 |
Correct |
2289 ms |
153052 KB |
Output is correct |
9 |
Correct |
91 ms |
6904 KB |
Output is correct |
10 |
Correct |
90 ms |
7416 KB |
Output is correct |
11 |
Correct |
98 ms |
7616 KB |
Output is correct |
12 |
Correct |
209 ms |
38904 KB |
Output is correct |
13 |
Correct |
2312 ms |
153108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
8 ms |
640 KB |
Output is correct |
4 |
Correct |
9 ms |
640 KB |
Output is correct |
5 |
Correct |
1346 ms |
84412 KB |
Output is correct |
6 |
Correct |
2286 ms |
104720 KB |
Output is correct |
7 |
Correct |
3220 ms |
130416 KB |
Output is correct |
8 |
Correct |
4418 ms |
145160 KB |
Output is correct |
9 |
Correct |
522 ms |
30548 KB |
Output is correct |
10 |
Correct |
467 ms |
27344 KB |
Output is correct |
11 |
Correct |
514 ms |
31060 KB |
Output is correct |
12 |
Correct |
471 ms |
28128 KB |
Output is correct |
13 |
Correct |
534 ms |
31176 KB |
Output is correct |
14 |
Correct |
466 ms |
28204 KB |
Output is correct |
15 |
Correct |
205 ms |
38908 KB |
Output is correct |
16 |
Correct |
2257 ms |
153180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
313 ms |
15344 KB |
Output is correct |
9 |
Correct |
404 ms |
17120 KB |
Output is correct |
10 |
Correct |
707 ms |
24096 KB |
Output is correct |
11 |
Correct |
2947 ms |
115320 KB |
Output is correct |
12 |
Correct |
2834 ms |
122664 KB |
Output is correct |
13 |
Correct |
3254 ms |
130544 KB |
Output is correct |
14 |
Correct |
209 ms |
38904 KB |
Output is correct |
15 |
Correct |
2233 ms |
153060 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
8 ms |
640 KB |
Output is correct |
18 |
Correct |
8 ms |
640 KB |
Output is correct |
19 |
Correct |
7 ms |
640 KB |
Output is correct |
20 |
Correct |
3597 ms |
115520 KB |
Output is correct |
21 |
Correct |
3191 ms |
120996 KB |
Output is correct |
22 |
Correct |
2743 ms |
121700 KB |
Output is correct |
23 |
Correct |
2289 ms |
153052 KB |
Output is correct |
24 |
Correct |
91 ms |
6904 KB |
Output is correct |
25 |
Correct |
90 ms |
7416 KB |
Output is correct |
26 |
Correct |
98 ms |
7616 KB |
Output is correct |
27 |
Correct |
209 ms |
38904 KB |
Output is correct |
28 |
Correct |
2312 ms |
153108 KB |
Output is correct |
29 |
Correct |
6 ms |
512 KB |
Output is correct |
30 |
Correct |
7 ms |
512 KB |
Output is correct |
31 |
Correct |
8 ms |
640 KB |
Output is correct |
32 |
Correct |
9 ms |
640 KB |
Output is correct |
33 |
Correct |
1346 ms |
84412 KB |
Output is correct |
34 |
Correct |
2286 ms |
104720 KB |
Output is correct |
35 |
Correct |
3220 ms |
130416 KB |
Output is correct |
36 |
Correct |
4418 ms |
145160 KB |
Output is correct |
37 |
Correct |
522 ms |
30548 KB |
Output is correct |
38 |
Correct |
467 ms |
27344 KB |
Output is correct |
39 |
Correct |
514 ms |
31060 KB |
Output is correct |
40 |
Correct |
471 ms |
28128 KB |
Output is correct |
41 |
Correct |
534 ms |
31176 KB |
Output is correct |
42 |
Correct |
466 ms |
28204 KB |
Output is correct |
43 |
Correct |
205 ms |
38908 KB |
Output is correct |
44 |
Correct |
2257 ms |
153180 KB |
Output is correct |
45 |
Correct |
114 ms |
9820 KB |
Output is correct |
46 |
Correct |
199 ms |
11752 KB |
Output is correct |
47 |
Correct |
1276 ms |
49108 KB |
Output is correct |
48 |
Correct |
2882 ms |
118816 KB |
Output is correct |
49 |
Correct |
92 ms |
7960 KB |
Output is correct |
50 |
Correct |
91 ms |
8056 KB |
Output is correct |
51 |
Correct |
99 ms |
8184 KB |
Output is correct |
52 |
Correct |
102 ms |
8440 KB |
Output is correct |
53 |
Correct |
104 ms |
8440 KB |
Output is correct |
54 |
Correct |
103 ms |
8440 KB |
Output is correct |