Untitled
unknown
plain_text
3 years ago
1.3 kB
3
Indexable
#include <iostream> #include <vector> using namespace std; int n,q; string s; vector<vector<int>> tree; vector<int> a(26,0); int binpow (int a, int n) { int res = 1; while (n) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; } void build (int v, int tl, int tr) { if (tl == tr) tree[v][s[tl]-'a'] = 1; else { int tm = (tl + tr) / 2; build (v*2, tl, tm); build (v*2+1, tm+1, tr); vector<int> a2(26); for (int i=0;i<26;i++){ a2[i]=tree[v*2][i]+tree[v*2+1][i]; } tree[v] = a2; } } vector<int> get(int v, int l, int r, int ql, int qr) { if (ql >= r || l >= qr) { return a; } if (ql <= l && qr <= r) { return tree[v]; } int m = (l + r) / 2; vector<int> a2 = get(2 * v, l, m, ql, qr); vector<int> a3=get(2 * v + 1, m, r, ql, qr); for (int i=0;i<26;i++){ a2[i]+=a3[i]; } return a2; } int main() { cin>>n>>q; cin>>s; for (int i=0;i<4*n;i++){ tree.push_back(a); } build(1,0,n-1); //for (int i=0;i<4*n;i++){ //for (int j=0;j<26;j++){ //cout<<"vertex "<<i<<" letter "<<j<<" count "<<tree[i][j]<<endl; //} //} int p,l,r,k; for (int i=0;i<q;i++){ cin>>p; if (p==2){ cin>>l>>r; vector<int> t=get(1,l,r,0,n-1); } } return 0; }
Editor is loading...