题意:给一个仅由小写字母组成的字符串,然后m个操作,每个操作一个区间,要求把区间中排列成字典序最小的回文串,如果不能形成回文串,就忽略该操作。
思路:和上一道线段树优化计数排序的题目很像,几乎是一样的。
同样的,26棵线段树,每种字母对应一棵。
每次统计询问区间中每种字母的个数。
然后先判断是否能形成回文(奇数长度只有一个个数为奇数的,偶数长度不能出现个数为奇数的)
能的话重置区间,然后前后分别覆盖。
注意如果是奇数长度的话,记得先覆盖中间点。
需要注意这道题的输入输出方式不是标准的。。。而是要加文件。。。不然会MLE 1
然而Tle 19….Tle了好久。。。
最后换了种线段树的写法就过了。。。
然而后面这种写法就一定好么。。。。好像也不是。。。
总之是个挺玄学的东西。。。。不管了。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
/* *********************************************** Author :111qqz Created Time :2016年10月05日 星期三 04时00分51秒 File Name :code/cf/problem/240F.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <deque> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <bitset> #include <bits/stdc++.h> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=1E5+1; int tree[26][N*4]; int lazy[26][N*4]; int n,m; char st[N]; int cnt[26]; int L,R; inline bool scan_d(int &num) { char in;bool IsN=false; in=getchar(); if(in==EOF) return false; while(in!='-'&&(in<'0'||in>'9')) in=getchar(); if(in=='-'){ IsN=true;num=0;} else num=in-'0'; while(in=getchar(),in>='0'&&in<='9'){ num*=10,num+=in-'0'; } if(IsN) num=-num; return true; } void PushUp(int rt,int id) { tree[id][rt] = tree[id][rt<<1] + tree[id][rt<<1|1]; } void PushDown(int l,int r,int rt,int id) { if (lazy[id][rt]==-1) return; lazy[id][rt<<1] = lazy[id][rt<<1|1] = lazy[id][rt]; if (lazy[id][rt]) { int m = (l+r)>>1; tree[id][rt<<1] = m-l+1; tree[id][rt<<1|1] = r-m; }else tree[id][rt<<1] = tree[id][rt<<1|1] = 0 ; lazy[id][rt] = -1; } void build(int l,int r,int rt) { if (l==r) { int id = st[l-1]-'a'; tree[id][rt] = 1; lazy[id][rt] = 1; return; } int m = (l+r)>>1; build(lson); build(rson); for ( int i = 0 ; i < 26 ; i++) PushUp(rt,i); } void update(int sc,int l,int r,int rt,int id) { if (r<L||l>R) return; if (L<=l&&r<=R) { lazy[id][rt] = sc; if (sc) tree[id][rt] = r - l + 1; else tree[id][rt] = 0; return; } int m = (l+r)>>1; PushDown(l,r,rt,id); update(sc,lson,id); update(sc,rson,id); PushUp(rt,id); } /* void update(int L,int R,int sc,int l,int r,int rt,int id) { if (L<=l&&r<=R) { lazy[id][rt] = sc; if (sc) tree[id][rt] = r-l+1; else tree[id][rt] = 0; return; } PushDown(l,r,rt,id); int m = (l+r)>>1; if (L<=m) update(L,R,sc,lson,id); if (R>=m+1) update(L,R,sc,rson,id); PushUp(rt,id); } */ int query(int l,int r,int rt,int id) { if (r<L||l>R) return 0; if (L<=l&&r<=R) return tree[id][rt]; int m = (l+r)>>1; PushDown(l,r,rt,id); return query(lson,id) + query(rson,id); } /* intquery(int L,int R,int l,int r,int rt,int id) { if (L<=l&&r<=R) return tree[id][rt]; PushDown(l,r,rt,id); int m = (l+r)>>1; int ret = 0 ; if (L<=m) ret = query(L,R,lson,id); if (R>=m+1) ret = ret + query(L,R,rson,id); return ret; } */ int main() { // freopen("code/in.txt","r",stdin); freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ms(tree,0); ms(lazy,-1); cin>>n>>m; cin>>st; build(1,n,1); while (m--) { int l,r; // scanf("%d%d",&l,&r); scan_d(l);scan_d(r); // cin>>l>>r; int odd = 0; int even = 0; int InMid=-1; for ( int i = 0 ; i < 26 ; i++) { L = l; R = r; cnt[i] = query(1,n,1,i); if (cnt[i]&1==1) odd++,InMid = i; else even++; if (odd>1) break; } int len = r-l+1; if (len%2==0) { if (odd) continue; } else { if (odd>1) continue; } if (odd==1&&even==0) continue; //先判断能否回文,再打上重置标记。 for ( int i = 0 ; i < 26 ; i++) { L = l; R = r; update(0,1,n,1,i); } int fpos = l; int bpos = r; L = (l+r)/2; R = (l+r)/2; if (len%2==1) update(1,1,n,1,InMid); for ( int i = 0 ; i < 26 ; i++) { if (cnt[i]<=1) continue; L = fpos; R = fpos + (cnt[i]>>1)-1; // if (L<l||R>(l+r)/2) return 0; update(1,1,n,1,i); L = bpos - (cnt[i]>>1) + 1; R = bpos; // if (L<=(l+r)/2||R>r) return 0; update(1,1,n,1,i); fpos +=cnt[i]>>1; bpos-=cnt[i]>>1; } } for ( int i = 1 ; i <= n ; i++) for ( int j = 0 ; j < 26 ; j++) { L = i; R = i; if (query(1,n,1,j)) { printf("%c",j+'a'); // cout<<char(j+'a'); break; } } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!