int countRange(vector<long>& sums, int start, int end, int lower, int upper) { if (end - start <= 1) return 0; int mid = start + (end - start) / 2; long count = countRange(sums, start, mid, lower, upper) + countRange(sums, mid, end, lower, upper); int r = mid, l = mid; for (int i = start; i != mid; i++) { while (l < end && sums[l] - sums[i] < lower) l++; while (r < end && sums[r] - sums[i] <= upper) r++; count += r - l; } inplace_merge(sums.begin()+start, sums.begin()+mid, sums.begin()+end); return count; }
void SpriteGroup::read(KConfigGroup &config) { qDebug() << QStringLiteral(" void SpriteGroup::read(KConfigBase &config) :") << config.name(); SpriteRange countRange( config.readEntry( QStringLiteral("Count"), QStringLiteral("1") ) ); mCount = countRange.random(); mRefresh.set( config.readEntry( QStringLiteral("Refresh"), QStringLiteral("1000") ) ); QStringList anims; anims = config.readEntry( QStringLiteral("Animations"), anims ); QStringList::const_iterator lst; for( lst = anims.constBegin(); lst != anims.constEnd(); ++lst ) { KConfigGroup grp( config.config(), *lst ); SpriteDef *obj = new SpriteDef( grp ); mAvailable.append( obj ); } }
int countRangeSum(vector<int>& nums, int lower, int upper) { vector<long> sums(nums.size()+1, 0); for (int i = 0; i != nums.size(); i++) sums[i+1] = sums[i] + nums[i]; return countRange(sums, 0, nums.size()+1, lower, upper); }