标签:class 1.0 支持 插入 代码 字符串 输入 eps lazy
第一行为两个整数 0 ≤ N ≤ 10
5
,表示接下来有 N 个操作;
接下来 N 行,每行输入一个操作,行首为一个整数 1 ≤ o
i
≤ 4,表示这一行的操作的种类,
对于每一次询问操作,在一行里面输出答案。
20 1 a -10 1 abcba -9 1 abcbacd 5 4 a 2 a 9 3 aadaa 3 abcbacd 4 a 3 a 2 a 10 3 a 2 a -2 2 d -8 1 ab -2 2 ab -7 1 aadaa -3 4 a 3 abcba 4 a 4 c
-14 0 14 13 -1 9 11 1 11 0
题意 : 直接看题干就行了,是一个带前缀修改的字典树
思路分析 : 正常的字典树,加一个前缀修改的懒标记就可以了
代码示例 :
#define ll long long const ll maxn = 1e6+5; const ll mod = 1e9+7; const double eps = 1e-9; const double pi = acos(-1.0); const ll inf = 0x3f3f3f3f; char s[maxn]; ll n, d; ll ch[3*maxn][26]; ll sz = 1; ll val[3*maxn]; ll lazy[3*maxn]; ll cnt[3*maxn]; void pushdown(ll k){ for(ll i = 0; i < 26; i++){ if (ch[k][i] != 0){ ll u = ch[k][i]; lazy[u] += lazy[k]; val[u] += cnt[u]*lazy[k]; } } lazy[k] = 0; } void insert(){ ll u = 0, c; ll last; for(ll i = 0; i < strlen(s); i++){ c = s[i]-‘a‘; if (lazy[u]) pushdown(u); if (!ch[u][c]) ch[u][c] = sz++; u = ch[u][c]; val[u] += d; cnt[u]++; } } void update(){ ll u = 0, c; ll last; for(ll i = 0; i <strlen(s); i++){ c = s[i]-‘a‘; last = u; if (!ch[u][c]) return; u = ch[u][c]; } ll num = cnt[u]; u = 0; for(ll i = 0; i <strlen(s); i++){ c = s[i]-‘a‘; if (lazy[u] != 0) pushdown(u); u = ch[u][c]; val[u] += d*num; } lazy[u] += d; } ll query3(ll k){ ll sum = 0; for(ll i = 0; i < 26; i++){ if (ch[k][i] != 0){ if (lazy[ch[k][i]] != 0) pushdown(ch[k][i]); sum += val[ch[k][i]]; } } return sum; } ll query1(){ ll u = 0, c; for(ll i = 0; i < strlen(s); i++){ c = s[i]-‘a‘; if (lazy[u] != 0) pushdown(u); if (!ch[u][c]) return 0; u = ch[u][c]; } if (lazy[u]) pushdown(u); ll sum = val[u]-query3(u); return sum; } ll query2(){ ll u = 0, c; for(ll i = 0; i < strlen(s); i++){ c = s[i]-‘a‘; if (lazy[u] != 0) pushdown(u); if (!ch[u][c]) return 0; u = ch[u][c]; } return val[u]; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); ll t; cin >> t; while(t--){ scanf("%lld%s", &n, s); if (n == 1){ scanf("%lld", &d); insert(); } else if (n == 2) {scanf("%lld", &d); update();} else if (n == 3) printf("%lld\n", query1()); else printf("%lld\n", query2()); } return 0; }
标签:class 1.0 支持 插入 代码 字符串 输入 eps lazy
原文地址:https://www.cnblogs.com/ccut-ry/p/8926161.html