[C++] | Implementation | Segment tree | Clean and easy thought process
Another approach for Jump Game VII , (Implementation using segment tree)\n---\nSorry for poor english\n---\nWe can implement the process described like this - \
**Another approach for [Jump Game VII](#1871)** , *(Implementation using segment tree)*\n---\n*Sorry for poor english*\n---\nWe can implement the process described like this - \n+ Since we can go from i to anywhere between `[i + minJump, min(n-1,i + maxJump]` so when seen from Right-hand side we see that -\nwe can arrive at an index j from `[max(0, j - maxJump), j - minJump]`.\n+ Note that when the range is not valid, we can just skip this index and assume we cannot arrive here.\n+ Now iterate from `i = 0 to n - 1` and first find the range `[L, R]`\n+ If its permissible just check if we can visit any index from L to R, \n     + If we can, then mark current index as canBeVisited.\n     + Else continue.\n+ At last check if we can visit n-1 or not.\n\n*Hurray!*, hard part of the question is done, now just think how to implement it within time limits. I used segment tree for marking and queries indexes -\n+ When we can visit any index, just update it value in segment tree as 1.\n+ To query if we have any visited vertex in `[L, R]`, just query the sum of range [L, R] , if its greater then zero than we have atleast one index where we can land in `[L, R]`.\n+ So mark current index as visited by `modify(index, 1)`, i.e, setting value of index to 1.\n\nThis was my code, though its without comments(Sorry for that, but I belive the explanation is sufficient and its better to try and implement it yourself)\n```cpp\nconst int NN = 1e5 + 10;  // limit for array size\nint nn;  // array size\nint tt[2 * NN];\n\nvoid modify(int p, int value) {  // set value at position p\n  for (tt[p += nn] = value; p > 1; p >>= 1) tt[p>>1] = tt[p] + tt[p^1];\n}\n\nint query(int l, int r) {  // sum on interval [l, r)\n  int res = 0;\n  for (l += nn, r += nn; l < r; l >>= 1, r >>= 1) {\n    if (l&1) res += tt[l++];\n    if (r&1) res += tt[--r];\n  }\n  return res;\n}\n\nclass Solution {\npublic:\n    bool canReach(string s, int minj, int maxj) {\n        if(s.back() == \'1\') return false;\n        int n = s.length();\n        nn = n;\n        memset(tt, 0, sizeof(tt));\n        modify(0, 1);\n        for(int i = 1 ; i < n ; i++){\n            if(s[i] == \'1\') continue;\n            int l = max(0, i - maxj), r = i - minj;\n            if(r < 0) continue;\n            if(query(l, r + 1)){\n                modify(i, 1);\n            }\n        }\n        return query(n-1, n);\n        \n    }\n};\n```
['Tree']