SAITAMA, Japan — All the necessary ingredients were in place for a damaging Olympic loss for the U.S. men’s basketball team Tuesday. Spain arrived at the quarterfinals focused and motivated by years of head-to-head heartbreak. Ricky Rubio, who first emerged as a teenage phenom more than a decade ago, played the game of his life.

The United States endured another slow start on offense, unable to get its three-point shooting on track, and Spain’s size advantage helped it control the boards and protect the rim as it built an 11-point second-quarter lead. Suddenly, Spain, the reigning FIBA World Cup champion and the world’s second-ranked team, appeared to have a real shot at upsetting the top-ranked United States.

But the Americans, who have medaled in all 18 of their previous Olympic appearances, responded well to the adverse circumstances, closing the second quarter on a 7-0 run to tie the score and opening the third quarter with a 22-6 blitz.

When the dust settled, Team USA claimed a 95-81 win in the quarterfinals at Saitama Super Arena, ending Spain’s gold medal hopes for the fifth straight Olympics. In addition to wins in the gold medal games at the 2008 and 2012 Olympics, the United States defeated Spain in the 2016 semifinals and the 2004 quarterfinals.

Kevin Durant rose to the moment, finishing with a team-high 29 points, two rebounds and four assists in his second straight takeover performance. While the Brooklyn Nets forward has demurred whenever he has been asked if he is Team USA’s leader, his knack for stepping forward at critical junctures has suggested he understands the nature of his role.

“We’ve got to finish it,” Durant said. “We’re supposed to be here. For us, it’s about getting the gold.”

With Spain threatening to pull away in the second quarter, Durant kept the Americans afloat with a tough, contested three-pointer and a drive for a dunk just before halftime.

“It was just big to respond” before halftime, U.S. forward Jayson Tatum said. “It was a time not to get worried, but to raise our sense of urgency. Basketball is a game of runs. [Spain is] a good team. They’re going to make shots. It’s all about how you respond. … It was a dogfight. It was what we expected.”

At the start of the third quarter, Durant hit three three-pointers to help the United States build a 16-point lead. His rhythm and confidence proved infectious, as the Americans finally got hot from the outside. After making just 4 of 17 three-point attempts in the first half, the United States made 9 of 15 in the second.

In the closing minutes, Durant ensured that Team USA avoided the late-game miscues that cost it dearly in its three losses this summer. After burying a turnaround midrange jumper, Durant found Jrue Holiday with a drive-and-kick pass for a decisive three-pointer. To deliver the exclamation point, Durant leaked out in transition with less than 20 seconds left to throw down one final dunk.

“We feel absolutely great about the victory, knowing full well that there’s a lot of work to be done,” U.S. Coach Gregg Popovich said. “In these games, every team has people they depend on to come through. [Durant] certainly did.”

The United States withstood a valiant performance from Rubio, a Spanish mainstay who was recently traded by the Minnesota Timberwolves to the Cleveland Cavaliers. Typically a pass-first point guard, Rubio scored a game-high 38 points by attacking aggressively off the dribble.

Spain couldn’t generate enough other offense once Team USA got its shooting on track in the second half, and its age started to show as the game wore on. Pau Gasol, Marc Gasol and Rudy Fernández, all in their late 30s or older, were scoreless. Sergio Rodriguez provided the only consistent help to Rubio, scoring 16 points in what could be the final Olympic game for a Spanish core that has competed together since the 2008 Beijing Games.

Pau Gasol said during his postgame comments that he has reached the end of his international career at 41. Marc Gasol, 36, followed his brother’s lead by tweeting, “That’s all, folks!” after the loss.

“This is a time to really enjoy the moment,” Spain Coach Sergio Scariolo said. “Being a part of a legend is a privilege. To share locker rooms, share practices, share games, trips. I couldn’t even dream of anything like this when I took over so many years ago.

“At the same time, there are many of us who are motivated and ready to keep going and keep competing with the legacy of the ones who will not be with us anymore. They are an example, hopefully, for our young players. … I will miss those of them who are not coming back, but our link will be there forever.”

While Durant led the way, five Americans finished in double figures in scoring. Tatum scored 13 points, and Damian Lillard added 12. Team USA forced 17 turnovers and had 28 assists on 36 baskets.

Those positive indicators will be put to the test in Thursday’s semifinal, when the Americans will face Australia. There are still bad habits to shake — such as slow starts and occasional overpassing — but the United States passed a critical test by pulling through in a tense contest against a decorated opponent with far more shared experience.

The Americans “have 10 times more talent than any other team here,” Scariolo said. “But they play as a team, they share the ball, and they play good defense. It shows that they’ve been working seriously to get ready and to compete in these Games.”