Codeforces 240F TorCoder 线段树
开26个线段树维护数量, 然后就没了。。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1000000007; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m; char s[N]; int cnt[26]; struct SegmentTree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 int sum[N << 2], lazy[N << 2]; inline void pull(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } inline void push(int rt, int l, int r) { if(~lazy[rt]) { int mid = l + r >> 1; sum[rt << 1] = lazy[rt] * (mid - l + 1); sum[rt << 1 | 1] = lazy[rt] * (r - mid); lazy[rt << 1] = lazy[rt]; lazy[rt << 1 | 1] = lazy[rt]; lazy[rt] = -1; } } void build(char c, int l, int r, int rt) { lazy[rt] = -1; if(l == r) { sum[rt] = s[l] == c; return; } int mid = l + r >> 1; build(c, lson); build(c, rson); pull(rt); } void update(int L, int R, int val, int l, int r, int rt) { if(R < l || r < L || R < L) return; if(L <= l && r <= R) { sum[rt] = val * (r - l + 1); lazy[rt] = val; return; } push(rt, l, r); int mid = l + r >> 1; update(L, R, val, lson); update(L, R, val, rson); pull(rt); } int query(int L, int R, int l, int r, int rt) { if(R < l || r < L || R < L) return 0; if(L <= l && r <= R) return sum[rt]; push(rt, l, r); int mid = l + r >> 1; return query(L, R, lson) + query(L, R, rson); } } Tree[26]; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); scanf("%d%d", &n, &m); scanf("%s", s + 1); for(int i = 0; i < 26; i++) Tree[i].build(i + 'a', 1, n, 1); while(m--) { int L, R; scanf("%d%d", &L, &R); for(int i = 0; i < 26; i++) cnt[i] = Tree[i].query(L, R, 1, n, 1); int odd = -1; bool can = true; for(int i = 0; i < 26; i++) { if(cnt[i] & 1) { if(~odd) { can = false; break; } else odd = i; } } if(can) { for(int i = 0; i < 26; i++) Tree[i].update(L, R, 0, 1, n, 1); if(~odd) Tree[odd].update(L + R >> 1, L + R >> 1, 1, 1, n, 1), cnt[odd]--; int be = L, ed = R; for(int i = 0; i < 26; i++) { cnt[i] >>= 1; Tree[i].update(be, be + cnt[i] - 1, 1, 1, n, 1); Tree[i].update(ed - cnt[i] + 1, ed, 1, 1, n, 1); be += cnt[i]; ed -= cnt[i]; } } } for(int i = 1; i <= n; i++) { for(int j = 0; j < 26; j++) { if(Tree[j].query(i, i, 1, n, 1)) { s[i] = 'a' + j; break; } } } puts(s + 1); return 0; } /* */