ba4dea3538 2012-01-22 kinaba: //------------------------------------------------------------- ba4dea3538 2012-01-22 kinaba: // Segment tree ba4dea3538 2012-01-22 kinaba: // in some general form ba4dea3538 2012-01-22 kinaba: // ba4dea3538 2012-01-22 kinaba: // Verified by ba4dea3538 2012-01-22 kinaba: // - Codeforces 104 E ba4dea3538 2012-01-22 kinaba: //------------------------------------------------------------- ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: class SegmentTree ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: // --- DATA FOR EACH NODE --- ba4dea3538 2012-01-22 kinaba: struct Node ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: int sum; ba4dea3538 2012-01-22 kinaba: int maxLeft; ba4dea3538 2012-01-22 kinaba: int minLeft; ba4dea3538 2012-01-22 kinaba: int n4; ba4dea3538 2012-01-22 kinaba: int n7; ba4dea3538 2012-01-22 kinaba: bool switched; ba4dea3538 2012-01-22 kinaba: }; ba4dea3538 2012-01-22 kinaba: // --- DATA FOR EACH NODE --- ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: Node Canonical(int lv, int idx) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: Node& x = tree[lv][idx]; ba4dea3538 2012-01-22 kinaba: // --- DO SOMETHING IF YOU NEED CANONICALIZATION --- ba4dea3538 2012-01-22 kinaba: if( !x.switched ) ba4dea3538 2012-01-22 kinaba: return x; ba4dea3538 2012-01-22 kinaba: Node n = {-x.sum, -x.minLeft, -x.maxLeft, x.n7, x.n4, false}; ba4dea3538 2012-01-22 kinaba: return n; ba4dea3538 2012-01-22 kinaba: // --- DO SOMETHING IF YOU NEED CANONICALIZATION --- ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: void UpdateMidNode(int lv, int idx) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: Node l = Canonical(lv-1, idx*2); ba4dea3538 2012-01-22 kinaba: Node r = Canonical(lv-1, idx*2+1); ba4dea3538 2012-01-22 kinaba: // --- BOTTOM UP COMPUTATION --- ba4dea3538 2012-01-22 kinaba: Node me = { ba4dea3538 2012-01-22 kinaba: l.sum + r.sum, ba4dea3538 2012-01-22 kinaba: max(l.maxLeft, l.sum+r.maxLeft), ba4dea3538 2012-01-22 kinaba: min(l.minLeft, l.sum+r.minLeft), ba4dea3538 2012-01-22 kinaba: l.n4 + r.n4, ba4dea3538 2012-01-22 kinaba: l.n7 + r.n7, ba4dea3538 2012-01-22 kinaba: false ba4dea3538 2012-01-22 kinaba: }; ba4dea3538 2012-01-22 kinaba: // --- BOTTOM UP COMPUTATION --- ba4dea3538 2012-01-22 kinaba: tree[lv][idx] = me; ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: vector< vector<Node> > tree; ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: public: ba4dea3538 2012-01-22 kinaba: SegmentTree(const string& s) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: int N = 1; ba4dea3538 2012-01-22 kinaba: while( N < s.size() ) ba4dea3538 2012-01-22 kinaba: N <<= 1; ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: tree.resize(tree.size()+1); tree.back().resize(N); ba4dea3538 2012-01-22 kinaba: for(int i=0; i<N; ++i) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: // --- INITIALIZE BASE 1-ELEMENT NODES --- ba4dea3538 2012-01-22 kinaba: int v = i<s.size() ? (s[i]=='4' ? +1 : -1) : 0; ba4dea3538 2012-01-22 kinaba: Node me = {v, max(0,v), min(0,v), v==+1, v==-1, false}; ba4dea3538 2012-01-22 kinaba: // --- INITIALIZE BASE 1-ELEMENT NODES --- ba4dea3538 2012-01-22 kinaba: tree.back()[i] = me; ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: while(N>>=1) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: tree.resize(tree.size()+1); tree.back().resize(N); ba4dea3538 2012-01-22 kinaba: for(int i=0; i<N; ++i) ba4dea3538 2012-01-22 kinaba: UpdateMidNode(tree.size()-1, i); ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: int Count() ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: Node x = Canonical(tree.size()-1, 0); ba4dea3538 2012-01-22 kinaba: return x.maxLeft + x.n7; ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: void Switch(int L, int R) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: SwitchRec(tree[0].size(), tree.size()-1, 0, L, R); ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: void SwitchRec(int Stride, int lv, int idx, int Raw_L, int Raw_R) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: // Outside ba4dea3538 2012-01-22 kinaba: if( Raw_R <= Stride*idx || Stride*(idx+1) <= Raw_L ) ba4dea3538 2012-01-22 kinaba: return; ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: // Inside ba4dea3538 2012-01-22 kinaba: bool& me = tree[lv][idx].switched; ba4dea3538 2012-01-22 kinaba: if( Raw_L <= Stride*idx && Stride*(idx+1) <= Raw_R ) ba4dea3538 2012-01-22 kinaba: { ba4dea3538 2012-01-22 kinaba: me ^= 1; ba4dea3538 2012-01-22 kinaba: return; ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: ba4dea3538 2012-01-22 kinaba: // Overlap ba4dea3538 2012-01-22 kinaba: tree[lv-1][idx*2].switched ^= me; ba4dea3538 2012-01-22 kinaba: tree[lv-1][idx*2+1].switched ^= me; ba4dea3538 2012-01-22 kinaba: SwitchRec(Stride/2, lv-1, idx*2, Raw_L, Raw_R); ba4dea3538 2012-01-22 kinaba: SwitchRec(Stride/2, lv-1, idx*2+1, Raw_L, Raw_R); ba4dea3538 2012-01-22 kinaba: UpdateMidNode(lv, idx); ba4dea3538 2012-01-22 kinaba: } ba4dea3538 2012-01-22 kinaba: };