牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)

链接:https://ac.nowcoder.com/acm/problem/15706

现在需要您来帮忙维护这个名册,支持下列 4 种操作:

  1. 插入新人名 si,声望为 a
  2. 给定名字前缀 pi 的所有人的声望值变化 di
  3. 查询名字为 sj 村民们的声望值的和(因为会有重名的)
  4. 查询名字前缀为 pj 的声望值的和

题解:一个非常明显的线段树操作,前缀可以看作是区间更新,区间查询,给定名字就是单点更新,单点查询,字典树上dfs序可以得到将树型结构变成一个线性的结构,然后用线段树维护即可,比较好的码力题了

/**

*        ┏┓    ┏┓

  •        ┏┛┗━━━━━━━┛┗━━━┓
  •        ┃       ┃  
  •        ┃   ━    ┃
  •        ┃ >   < ┃
  •        ┃       ┃
  •        ┃... ⌒ ...  ┃
  •        ┃       ┃
  •        ┗━┓   ┏━┛
  •          ┃   ┃ Code is far away from bug with the animal protecting          
  •          ┃   ┃ 神兽保佑,代码无bug
  •          ┃   ┃           
  •          ┃   ┃       
  •          ┃   ┃
  •          ┃   ┃           
  •          ┃   ┗━━━┓
  •          ┃       ┣┓
  •          ┃       ┏┛
  •          ┗┓┓┏━┳┓┏┛
  •           ┃┫┫ ┃┫┫
  •           ┗┻┛ ┗┻┛

    */

    // warm heart, wagging tail,and a smile just for you!

    //

    // ooOoo

    // o8888888o

    // 88" . "88

    // (| -_- |)

    // O\ = /O

    // /---'\____ // .' \| |// .

    // / ||| : |||//

    // / ||||| -:- |||||-

    // | | \ - /// | |

    // | _| ''---/'' | |

    // \ .-_
    - /-. /

    // . .' /--.--\ . . __

    // ."" '< .___\_<|>_/___.' >'"". // | | : - `.;\ _ /;./ - : | |

    // \ \ -. \_ __\ /__ _/ .- / /

    // ======-.____-.
    _
    /
    .-____.-'====== // =---='

    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    // 佛祖保佑 永无BUG

include

include

include

include

include

include

include

include

include

include

include

using namespace std;

typedef long long LL;

typedef pair<int, int> pii;

typedef unsigned long long uLL;

define ls rt<<1

define rs rt<<1|1

define lson l,mid,rt<<1

define rson mid+1,r,rt<<1|1

define bug printf("*********\n")

define FIN freopen("input.txt","r",stdin);

define FON freopen("output.txt","w+",stdout);

define IO ios::sync_with_stdio(false),cin.tie(0)

define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"

define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"

define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"

const int maxn = 3e5 + 5;

const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;

const double Pi = acos(-1);

LL gcd(LL a, LL b) {

return b ? gcd(b, a % b) : a;

}

LL lcm(LL a, LL b) {

return a / gcd(a, b) * b;

}

double dpow(double a, LL b) {

double ans = 1.0;

while(b) {

if(b % 2)ans = ans * a;

a = a * a;

b /= 2;

} return ans;

}

LL quick_pow(LL x, LL y) {

LL ans = 1;

while(y) {

if(y & 1) {

ans = ans * x % mod;

} x = x * x % mod;

y >>= 1;

} return ans;

}

struct Trie {

int nxt[maxn][26], tot;

int in[maxn], out[maxn], time_tag;

void init() {

memset(nxt, 0, sizeof(nxt));

tot = 1;

time_tag = 0;

}

void insert(string str) {

int p = 1;

int len = str.length();

for(int i = 0; i < len; i++) {

int ch = str[i] - 'a';

if(!nxt[p][ch]) {

nxt[p][ch] = ++tot;

}

p = nxt[p][ch];

}

}

int query(string str) {

int p = 1;

int len = str.length();

for(int i = 0; i < len; i++) {

int ch = str[i] - 'a';

if(!nxt[p][ch]) {

return 0;

}

p = nxt[p][ch];

}

return p;

}

void dfs(int u) {

in[u] = ++time_tag;

for(int i = 0; i < 26; i++) {

if(nxt[u][i]) {

dfs(nxt[u][i]);

}

}

out[u] = time_tag;

}

} trie;

