so what do I have to change in my AEG to get a better accuracy on "long range" targets ?
Well, if you want to have longer reach you can replace your existing spring with a stronger one (which may also require bigger batteries). And then, you would also need a longer and tighter inner barrel (which will also require a new cylinder whose length is proportionate to your new barrel's length).
Then there's the issue of compression. Depending on what brand your M4 RIS is -- you may need ported piston heads, air seal nozzle, a more snug O-ring, spring guides with bearing to relieve the twisting on springs during compressions, type-1 or -2 cylinder, etc., etc.
That's just what I can come up right now. Most good brands have these sorted out already and you may only need a stronger spring, but really depends more on what's inside your gear box.
For example, you can't put in a stronger spring in there unless you have metal bushings already.
My advise is let someone who knows about this stuff have a look at your AEG.
if you have a metal body on your M4, a good one-piece hop unit, hard hop rubber and a tightbore barrel will help you achieve better accuracy at the far end.
The quality of the bb's you use also have a big effect. A perfect sphere will be more stable than a slightly-off shape. Since I switched from Excell to Tokyo Marui or Blaster bb's I've seen a significant improvement in accuracy. With Excell bb's I would get weird stray shots almost 1 in 3 (this aside from bb's breaking in half inside the magazines and jams). Now I rarely get those unexplainable stray shots, hop jams or magazine jams have yet to happen.