CodeForces 240F TorCoder(线段树)
来源:互联网 发布:余姚数控编程培训 编辑:程序博客网 时间:2024/06/07 18:35
题意:
你给你一个长度为n 的字符串, q 次操作:
每次操作将指定区间内拍成字典序最小的回文串。 如果不可能成回文串, 忽略这次操作。
思路:
大开脑洞一个题, 没想到这题还能线段树搞。(挺巧妙的)
线段树统计一个区间内, 每个字母出现的次数。
那么对于每个区间操作:[x,y]
我们先看看这个区间长度是奇数还是偶数, 如果奇数, 必须只有一个出现次数为奇数的字母。 否则忽略。
如果偶数, 必须所有字母出现次数均为偶数。
然后要字典序最小的话, 直接从a 开始枚举即可,一个一个赋值。
那么问题就转换成了 线段树的区间赋值问题, 查询是查询每个字母出现次数。
吐槽:
这题是要文件读入读出的, VJ上貌似没说, 白白RE test1 两发。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000 + 10;char s[maxn];int cnt[30], n, q;struct node{ int l, r; int setv; int sum[26];}nod[maxn<<2];void pushup(int o){ for (int i = 0; i < 26; ++i){ nod[o].sum[i] = nod[o<<1].sum[i] + nod[o<<1|1].sum[i]; }}void build(int l,int r,int o){ nod[o].l = l; nod[o].r = r; nod[o].setv = -1; if (l == r){ int id = s[l] - 'a'; nod[o].sum[id] = 1; return; } int m = l + r >> 1; build(l, m, o<<1); build(m+1, r, o<<1|1); pushup(o);}void pushdown(int o){ if (nod[o].setv != -1){ int l = nod[o].l; int r = nod[o].r; int m = l + r >> 1; int lson = o << 1; int rson = o << 1 | 1; nod[lson].setv = nod[rson].setv = nod[o].setv; memset(nod[lson].sum, 0, sizeof nod[lson].sum); memset(nod[rson].sum, 0, sizeof nod[rson].sum); nod[lson].sum[nod[o].setv ] = m - l + 1; nod[rson].sum[nod[o].setv ] = r - m; nod[o].setv = -1; }}void update(int L,int R,int c,int l,int r,int o){ if (L > R) return; if (L <= l && r <= R){ nod[o].setv = c; memset(nod[o].sum,0,sizeof nod[o].sum); nod[o].sum[c] = r-l+1; return; } pushdown(o); int m = l + r >> 1; if (m >= L){ update(L, R, c, l, m, o<<1); } if (m < R){ update(L, R, c, m+1, r, o<<1|1); } pushup(o);}void query(int L,int R,int l,int r,int o){ if (L > R) return; if (L <= l && r <= R){ for (int i = 0; i < 26; ++i){ cnt[i] += nod[o].sum[i]; } return; } pushdown(o); int m = l + r >> 1; if (m >= L){ query(L, R, l, m, o<<1); } if (m < R){ query(L, R, m+1, r, o<<1|1); }}void solve(int x,int y){ if (x > y)return; memset(cnt,0,sizeof cnt); query(x, y, 1, n, 1); int odd = 0; int pos; for (int i = 0; i < 26; ++i){ if (cnt[i] & 1) { ++odd; pos = i; } } int len = y - x + 1; if (len & 1){ if (odd != 1) return; int cur = 1; for (int i = 0; i < 26; ++i){ if (!cnt[i]) continue; update(x + cur - 1, x + cur - 1 + cnt[i] / 2 - 1, i, 1, n, 1); update(y - cur + 1 - cnt[i] / 2 + 1, y - cur + 1, i, 1, n, 1);// printf("[%d,%d] -> [%d,%d]\n", x+cur-1, x+cur-1+cnt[i]/2-1, y-cur+1-cnt[i]/2+1,y-cur+1); cur += cnt[i] / 2; } update(x + cur - 1, x + cur - 1, pos, 1, n, 1);// printf("[%d,%d]\n", x+cur-1, x+cur-1); } else { if (odd) return; int cur = 1; for (int i = 0; i < 26; ++i){ if (!cnt[i]) continue; update(x + cur - 1, x + cur - 1 + cnt[i] / 2 - 1, i, 1, n, 1); update(y - cur + 1 - cnt[i] / 2 + 1, y - cur + 1, i, 1, n, 1); cur += cnt[i] / 2; } }}void dfs(int l,int r,int o){ if (l == r){ for (int i = 0; i < 26; ++i){ if (nod[o].sum[i]){ printf("%c", i + 'a'); break; } } return; } if (nod[o].setv != -1){ for (int i = l; i <= r; ++i){ printf("%c", nod[o].setv + 'a'); } return; } int m = l + r >> 1; dfs(l, m ,o<<1); dfs(m+1,r,o<<1|1);}/**7 1aabcbaa5 7**/int main(){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d%d",&n, &q); scanf("%s", s + 1); build(1, n, 1); while(q--){ int x,y; scanf("%d%d",&x, &y); solve(x,y); } dfs(1, n, 1); putchar('\n'); return 0;}
阅读全文
0 0
- CodeForces 240F TorCoder(线段树)
- 【CodeForces】240F - TorCoder 线段树
- Codeforces 240F. TorCoder 线段树
- Codeforces 240F : TorCoder
- CF 240F TorCoder(线段树)
- CF 240F TorCoder(线段树)
- CF 240F TorCoder
- Codeforces Round #145 (Div. 1) 240F TorCoder
- CodeForces 474F Ant colony(线段树)
- codeforces 474F Ant colony(线段树)
- Codeforces Round #397 F Souvenirs(线段树)
- 【Codeforces】452F Permutation hash+线段树
- 【线段树+Hash】Codeforces 452F Permutation
- Codeforces 452F Permutation【线段树】【哈希】
- [线段树+哈希] Codeforces 452F. Permutation
- Codeforces 899F Letters Removing [线段树]
- Codeforces 899F Letters Removin-线段树
- codeforces 474F F. Ant colony(线段树+数论)
- 面试题分享
- 八数码 二 康托哈希
- js数组API和字符串API汇总
- JS的常用对象
- HBase学习之五:HBase的RowKey设计原则
- CodeForces 240F TorCoder(线段树)
- R(N)(勾股定理的变形)
- Thriftpy源代码走读(二)-Thrift文件加载
- Unity发布读取中文数据乱码
- 最基础的Dijkstra的应用
- git常用命令
- 自动控制原理04
- String方法之比较字符串
- 算法十一