LL sum[maxn << 2];

int lazy[maxn << 2];

int cnt[maxn << 2];

void push_up(int rt) {

sum[rt] = sum[ls] + sum[rs];

cnt[rt] = cnt[ls] + cnt[rs];

}

void build(int l, int r, int rt) {

sum[rt] = lazy[rt] = 0;

if(l == r) return;

int mid = (l + r) >> 1;

build(lson);

build(rson);

}

void push_down(int rt, int len) {

if(lazy[rt]) {

lazy[ls] += lazy[rt];

lazy[rs] += lazy[rt];

  1. sum[ls] += lazy[rt] * cnt[ls];
  2. sum[rs] += lazy[rt] * cnt[rs];
  3. lazy[rt] = 0;
  4. }

}

void update_pos(int pos, int val, int l, int r, int rt) {

if(l == r) {

sum[rt] += val;

cnt[rt]++;

return;

}

int mid = (l + r) >> 1;

push_down(rt, r - l + 1);

if(pos <= mid) update_pos(pos, val, lson);

else update_pos(pos, val, rson);

push_up(rt);

}

void update(int L, int R, int val, int l, int r, int rt) {

if(L <= l && r <= R) {

sum[rt] += cnt[rt] * val;

lazy[rt] += val;

return;

}

int mid = (l + r) >> 1;

push_down(rt, r - l + 1);

if(L <= mid) update(L, R, val, lson);

if(R > mid) update(L, R, val, rson);

push_up(rt);

}

LL query(int L, int R, int l, int r, int rt) {

if(L <= l && r <= R) {

return sum[rt];

}

int mid = (l + r) >> 1;

push_down(rt, r - l + 1);

LL ans = 0;

if(L <= mid) ans += query(L, R, lson);

if(R > mid) ans += query(L, R, rson);

return ans;

}

int op[maxn];

char buf[maxn];

string str[maxn];

int val[maxn];

int main() {

ifndef ONLINE_JUDGE

  1. FIN

endif

  1. int n;
  2. scanf("%d", &n);
  3. for(int i = 1; i <= n; i++) {
  4. scanf("%d %s", &op[i], buf);
  5. str[i] = buf;
  6. if(op[i] <= 2) {
  7. scanf("%d", &val[i]);
  8. }
  9. }
  10. trie.init();
  11. for(int i = 1; i <= n; i++) {
  12. if(op[i] == 1) {
  13. trie.insert(str[i]);
  14. // cout << str[i] << endl;
  15. }
  16. }
  17. trie.dfs(1);
  18. // bug;
  19. int tot = trie.tot;
  20. // debug1(tot);
  21. build(1, tot, 1);
  22. for(int i = 1; i <= n; i++) {
  23. if(op[i] == 1) {
  24. int u = trie.query(str[i]);
  25. int pos = trie.in[u];
  26. // debug1(pos);
  27. update_pos(pos, val[i], 1, tot, 1);
  28. // bug;
  29. } else if(op[i] == 2) {
  30. int u = trie.query(str[i]);
  31. if(u) {
  32. int l = trie.in[u];
  33. int r = trie.out[u];
  34. update(l, r, val[i], 1, tot, 1);
  35. }
  36. } else if(op[i] == 3) {
  37. int u = trie.query(str[i]);
  38. if(u) {
  39. int l = trie.in[u];
  40. int r = trie.in[u];
  41. LL ans = query(l, r, 1, tot, 1);
  42. printf("%lld\n", ans);
  43. } else {
  44. printf("0\n");
  45. }
  46. } else if(op[i] == 4) {
  47. int u = trie.query(str[i]);
  48. if(u) {
  49. int l = trie.in[u];
  50. int r = trie.out[u];
  51. LL ans = query(l, r, 1, tot, 1);
  52. printf("%lld\n", ans);
  53. } else {
  54. printf("0\n");
  55. }
  56. }
  57. }
  58. return 0;

}

牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)的更多相关文章

  1. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  2. 牛客~~wannafly挑战赛19~A 队列

    链接:https://www.nowcoder.com/acm/contest/131/A来源:牛客网 题目描述 ZZT 创造了一个队列 Q.这个队列包含了 N 个元素,队列中的第 i 个元素用 Qi ...

  3. 牛客Wannafly挑战赛23 B.游戏

    游戏 题目描述 小N和小O在玩游戏.他们面前放了n堆石子,第i堆石子一开始有ci颗石头.他们轮流从某堆石子中取石子,不能不取.最后无法操作的人就输了这个游戏.但他们觉得这样玩太无聊了,更新了一下规则. ...

  4. 牛客练习赛 29 E 位运算?位运算!(线段树)

    题目链接  牛客练习赛29E 对$20$位分别建立线段树.首先$1$和$2$可以合起来搞(左移右移其实是等效的) 用个lazy标记下.转移的时候加个中间变量. $3$和$4$其实就是区间$01$覆盖操 ...

  5. 牛客小白月赛16 H 小阳的贝壳 (差分+线段树)

    链接:https://ac.nowcoder.com/acm/contest/949/H来源:牛客网 题目描述 小阳手中一共有 n 个贝壳,每个贝壳都有颜色,且初始第 i 个贝壳的颜色为 colico ...

  6. 牛客小白月赛12 H 华华和月月种树 (离线dfs序+线段树)

    链接:https://ac.nowcoder.com/acm/contest/392/H 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...

  7. HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)

    描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...

  8. 牛客 Wannafly 挑战赛26D 禁书目录 排列组合 概率期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/9781060.html 题目传送门 - NowCoder Wannafly 26D 题意 放一放这一题原先的题面 ...

  9. 【牛客Wannafly挑战赛12】 题解

    传送门:https://www.nowcoder.com/acm/contest/79#question 说是比赛题解,其实我只会前三题: 后面的一定补 T1 题意,在一个长度为n的时间内,问如何选择 ...

随机推荐

  1. 集合--Set&&HashSet和TreeSet

    特点:元素无序,不可重复 1.添加元素 set.add("tanlei"); set.addAll(Arrays.asList(44,"磊","磊&q ...

  2. python 捕获异常

  3. Python中的生产者消费者模型

    ---恢复内容开始--- 了解知识点: 1.守护进程: ·什么是守护进程: 守护进程其实就是一个‘子进程’,守护即伴随,守护进程会伴随主进程的代码运行完毕后而死掉 ·为何用守护进程: 当该子进程内的代 ...

  4. ros 工作空间下cpp文件调用其他cpp文件的函数或变量

    最近在学习ros节点编程,在工作空间下添加如下文件: message.h #ifndef MESSAGE_H #define MESSAGE_H extern int n; void init_ros ...

  5. 开发者说:Sentinel 流控功能在 SpringMVC/SpringBoot 上的实践

    从用户的视角来感受一个开源项目的成长,是我们推出「开发者说」专栏的初衷,即在开发者进行开源项目选型时,提供更为立体的项目信息.专栏所有内容均来自作者原创/投稿,本文是「开发者说」的第6篇,作者 Jas ...

  6. 8.5打包libgdx为一个桌面程序(jar包)

    简陋的地图编辑终于做好了,于是要开始制作地图了,想导出为一个windows下可用的程序,让熟人代做地图,然后找人问了下打包流程,其实跟普通java打包为jar没什么区别,记录如下: 导出类型选第三个 ...

  7. Save and Load from XML

    using UnityEngine; using System.Collections; using System.Xml; using System.Xml.Serialization; using ...

  8. @codeforces - 631E@ Product Sum

    目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 给定一个序列 a,定义它的权值 \(c = \sum_{i=1}^{ ...

  9. H3C 轮询DCC和共享DCC

  10. web与原生交互+活动

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...