Submission #75817122
Source Code Expand
#include <bits/stdc++.h>
#pragma warning(disable:4996)
#pragma comment(linker,"/STACK:336777216")
#pragma GCC optimize("03,unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll,ll>;
ll POW(ll a,ll b,ll rem) {
ll p=1;
a %= rem;
for (;b;b>>=1,a=(a*a)%rem) {
if (b&1)p = (p*a)%rem;
}
return p;
}
tuple<ll,ll,ll> extended_gcd(ll a,ll b) {
if (a==0) return {b,0,1};
auto [g,x,y] = extended_gcd(b%a,a);
return {g,y-(b/a)*x,x};
}
ll modinverse(ll a ,ll m) {
return (get<1>(extended_gcd(a,m))%m+m)%m;
}
void pre() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void pre2() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
void presolve() {
}
struct Data {
ll val;
ll lcnt,rcnt;
bool connect;
Data operator+(const Data&r)const {
if (connect && r.connect)return {val + r.val,lcnt+r.rcnt,lcnt+r.rcnt,true};
if (connect)return {val + r.val,lcnt+r.lcnt,r.rcnt,false};
if (r.connect)return {val + r.val,lcnt,rcnt+r.rcnt,false};
return {(rcnt+r.lcnt+1)/2 + val + r.val,lcnt,r.rcnt,false};
}
};
template <typename T>
struct Seg {
ll two;
T init;
vector<T> seg;
Seg(int _n) :init(T()){
two=1;
while (two<_n)two<<=1;
seg.assign(two<<1,init);
}
Seg(int _n, T _init) :init(_init){
two=1;
while (two<_n)two<<=1;
seg.assign(two<<1,init);
}
void update(ll g,ll s,ll e,ll t,T val) {
if (e<t||t<s)return;
if (s==e) {
seg[g] = val;
return;
}
ll m = s+e>>1;
update(g<<1,s,m,t,val);update((g<<1)+1,m+1,e,t,val);
seg[g] = seg[g<<1]+seg[(g<<1)+1];
}
T find(ll g,ll s,ll e,ll ts,ll te) {
if (e<ts||te<s)return init;
if (ts<=s&&e<=te) {
return seg[g];
}
ll m = s+e>>1;
return (find(g<<1,s,m,ts,te)+find((g<<1)+1,m+1,e,ts,te));
}
void Update(ll t,T val) {
update(1,1,two,t,val);
}
T Find(ll l,ll r) {
if(l>r)return init;
return find(1,1,two,l,r);
}
};
void solve() {
ll N,Q;
cin>>N>>Q;
string str;
cin>>str;
vl d(N-1);
for (int i=0;i<N-1;i++) {
d[i] = (str[i]==str[i+1]&&str[i]!='X')?1:0;
}
Seg<Data> seg(N-1,{0,0,0,false});
for (int i=0;i<N-1;i++)seg.Update(i+1,{0,d[i],d[i],d[i]==1});
for (int i=0;i<Q;i++) {
ll l,r;
cin>>l>>r;
auto res = seg.Find(l,r-1);
if (res.connect)cout<<res.val+(res.lcnt+1)/2<<"\n";
else cout<<res.val+(res.lcnt+1)/2+(res.rcnt+1)/2<<"\n";
}
}
int main() {
pre();
presolve();
ll t=1;
//cin>>t;
while (t--) solve();
}
Submission Info
| Submission Time |
|
| Task |
D - Coloring |
| User |
kuming19 |
| Language |
C++23 (GCC 15.2.0) |
| Score |
100 |
| Code Size |
2948 Byte |
| Status |
AC |
| Exec Time |
245 ms |
| Memory |
38924 KiB |
Compile Error
./Main.cpp:2: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
2 | #pragma warning(disable:4996)
./Main.cpp:3: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
3 | #pragma comment(linker,"/STACK:336777216")
./Main.cpp: In instantiation of 'void Seg<T>::update(ll, ll, ll, ll, T) [with T = Data; ll = long long int]':
./Main.cpp:91:9: required from 'void Seg<T>::Update(ll, T) [with T = Data; ll = long long int]'
91 | update(1,1,two,t,val);
| ^~~~~~
./Main.cpp:110:38: required from here
110 | for (int i=0;i<N-1;i++)seg.Update(i+1,{0,d[i],d[i],d[i]==1});
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
./Main.cpp:78:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | ll m = s+e>>1;
| ~^~
./Main.cpp: In instantiation of 'T Seg<T>::find(ll, ll, ll, ll, ll) [with T = Data; ll = long long int]':
./Main.cpp:95:16: required from 'T Seg<T>::Find(ll, ll) [with T = Data; ll = long long int]'
95 | return find(1,1,two,l,r);
| ^~~~
./Main.cpp:115:28: required from here
115 | auto res = seg.Find(l,r-1);
| ~~~~~~~~^~~~~~~
./Main.cpp:87:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | ll m = s+e>>1;
| ~^~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
100 / 100 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00-sample-001.txt |
| All |
00-sample-001.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt, 01-043.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00-sample-001.txt |
AC |
1 ms |
3404 KiB |
| 01-001.txt |
AC |
243 ms |
38840 KiB |
| 01-002.txt |
AC |
244 ms |
38772 KiB |
| 01-003.txt |
AC |
240 ms |
38848 KiB |
| 01-004.txt |
AC |
236 ms |
38748 KiB |
| 01-005.txt |
AC |
235 ms |
38828 KiB |
| 01-006.txt |
AC |
239 ms |
38808 KiB |
| 01-007.txt |
AC |
240 ms |
38848 KiB |
| 01-008.txt |
AC |
240 ms |
38776 KiB |
| 01-009.txt |
AC |
239 ms |
38748 KiB |
| 01-010.txt |
AC |
244 ms |
38716 KiB |
| 01-011.txt |
AC |
236 ms |
38804 KiB |
| 01-012.txt |
AC |
236 ms |
38788 KiB |
| 01-013.txt |
AC |
236 ms |
38844 KiB |
| 01-014.txt |
AC |
239 ms |
38828 KiB |
| 01-015.txt |
AC |
240 ms |
38804 KiB |
| 01-016.txt |
AC |
228 ms |
38764 KiB |
| 01-017.txt |
AC |
228 ms |
38840 KiB |
| 01-018.txt |
AC |
229 ms |
38920 KiB |
| 01-019.txt |
AC |
231 ms |
38796 KiB |
| 01-020.txt |
AC |
202 ms |
38924 KiB |
| 01-021.txt |
AC |
204 ms |
38736 KiB |
| 01-022.txt |
AC |
208 ms |
38816 KiB |
| 01-023.txt |
AC |
203 ms |
38760 KiB |
| 01-024.txt |
AC |
210 ms |
38712 KiB |
| 01-025.txt |
AC |
245 ms |
38844 KiB |
| 01-026.txt |
AC |
239 ms |
38744 KiB |
| 01-027.txt |
AC |
233 ms |
38740 KiB |
| 01-028.txt |
AC |
227 ms |
38772 KiB |
| 01-029.txt |
AC |
231 ms |
38848 KiB |
| 01-030.txt |
AC |
236 ms |
38712 KiB |
| 01-031.txt |
AC |
228 ms |
38696 KiB |
| 01-032.txt |
AC |
238 ms |
38804 KiB |
| 01-033.txt |
AC |
240 ms |
38804 KiB |
| 01-034.txt |
AC |
194 ms |
38804 KiB |
| 01-035.txt |
AC |
195 ms |
38776 KiB |
| 01-036.txt |
AC |
196 ms |
38820 KiB |
| 01-037.txt |
AC |
224 ms |
38920 KiB |
| 01-038.txt |
AC |
1 ms |
3488 KiB |
| 01-039.txt |
AC |
1 ms |
3472 KiB |
| 01-040.txt |
AC |
1 ms |
3512 KiB |
| 01-041.txt |
AC |
1 ms |
3488 KiB |
| 01-042.txt |
AC |
1 ms |
3580 KiB |
| 01-043.txt |
AC |
1 ms |
3476 KiB |