Violent crime rose in Rio de Janeiro in the first four months of this year. Citizens now fear that the city’s flagship program to pacify its favelas is falling apart two months before the Olympics. (Dom Phillips,Jason Aldag/The Washington Post)

The officer stood at the door of the police base beside a basketball court in Prazeres, an impoverished hillside neighborhood. As Saturday afternoon melted into night, music played quietly in the distance.

The court used to echo with the sound of samba school ­rehearsals and children’s games. It had been the cultural hub of the favela, as these improvised communities are called.

Now the basketball court was silent and dark. Nearby, a member of a drug-selling gang patrolled an alley with an automatic rifle. A week earlier, gun battles had raged between police and a drug-selling gang in Prazeres, leaving three men dead.

Just two months before Rio hosts the Olympics, a much-vaunted “pacification” program in the city’s favelas appears to be crumbling, and a wave of violent crime is causing anguish among city residents.

The number of homicides in Rio state was up 15 percent in the first four months of 2016 compared with last year, although the figure dipped in May. Street robbery climbed 24 percent this year, according to the latest statistics, which run through April. And last month, the gang rape of a 16-year-old girl in a Rio favela made headlines in Brazil and around the world.

A police officer patrols during an operation at Rocinha slum in Rio de Janeiro on May 20. A firefight between law enforcement and drug-trafficking gangs raged for hours in Rio’s largest slum, sowing panic among residents. (Felipe Dana/AP)

Authorities insist that the Olympic Games will be safe for visitors, with 85,000 armed soldiers and police officers guarding Rio’s streets.

But even the Rio state security secretary, José Beltrame, acknowledged problems with the growth in crime, which he blamed on a severe recession and a financial crisis in the state government.

“Without any doubt, the situation got worse in the last four months,” he said in an interview.

In 2008, a year before it clinched the Olympics, Rio state launched a “pacification” program in favelas long dominated by violent drug gangs. Police bases were set up in metal shipping containers — 38 favelas have them. Under the plan, the state would provide community policing, and federal and municipal bodies would supply improved transportation, education and other services.

But the Prazeres favela offers a glimpse of how the program has fallen short of its ambitions.

The neighborhood got its police base in 2011. Foreigners began flocking to monthly hip- hop parties here. The community sits just above Santa Teresa, a colorful colonial neighborhood that is a magnet for tourists because of its bars, hostels and five-star hotel. Santa Teresa is within walking distance of the marina where Olympic sailing races will be staged.

But these days, the hip-hop parties in Prazeres are on hold. Two Spanish Olympic sailors and a coach were recently mugged at gunpoint on a Santa Teresa street. A local bar and a pizzeria were held up by armed gangs. When a reporter visited another nearby favela, Fallet, he saw what appeared to be armed gang members guarding a street.

Police bases in favelas are increasingly coming under attack as the drug gangs get ­bolder.

“In the beginning, the community believed the [pacification] project was coming to benefit everyone,” said Eliza Brandão, 55, president of the Prazeres residents association. Residents hoped for better public services, but they never arrived, she said. “People lost confidence in the process,” she said.

Residents say the rise in crime is linked to Brazil’s economic recession, its worst since the 1930s.

“People are desperate. We have a failing economy, nothing for these communities, no opportunities,” said Theresa Williamson, founder of a nonprofit group called Catalytic Communities that works in favelas.

To compound the situation, Rio’s state government is broke and has slashed police budgets by a third. The state relies heavily on tax revenue from offshore oil fields, and its revenue has been decimated by the tumbling global price of oil.

Short on supplies, morale and at times even ammunition, police stationed in Rio favelas such as this one are increasingly confined to their bases, one officer said, speaking on the condition of anonymity because he was not authorized to comment.

“Before they used to do patrols, talking to residents, working out conflicts,” Brandão said. “I think they gave up.”

Beltrame, the state security official, said that he had been forced to cut overtime hours for hundreds of officers.

Budgets for fuel and food have been reduced — at one police station in central Rio, local residents have been donating stationery and toilet paper.

“I have never faced a crisis like this,” said Beltrame, who has been in his job nine years.

A promise of safety

Officials nonetheless pledge that tourists will be safe during the Olympics. More than 300,000 visitors are expected for the Games, which begin Aug. 5.

Beltrame noted that May’s crime statistics showed an improvement. And the state parliament has agreed to provide $14 million in emergency funding to pay outstanding bills for fuel, food and surveillance cameras. “This is going to be our salvation,” Beltrame said.

After hosting a papal visit and a World Cup in recent years, Rio has what it needs to police the Games, Beltrame said. “The Olympics, in our vision, is ready,” he said.

The military will patrol the highways used by athletes, and police will be reinforced on the new bus rapid-transit lanes that link Olympic venues. One lane passes near the favela where the gang rape took place. Police will control entrances to four outlying crime-ridden favelas, including Mare, close to the international airport.

Beltrame defended the pacification project, which has led to a major reduction in violent crime in the city of Rio since 2008. But from 2013 through mid-2015, the most recent time period for which data is available, crime rates in its 38 pacified favelas climbed again.

“The program has problems,” Beltrame said. “But we can’t generalize.”

Some favelas, he said, are seeing more of a crime problem than others — such as Rocinha, close to Rio’s famous Leblon and Ipanema beaches.

This is considered the biggest favela in South America, with 70,000 residents. Yet on a recent afternoon, very few police officers were to be seen. Two were beside a police car at the favela’s entrance. Half a dozen hung around their new base at the top of the favela. One stood farther down the hill, a hand nervously resting on a pistol in his holster.

The drug trade carried on a few hundred yards from him, where a youth sat at the end of a twisting alley with a crackling radio and a mirror to spot an impending attack.

It wasn’t like that when the Brazilian flag was raised here in 2011, after a military operation took control of the favela without a shot being fired.

“The impression was that they had taken the territory,” said Cleber Araujo, 36, who works for a community-run ­media organization. A library was built. A park was opened.

But the goodwill won by the authorities soon evaporated as Brazilian police — often known for their brutality — settled in. The high-profile torture and killing of a local man by officers from the police base here in 2013 sparked street protests. Policing visibly diminished.

“Today you see alleys taken over by bandits,” Araujo said. “Everyone can see the government’s makeup has washed off.”