QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#768476 | #8079. Range Periodicity Query | ASnown | WA | 1799ms | 89472kb | C++17 | 6.4kb | 2024-11-21 11:06:12 | 2024-11-21 11:06:13 |
Judging History
answer
#include<bits/stdc++.h>
#define file(F) freopen(#F".in","r",stdin),freopen(#F".out","w",stdout)
#define lowbit(x) ((x)&-(x))
#define ALL(x) (x).begin(),(x).end()
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define popc(x) (__builtin_popcountll((x)))
#define abs(x) ((x)>=0?(x):-(x))
using namespace std;
using ll=long long;
using uint=uint32_t;
template<typename T>
inline bool Max(T &x,T y) { return std::less<T>()(x,y)&&(x=y,true); }
template<typename T>
inline bool Min(T &x,T y) { return std::less<T>()(y,x)&&(x=y,true); }
void Solve();
namespace asnown {
using i64=int64_t;
using u64=uint64_t;
using u128=__uint128_t;
namespace isPrime {
constexpr u64 qpow(u64 a,u64 b,u64 p) {
u64 res=1;
for(;b;b>>=1,a=a*a%p)
if(b&1) res=res*a%p;
return res;
}
constexpr bool checkPrime(u64 p) {
if(p==1) return false;
u64 d=__builtin_ctzll(p-1),s=(p-1)>>d;
for(auto a:{2,3,5,7,11,13,82,373}) {
if(a%p==0) continue;
u64 x=qpow(a,s,p),y=0;
for(int i=0;i<d;i++,x=y) {
y=(u128)x*x%p;
if(y==1&&x!=1&&x!=p-1)
return false;
} if(y!=1) return false;
} return true;
}
}
// gen prime in [L,R)
template<u64 __start=0>
constexpr u64 genPrime(u64 l,u64 r) {
using isPrime::checkPrime;
u64 x=__start^0x113817;
for(char c:__TIME__ __TIMESTAMP__)
x+=c,x^=x<<13,x^=x>>7,x^=x<<17;
while(true) {
x^=x<<13,x^=x>>7,x^=x<<17;
auto y=l+x%(r-l);
if(checkPrime(y)) return y;
} return 0;
}
}
namespace asnown {
using i32=int32_t;
using u32=uint32_t;
using i64=int64_t;
using u64=uint64_t;
using i128=__int128_t;
using u128=__uint128_t;
constexpr i32 inverse(i32 a,i32 m) {
i64 r=1;
while(a>1) { r=r*(m-m/a)%m,a=m%a; }
return r;
}
template<i32 m>
class static_modint {
using mint=static_modint;
public:
constexpr static_modint() : v(0) {}
constexpr static_modint(i32 v) : v(normal(v%m)) {}
constexpr static_modint(u32 v) : v(v%m) {}
constexpr static_modint(i64 v) : v(normal(v%m)) {}
constexpr static_modint(u64 v) : v(v%m) {}
constexpr static_modint(i128 v) : v(normal(v%m)) {}
constexpr static_modint(u128 v) : v(v%m) {}
constexpr u32 normal(i32 v) { if(v>=m) v-=m; return v+((v>>31)&m); }
constexpr u32 val() const { return v; }
constexpr static u32 mod() { return m; }
constexpr explicit operator bool() const { return v; }
constexpr mint inv() const { assert(v!=0); return mint(inverse(v,m)); }
constexpr mint pow(i64 b) const { assert(b>=0);
mint a=*this,res=1; for(;b;b>>=1,a*=a) if(b&1) res*=a; return res; }
constexpr mint operator+() const { return *this; }
constexpr mint operator-() const { return mint()-*this; }
constexpr mint& operator+=(const mint &p) { return v=normal(v+p.v),*this; }
constexpr mint& operator-=(const mint &p) { return v=normal(v-p.v),*this; }
constexpr mint& operator++() { return (*this)+=1; }
constexpr mint& operator--() { return (*this)-=1; }
constexpr mint& operator++(i32) { auto tmp=*this; return ++*this,tmp; }
constexpr mint& operator--(i32) { auto tmp=*this; return --*this,tmp; }
constexpr mint& operator*=(const mint &p) { v=i64(v)*p.v%m; return *this; }
constexpr mint& operator/=(const mint &p) { return *this *= p.inv(); }
constexpr friend mint operator+(const mint &p,const mint &q) { return mint(p)+=q; }
constexpr friend mint operator-(const mint &p,const mint &q) { return mint(p)-=q; }
constexpr friend mint operator*(const mint &p,const mint &q) { return mint(p)*=q; }
constexpr friend mint operator/(const mint &p,const mint &q) { return mint(p)/=q; }
constexpr friend mint operator++(mint &v,i32) { auto tmp=v; v+=1; return tmp; }
constexpr friend mint operator--(mint &v,i32) { auto tmp=v; v-=1; return tmp; }
constexpr friend std::istream& operator>>(std::istream &is,mint &a) { i64 v; is>>v; a=v; return is; }
constexpr friend std::ostream& operator<<(std::ostream &os,const mint &a) { os<<a.val(); return os; }
constexpr bool operator==(const mint& p) const { return val()==p.val(); }
constexpr bool operator!=(const mint& p) const { return val()!=p.val(); }
private:
u32 v;
};
using modint998244353=static_modint<998244353>;
using modint107=static_modint<1000000007>;
};
const int B = asnown::genPrime<>(100,300);
using hint=asnown::static_modint<asnown::genPrime<B>(1e8,1e9)>;
const int N = 5e5+55;
int n,m,Q;
string s; int low;
deque<char> t;
int st[N];
hint pw[N],h[N];
vector<int> c[N],pos[N];
namespace SGT {
const int N = ::N<<2;
#define mid ((L+R)>>1)
#define ls (u<<1)
#define rs (ls|1)
int mn[N];
void build(int u,int L,int R) {
mn[u]=0x3f3f3f3f;
if(L==R) return ;
build(ls,L,mid),build(rs,mid+1,R);
}
void update(int u,int L,int R,int x,int k) {
if(L==R) return void(mn[u]=k);
if(x<=mid) update(ls,L,mid,x,k);
else update(rs,mid+1,R,x,k);
mn[u]=min(mn[ls],mn[rs]);
}
int query(int u,int L,int R,int l,int r) {
if(l<=L&&R<=r) return mn[u];
if(r<=mid) return query(ls,L,mid,l,r);
if(l> mid) return query(rs,mid+1,R,l,r);
return min(query(ls,L,mid,l,r),query(rs,mid+1,R,l,r));
}
#undef mid
#undef ls
#undef rs
}
vector<tuple<int,int,int>> q[N];
int ans[N];
signed main() {
#ifndef ONLINE_JUDGE
#endif
cin.tie(nullptr)->sync_with_stdio(false);
Solve();
}
void Solve() {
cin>>n>>s,s=' '+s;
for(int i=1;i<=n;i++) {
if(isupper(s[i])) t.emplace_back(tolower(s[i]));
else ++low,t.emplace_front(s[i]);
} t.emplace_front(' ');
st[0]=low+1;
pw[0]=1;
for(int i=1;i<=n;i++) {
st[i]=st[i-1]-bool(islower(s[i]));
pw[i]=pw[i-1]*B; h[i]=h[i-1]*B+t[i];
}
for(int i=1;i<=n;i++) {
int l=i,r=n,mid;
while(l<r) {
mid=l+r+1>>1;
if(h[st[mid]+mid-i-1]-pw[mid-i]*h[st[mid]-1]
==h[st[mid]+mid-1]-pw[mid-i]*h[st[mid]+i-1])
l=mid; else r=mid-1;
}
c[l].emplace_back(i);
}
cin>>m;
SGT::build(1,1,m);
for(int i=1;i<=m;i++) {
int x; cin>>x; pos[x].emplace_back(i);
}
cin>>Q;
for(int i=1;i<=Q;i++) {
int k,l,r; cin>>k>>l>>r;
q[k].emplace_back(l,r,i);
}
for(int i=n;i>=1;i--) {
for(auto cc:c[i]) for(auto x:pos[cc])
SGT::update(1,1,m,x,cc);
for(auto [l,r,id]:q[i]) {
ans[id]=SGT::query(1,1,m,l,r);
if(ans[id]>i) ans[id]=-1;
}
}
for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 48864kb
input:
7 AABAAba 9 4 3 2 1 7 5 3 6 1 6 1 4 4 2 1 4 2 1 3 3 3 5 5 4 7 7 8 9
output:
1 1 2 -1 3 6
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 278ms
memory: 65048kb
input:
200000 BAbBbBabBBbbABbbaBbaaabaBBAbBbBAAAAABBaBaAAabBAAbABaaBABAabAAAbabbAaBABAbabbAAAbbbbabBBAbbBaabBAAAbBBBbBbbAbbbBabbBABaBAaAAAbBbaABabBAbAAbBbbAbAbBaabAbBBbaaaaBaBbbABBBaaabBaBABAbBabBbbAABBbaBAbaBAbAAABABAbaabbaAAaBAbAbAbBBbaaaAaBaaABBbBAAaAAAaaABbbaAbAaBbaAaaababbaBbaAAAAAAabbBaAabbbaBBAAaABb...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 61006 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 500000 lines
Test #3:
score: 0
Accepted
time: 352ms
memory: 54392kb
input:
10 baaAaAAaAA 500000 6 8 2 3 1 8 7 3 9 4 1 6 9 4 10 10 4 3 1 7 4 3 9 7 1 2 9 3 3 1 10 8 1 6 4 1 6 10 1 5 1 8 9 9 7 3 6 3 9 1 7 6 7 7 9 10 3 2 4 10 7 3 7 1 5 3 5 1 10 1 3 2 2 4 2 3 4 10 5 2 7 10 5 6 8 9 10 6 9 7 5 4 5 4 4 2 5 8 1 9 1 2 10 8 2 5 6 6 6 4 3 1 2 2 3 5 7 4 5 7 5 8 1 8 9 7 6 3 10 7 5 4 8 8...
output:
5 4 4 2 6 3 3 4 6 3 1 6 4 5 3 5 2 5 4 4 2 5 3 5 5 1 2 5 3 5 4 3 5 6 4 5 4 6 4 6 4 1 5 4 4 3 5 3 3 4 5 5 5 4 1 6 5 5 4 4 2 4 3 5 4 5 1 5 1 1 4 4 5 4 4 3 3 4 2 4 4 4 2 4 6 5 2 5 4 3 4 2 4 5 5 4 6 1 2 6 4 5 6 1 1 3 2 3 1 4 4 3 4 4 4 3 6 5 4 5 5 4 1 2 3 4 5 4 4 4 3 6 4 4 4 2 3 3 3 3 3 6 3 5 3 4 6 3 4 5 ...
result:
ok 500000 lines
Test #4:
score: 0
Accepted
time: 419ms
memory: 61404kb
input:
500 ababbBbBabaaBAbabBbbBBAAABabBbBAAABbaBbBAAbabaBaAAaabAaABBBabababAAbaaAbbAAabAAbBbaabbBbaAAABaAaBbbBbabBAABBaabbAabbBabbbAbABaBAABaBbAaaBABBbBAAbbbBabbABABAaAaAAAbaAabBbBaaaaAAAAAabaBBAAABAbbabAaBAbAaaBBbABbBBbaaAaAaBBbaBbabBbBABbaaBbAaabBABaBBbAAaaBABBAaaABAbbaaAaBaAAbAbbbbbaabBabaBbaabaAbaBaaa...
output:
386 327 309 141 424 175 186 273 45 498 99 262 478 149 424 444 49 267 233 388 359 310 203 81 498 12 97 295 400 351 352 407 310 471 291 479 448 203 267 60 223 458 421 391 5 470 212 253 99 281 167 451 154 86 299 434 370 255 383 207 258 310 487 380 6 368 235 137 334 141 50 128 29 478 448 223 466 345 407...
result:
ok 500000 lines
Test #5:
score: 0
Accepted
time: 318ms
memory: 64348kb
input:
10000 BaBbAAaaaaBAbbbbbaBbaaAbaaaabAaaaAAbabBAbaaBABaaabaAbBBaBBABAbabBAbaaAAaAABABbbbABBaBBaABbbAAbBabaAbaBBaAbabaaAAAabAbAABAabBbBaBaAaAbbBAAABbbabAaABABaBbaAABBbbBAABbbbAaABaAaaABAbbbABAabbaAaaBbbbBaBBbAaaabbaBbaaAbabBabaBaAAAbBAabbBAbabAAbbBBBbBAAaBBbBBAbaaaAbBaaBAAbbaAbbbBAbaAaaAbBBAaBabBaaaBab...
output:
-1 5219 4322 2614 7302 1876 -1 5584 2861 3586 4821 6579 6706 1605 7878 886 9218 293 167 7298 5146 6860 2921 8263 4330 9578 7472 6086 5537 4890 8285 58 9733 -1 3157 262 9533 6943 8285 2837 451 6494 7918 8912 2187 9832 4487 2077 871 210 951 1761 6892 4304 6634 9572 9544 5744 4015 7418 7804 5928 3611 8...
result:
ok 500000 lines
Test #6:
score: 0
Accepted
time: 859ms
memory: 71356kb
input:
100000 aabAbBbaBAaabbbbbaAAABaaabbBaBAAaBabbBAbBbbBbbbaaaABaaBaBbBABBBbabBAABbabbAaaaBBaAAbABaBABAABbBAbBAAAbaBaabbAAABaBAaaaBBbBbaBabAbBBaaabaaaaBbBaAaAbAbbBaABaabBbBaAAaAaaBbbAbbaaBBbbbaAaAabaBaAaaBaAAbbBabBaBAbAaabAbbbAbaAbBbaABABAaBBABAaABBBBABAaBAbbbaBbaAABBaAabaAbaAaabAAAbbbaBBbBaaaaAaaAABbBaa...
output:
35335 42708 80231 -1 52892 27828 25395 21105 26112 55093 16568 16170 -1 73256 -1 82801 58592 52120 48659 -1 -1 -1 92581 -1 67746 9463 50384 69443 71368 -1 62536 83524 71293 88216 83685 45630 5450 969 3140 19286 79236 80564 33058 44088 24142 -1 40385 68116 -1 20399 78247 52636 37514 -1 54565 44272 75...
result:
ok 500000 lines
Test #7:
score: 0
Accepted
time: 901ms
memory: 78040kb
input:
500000 AaAAaaAaaaAaaaaaAaAAAaaaaAAaAAAaaAAAaAaaaaAaAaaaAaAaAAaAAaAaaAaaAaAAAAAAAAAAAAaAaAAAaAaAAAAAaaaAaAAAaAaaaAaaAaaaaaaAaaaaAaAaaAAaAAaaAAAaAaaaaaaaAaAaAaAaaAAaaaAAaAaaAAAaaaaaaaAAaAAaAaaaaaAAaAaAaaAAaaaAAaaaAaAAaaaAaAaaAAAaaAAAaAaaaaaaaaaAaAaAaAAAaaAAAAaAaAAAAAAaAAAaAaaaaaaAAaAaaAAaAAAaaaAaAAaAA...
output:
3 13 3 4 3 3 6 3 6 131 3 3 6 4 33 5 9 3 195 105 77 4 3 3 3 3 3 4 3 3 4 3 4 3 3 3 3 4 3 3 4 4 4 4 4 3 9 3 3 23 33 3 4 3 3 3 3 4 4 3 4 4 4 3 5 1 3 5 3 74 3 23 5 3 3 4 3 3 3 3 3 6 4 3 4 3 4 4 3 4 3 3 4 7 4 3 3 4 3 13 3 4 1 6 3 5 3 3 4 4 20 4 532 4 3 3 3 6 97 4 6 3 3 4 3 4 6 3 3 3 3 3 3 4 7 3 6 4 4 4 3 ...
result:
ok 500000 lines
Test #8:
score: 0
Accepted
time: 928ms
memory: 85604kb
input:
500000 BbBabaaAABbABbaAABaaAabBBABbBBBAbaAbbABAaBbbAAabAaBaabBbaABAbaAbBabbaaaaaaaaBBbbBabaaAAbaAABaAAAaAaAbbbaaAaaAaaABAAAAAbbbABaBBbBAAaAAaBbABbBaaBabaAAaBAABaAaaBBbaBaBaBaaAbBAbAaABbBaaAAAAAabBAABaaAbbBaBAAbBBaBaabBaBBAbAbaaaaAbBbaAbbaAaABBaaAbAaaBABABBaAbaBbAAbaAAbaBAbAAaabBbAaabABAaBBBAbBbbBABa...
output:
-1 125970 -1 -1 -1 435323 -1 425031 252960 236797 -1 -1 -1 334816 -1 -1 319448 234360 344601 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 38745 -1 379427 -1 325294 -1 -1 -1 365248 387079 -1 492283 346128 -1 -1 -1 356064 -1 -1 321398 -1 -1 13515 -1 338767 461122 -1 436442 -1 309126 -1 207537 -1 -1 -1 -1 381656 1079...
result:
ok 500000 lines
Test #9:
score: 0
Accepted
time: 1115ms
memory: 86244kb
input:
500000 cCCBAcaAbbbBAbAAAabaCCcbbaCAacABcaCBCBCBCaacCCBbcBacAaaAABBBaCbcccBcaAcaBCBcccbCcaBAbbCAcCbcacAaAbcCbcCAcaaaBabBCbCCaCbCAcAAbAaCbcCCACbCccCACcCcCbAcAbBaCCAbacBAcaBbAcCBcAcbacCCabCAacbCCbCCCBcacCaCbCacccaCbcBaaCCaACAaaabCAbBcAAAcCaCaBcaccaacAaacbbCacBBBCBaaCBACCAaaccbBaBCacabcBbCACCbaBaCaCbAbb...
output:
-1 -1 373736 135320 -1 -1 -1 -1 -1 -1 -1 -1 106473 295826 386781 382253 -1 -1 211227 -1 -1 435332 -1 487098 -1 -1 -1 322685 387263 -1 366267 299799 -1 -1 -1 63851 301486 426183 -1 -1 -1 158872 299489 -1 158501 -1 -1 -1 421755 -1 -1 -1 -1 -1 -1 -1 379236 -1 -1 162368 -1 20735 -1 379535 408080 43142 -...
result:
ok 500000 lines
Test #10:
score: -100
Wrong Answer
time: 1799ms
memory: 89472kb
input:
500000 CGLmxIQvAprgtdDDuZvZDwKvAyqsptLBKwehlQYMUAGNZYjIBwQJotGzdfdJefPNFsvQmsQMQHDThKCosCRLBfDPBmYrOzoPCOmRFKyCEwmCYZrZpzNeUuHsUBqpXrqKbmoNqsUAIGBCNFeHnXUeGaUAKLXrjtcHVKgdmNavqTnAqAIcqyujjfqPDbrQaYwiqKQNMQMCMjxcxVgHfoMdlIjsRbKBADzljmfNENfFOXjVCcUAmnqgcRKfIqCeQMXcqqTtgDSjYpDrKCbAIvpYqtxDCmniGfURGBNPg...
output:
-1 -1 -1 -1 153175 -1 -1 160471 8265 -1 -1 304616 -1 -1 457941 -1 136029 239352 -1 -1 248379 201699 -1 376599 218943 -1 -1 -1 -1 -1 283494 441809 -1 471567 -1 -1 -1 40751 -1 -1 181033 -1 -1 -1 -1 -1 -1 4025 -1 398460 -1 -1 339034 -1 -1 -1 89916 -1 -1 -1 -1 -1 -1 -1 203747 160541 -1 -1 -1 -1 -1 -1 -1...
result:
wrong answer 3894th lines differ - expected: '-1', found: '184840'