FZUOJ 2280 Magic
来源:互联网 发布:膜法世家绿豆面膜知乎 编辑:程序博客网 时间:2024/06/03 21:38
看了这位大哥的思路:http://blog.csdn.net/zmh69695328/article/details/76154450
用字典树来处理后缀, 先将字符串排序,然后倒转字符串建树。
排序与倒转都不用直接对字符串操作,排序可以建立下标数组。
用树状数组来维护权值。
对相同字符串的处理上,让他们共享同一个bit数组,查询时按各自的w查询sum。
用字典树来处理后缀, 先将字符串排序,然后倒转字符串建树。
排序与倒转都不用直接对字符串操作,排序可以建立下标数组。
用树状数组来维护权值。
对相同字符串的处理上,让他们共享同一个bit数组,查询时按各自的w查询sum。
#ifdef _DEBUG#pragma warning(disable : 4996)#endif#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <functional>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#define CLEAR(a, b) memset(a, b, sizeof(a))#define CLOSE() ios::sync_with_stdio(false)#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)#define PF(a) printf("%d\n", a)#define SF(a) scanf("%d", &a)#define SFF(a, b) scanf("%d%d", &a, &b)#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)#define FOR(i, a, b) for(int i = a; i < b; ++i)#define LL long long#define maxn 1005#define maxm 1000005#define MOD 1000000007#define INF 10000000#define EPS 1e-6#define N 26using namespace std;//-------------------------CHC------------------------------//#define lowbit(x) x & -xchar s[maxn][maxn];int w[maxn], idx[maxn], len[maxn];int bit[maxn][maxn], share[maxn];void update(int idx, int i, int val) {while (i <= 1000) {bit[idx][i] += val;i += lowbit(i);}}int sum(int idx, int i) {int ret = 0;while (i) {ret += bit[idx][i];i -= lowbit(i);}return ret;}struct Node {int end;Node *next[N];Node() {end = 0;FOR(i, 0, N) next[i] = NULL;}};void _insert(int idx, Node *root) {Node *cur = root;for (int i = len[idx] - 1; i >= 0; --i) {int id = s[idx][i] - 'a';if (!cur->next[id]) cur->next[id] = new Node;cur = cur->next[id];if (cur->end) update(share[cur->end], w[idx], 1);}if (!cur->end) cur->end = idx, update(idx, w[idx], 1);share[idx] = cur->end;}void change(int idx, int val, Node *root) {Node *cur = root;for (int i = len[idx] - 1; i >= 0; --i) {int id = s[idx][i] - 'a';cur = cur->next[id];if (cur->end) {update(share[cur->end], w[idx], -1);update(share[cur->end], val, 1);}}w[idx] = val;}bool cmp(int a, int b) {return len[a] < len[b];}int main() {//IN(); OUT();int T; SF(T);while (T--) {CLEAR(bit, 0); CLEAR(share, 0);int n; SF(n);FOR(i, 1, n + 1) scanf("%s %d", s[i], &w[i]), idx[i] = i, len[i] = strlen(s[i]);sort(idx + 1, idx + n + 1, cmp);Node *root = new Node;FOR(i, 1, n + 1) _insert(idx[i], root);int q; SF(q);while (q--) {int a, b, op;SF(op);if (op == 1) {SFF(a, b);change(a, b, root);}else SF(a), PF(sum(share[a], w[a]));}}return 0;}
阅读全文
0 0
- FZUOJ 2280 Magic
- [FZUOJ
- FZUOJ 2214
- FZU 2280 Magic
- 【magic】
- magic
- Magic
- magic
- FZUOJ 1894 志愿者选拔
- fzuoj 2143 Board Game
- fzuoj 2149 Reverse Game
- FZUOJ 2135 数字游戏
- 【FZUOJ 2020】 组合
- FZUOJ 2231 平行四边形数
- FZUOJ-2273 Triangles
- FZUOJ-2275 Game
- FZUOJ 2273 Triangles
- FZUOJ 2282 Wand
- h5+vue video使用
- shell——排序(sort)
- 我的第三篇博客
- selenium与selenium在scrapy中的集成
- 获取JavaScript对象的方法
- FZUOJ 2280 Magic
- Eclipse+flask+virtualenv项目开发环境准备
- 学习bootstrap的day3
- Guardian of Decency UVA
- pom中的build
- Android饼状图的绘制
- vs2012创建dll
- 10种极具创意的验证码设计
- Codeforces Round #428 (Div. 2)-贪心&模拟-B. Game of the Rows