1 条题解
-
0
C++ :
#pragma GCC optimize ("O2") #define ONLINE_JUDGE #include<bits/stdc++.h> #define HEAP priority_queue #define rep(i, n) for(int i = 0, _end_ = (n); i < _end_; ++i) #define per(i, n) for(int i = (n) - 1; i >= 0 ; --i) #define forn(i, l, r) for(int i = (l), _end_ = (r); i <= _end_; ++i) #define nrof(i, r, l) for(int i = (r), _end_ = (l); i >= _end_; --i) #define FOR(a, b) for(auto (a): (b)) #define mp make_pair #define mt make_tuple #define pb push_back #define X first #define Y second #define eps 1e-6 #define pi 3.1415926535897932384626433832795 #define SZ(x) (int)x.size() #define ALL(x) x.begin(), x.end() #define FILL(a, b) memset((a), (b), sizeof((a))) #define MCPY(a, b) memcpy((a), (b), sizeof((b))) using namespace std; typedef long long LL; typedef double flt; typedef vector<int> vi; typedef vector<LL> vl; typedef pair<int,int> pii; typedef pair<int,LL> pil; typedef pair<LL,int> pli; typedef pair<LL,LL> pll; typedef vector<pil> vil; typedef vector<pii> vii; typedef vector<pli> vli; typedef vector<pll> vll; const int iinf = 1e9 + 7; const int oo = 0x3f3f3f3f; const LL linf = 1ll << 60; const flt dinf = 1e60; template <typename T> inline void scf(T &x) { bool f = 0; x = 0; char c = getchar(); while((c < '0' || c > '9') && c != '-') c = getchar(); if(c == '-') { f = 1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } if(f) x = -x; return; } template <typename T1, typename T2> void scf(T1 &x, T2 &y) { scf(x); return scf(y); } template <typename T1, typename T2, typename T3> void scf(T1 &x, T2 &y, T3 &z) { scf(x); scf(y); return scf(z); } template <typename T1, typename T2, typename T3, typename T4> void scf(T1 &x, T2 &y, T3 &z, T4 &w) { scf(x); scf(y); scf(z); return scf(w); } inline char mygetchar(){ char c = getchar(); while(c == ' ' || c == '\n') c = getchar(); return c; } template <typename T> inline bool chkmax(T &x, const T &y){ return y > x ? x = y, 1 : 0; } template <typename T> inline bool chkmin(T &x, const T &y){ return y < x ? x = y, 1 : 0; } #ifdef ONLINE_JUDGE #define debug(...) ; #else #define debug(...) fprintf(stderr, __VA_ARGS__) #define DEBUG #endif //---------------------------------------------------------head---------------------------------------------------- const int maxn = 1e5 + 100; const int maxb = 10; const int mod = 1e9 + 7; inline void ADD(int &x, const int &y){ x = (x + y < mod) ? (x +y) : (x + y - mod); } inline int SUM(const int &x, const int &y){ return (x + y < mod) ? (x +y) : (x + y - mod); } inline void SUB(int &x, const int &y){ x = (x - y < 0) ? (x - y + mod) : (x - y); } inline int DIF(const int &x, const int &y){ return (x - y < 0) ? (x - y + mod) : (x - y); } int n, q; char str[maxn]; int L[maxn], R[maxn], ans[maxn], ord[maxn], nxt[maxn][maxb], prv[maxn][maxb]; void work(int ln, int rn, int lq, int rq) { if(ln == rn) { forn(i, lq, rq) ans[ord[i]] = 1; return; } if(lq > rq) return; int midn = ln + rn >> 1; static int sum[10], rem[10], f[maxn][10], dp[maxn], F[maxn][10], G[maxn][10], DP[maxn], sm, s[10][10]; memset(sum, 0, sizeof sum); memset(rem, 0, sizeof rem); nrof(i, midn, ln) { rep(j, maxb) f[i][j] = SUM((nxt[i][j] <= midn) ? 0 : 1, sum[j]); if(rem[str[i] - 'a']) rep(j, maxb) SUB(sum[j], f[rem[str[i] - 'a']][j]); rep(j, maxb) ADD(sum[j], f[i][j]); rem[str[i] - 'a'] = i; } memset(sum, 0, sizeof sum); memset(rem, 0, sizeof rem); nrof(i, midn, ln) { if(rem[str[i] - 'a']) rep(j, maxb) SUB(sum[j], f[rem[str[i] - 'a']][j]); rep(j, maxb) { ADD(sum[j], f[i][j]), F[i][j] = sum[j]; if(nxt[i - 1][j] > midn) ADD(F[i][j], 1); } rem[str[i] - 'a'] = i; } memset(s, 0, sizeof s); memset(sum, 0, sizeof sum); forn(i, midn + 1, rn) { rep(j, maxb) G[i][j] = SUM((str[i] - 'a' == j && prv[i][j] <= midn) ? 1 : 0, DIF(sum[j], s[str[i] - 'a'][j])); memcpy(s[str[i] - 'a'], sum, sizeof sum); rep(j, maxb) ADD(sum[j], G[i][j]); } forn(i, midn + 2, rn) rep(j, maxb) ADD(G[i][j], G[i - 1][j]); sm = 0; memset(rem, 0, sizeof rem); nrof(i, midn, ln) { dp[i] = SUM(sm, 1); if(rem[str[i] - 'a']) SUB(sm, dp[rem[str[i] - 'a']]); ADD(sm, dp[i]); rem[str[i] - 'a'] = i; } sm = 0; memset(rem, 0, sizeof rem); nrof(i, midn, ln) { if(rem[str[i] - 'a']) SUB(sm, dp[rem[str[i] - 'a']]); ADD(sm, dp[i]), DP[i] = sm; rem[str[i] - 'a'] = i; } static int qa[maxn], qb[maxn], na, nb; na = nb = 0; forn(_, lq, rq) { int &i = ord[_]; if(L[i] > midn) qb[nb++] = i; else if(R[i] <= midn) qa[na++] = i; else { ans[i] = DP[L[i]]; rep(j, maxb) ADD(ans[i], 1ll * F[L[i]][j] * G[R[i]][j] % mod); } } int foo, bar; rep(i, na) ord[lq + i] = qa[i]; foo = lq + na; rep(i, nb) ord[foo + i] = qb[i]; bar = foo + nb; work(ln, midn, lq, foo - 1); work(midn + 1, rn, foo, bar - 1); return; } int main() { scanf("%s", str + 1); n = strlen(str + 1); scf(q); forn(i, 1, q) scf(L[i], R[i]), ord[i] = i; rep(i, maxb) nxt[n][i] = n; nrof(i, n - 1, 0){ memcpy(nxt[i], nxt[i + 1], sizeof nxt[i]); nxt[i][str[i + 1] - 'a'] = i + 1; } rep(i, maxb) prv[1][i] = 1; forn(i, 2, n){ memcpy(prv[i], prv[i - 1], sizeof prv[i]); prv[i][str[i - 1] - 'a'] = i - 1; } work(1, n, 1, q); forn(i, 1, q) printf("%d\n", ans[i]); return 0; }
- 1
信息
- ID
- 1030
- 时间
- 2000ms
- 内存
- 512MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者