Don't Luke 21:16 and Luke 21:18 seem to contradict each other?
On Luke 21:16, “you will be betrayed” compared to Luke 21:18 “but not a hair of your head will perish”, well, I would liken these two statements to the situation regarding the 144,000 (see the link: The Sealing of the 144,000). During the days of their ministry, the 144,000 will receive miraculous protection that will preserve them despite intense opposition to their activities (just as in the case of Moses and Elijah who direct this worldwide ministry). At the conclusion of their ministries, however, when their work is done, they will be martyred to a man (as will Moses and Elijah). In a similar way, Jesus is giving an overview of the difficult times that will face believers who stay faithful to Him during the Great Persecution of the Tribulation's second half. Some, in fact a great many, will be martyred (verse 16). Indeed, these believers are seen in Revelation 7:9-14, a great multitude from all over the world, and it is no accident that they are introduced directly on the heels of the sealing of the 144,000 (whose experience they parallel).
We all have a role to play in the plan of God, and none of us has the power to determine the specifics of it, just as the spiritual gifts we receive are from Him and not of our own will. For those whose role it is to martyred for Jesus Christ - can there be any greater or finer way of demonstrating love and service to the Master than to die on behalf of the One who died for us? On the other hand, there must be believers still present on the earth to whom He returns - these are they who rise in resurrection to meet Him (1Thes.4:15-17) - for this is an important part of the fulfillment of "the blessed hope" (Tit.2:13), that is, the resurrection of the living as well as of the dead (1Cor.15:51-55), and a confirmation of the validity of our continued waiting for Him to return (1Cor.1:7; 1Thes.1:10; cf. 1Cor.16:22 [Marana Tha = "O Lord, return!"]; 1Thes.2:19; Jas.5:8; 1Pet.1:13; 2Pet.1:19-20).
It is to this second group that verse 18 is addressed. For all those who have been spared martyrdom, the flip-side is that they will have to endure the entire Tribulation, drinking that cup to the bitter end. But our Lord gives them (and us, if it so be our lot) the encouragement of knowing that we will be brought safe through the trial despite all visual evidence to the contrary (without losing a hair on our heads). I take from this that those of us who will indeed be led into the Great Tribulation can take comfort from and have confidence in the fact that we will either 1) die for the Lord at just the right time and in just the right way, or 2) be delivered, "calling upon Him" and being resurrected at the decisive moment. Because of Jesus' words here, we can, therefore, have even more confidence than would otherwise be the case of a perfect outcome to events, for either we will be spared enduring the worst of the Tribulation and have the honor to lose our lives for the One we love, or we will be spared our lives and never know death, being resurrected alive at the Tribulation's end. Revelation addresses this issue from both points of view; first, from the point of view of the martyrs who, as your question points out, are not to fall into the category of those for whom "not a hair of your head will be lost", at least ultimately:
Blessed are the dead who die in the Lord from henceforth, says the Spirit, that they may rest from their labors, for their works follow with them.
And, secondly, also from the standpoint of the two possibilities put side-by-side, namely of suffering which is to end either in living resurrection or by death in martyrdom, with both of these two possibilities requiring a solidity of faith that demands our focus and concentration today if we are going to be ready for them tomorrow:
If anyone [is marked] for captivity, he is going into captivity. If anyone [is marked] for death by the sword, by the sword [he must] be killed. Therefore endurance and faith [on the part] of [my] sanctified ones is [called for].
Whichever way the Lord has chosen for us, we know that such is the first best destiny for us, and that we can have complete faith that it will work out for the ultimate divine good, for His glory, and for our great reward. All we have to do is to keep faith with Him - and in the meantime take advantage of every opportunity for spiritual growth and preparation. On all of this, please see the extensive Coming Tribulation series (eight parts now available).
In the One who has by His own example showed us the way in all things, our faithful Lord and Savior Jesus Christ.