Concerns about sexual and bowel health and how they relate are very common, and in order for you to have your particular issues addressed, it will be important for you to schedule an appointment to see your gynecologist in order for him or her to obtain more history, perform an exam, and run any tests that may be indicated in your case.
Having said that, the cause of your bowel changes following sex is likely related to the proximity of the genitourinary and gastrointestinal tracts as well as the autonomic nervous system and how it relates to these entities. To address the first of these, the vagina is anatomically very close to the large intestine, rectum, and anus. During penetration of the vagina during sex, contact with the walls of the vagina can also have an impact on the nervous system that controls the GI tract (the enteric nervous system), which can lead to what is a called a "reactive ileus." What this means is that the enteric nervous system causes the bowel wall to stop contracting briefly, which leads to constipation. This is only a very brief occurrence in most cases, which could explain why you feel constipated at first and then experience diarrhea
following its resolution, when the bowel becomes active again.
The other explanation for your constipation following diarrhea is that during sex the sympathetic nervous system is activated. This system is what regulates our "fight or flight" response and is activated during exercise, such as when you are having sex. This nervous system also inhibits bowel contraction and leads to transient constipation. When you rest following sex, the parasympathetic nervous system becomes active, which is involved in our "rest and digest" response, and your GI system becomes active again, possibly leading to a reactive diarrhea. In either case, this is generally not a dangerous occurrence.
Again, in order to address your particular concerns about your sexual and bowel health, I recommend making an appointment to speak with your gynecologist.