Sponsors

Codeforces Round #378 (Div. 2), problem: (E) Sleep in Class Solution in C

#include
#define ABS(a) ((a) < 0 ? (a) : (a)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define TRUE 1
#define FALSE 0

#define getchar_unlocked() getchar()

long long fast_in()
{
static long long j_fast;
static char c_fast;
j_fast = 0;
c_fast = getchar_unlocked();
while (c_fast < '0' || c_fast > ‘9’)
c_fast = getchar_unlocked();
while (c_fast >= ‘0’ && c_fast <= '9') { j_fast = (j_fast << 1) + (j_fast << 3) + c_fast - '0'; c_fast = getchar_unlocked(); } return j_fast; } #define MAXN 1000006 long long n, a[MAXN], ans[MAXN], down[MAXN], up[MAXN], wdown[MAXN], wup[MAXN]; long long binary_search_up(long long req) { // search for last occurance static long long f, l, m; f = 1; l = n + 1; while ((l - f) > 1) {
m = ((l + f) >> 1);
if (up[m] >= req)
f = m;
else
l = m;
}
if (up[l] == req)
return l;
else
return f;
}
long long binary_search_down(long long req)
{
// search for first occurance
static long long f, l, m;
f = 0;
l = n;
while ((l – f) > 1) {
m = ((l + f) >> 1);
if (down[m] < req) f = m; else l = m; } if (down[f] == req) return f; else return l; } int main() { static long long i, considered, uptill, downtill; static char c; n = fast_in(); for (i = 1; i <= n; i++) { c = getchar_unlocked(); if (c == 'U') a[i] = 1; else a[i] = 0; } for (i = 1; i <= n; i++) { if (a[i]) down[i] = down[i - 1]; else down[i] = down[i - 1] + 1; } for (i = 0; i <= n; i++) { if (a[i]) wdown[i] = wdown[i - 1]; else wdown[i] = wdown[i - 1] + i; } for (i = n; i > 0; i–) {
if (a[i])
up[i] = up[i + 1] + 1;
else
up[i] = up[i + 1];
}
for (i = n; i > 0; i–) {
if (a[i])
wup[i] = wup[i + 1] + (n – i + 1);
else
wup[i] = wup[i + 1];
}
up[0] = up[1];
wup[0] = wup[1];
down[n + 1] = down[n];
wdown[n + 1] = wdown[n];
// for (i = 0; i <= n + 1; i++) { // printf("%lld %lld %lld %lld\n", up[i], down[i], wup[i], // wdown[i]); // } for (i = 1; i <= n; i++) { considered = MIN(up[0] - up[i], down[n] - down[i]); if (a[i]) { downtill = binary_search_down(considered + down[i] + 1); uptill = binary_search_up(considered + up[i]); } else { downtill = binary_search_down(considered + down[i]); uptill = binary_search_up(considered + up[i] + 1); } ans[i] = (wdown[downtill] - wdown[i] - i * (down[downtill] - down[i])); ans[i] += (wup[uptill] - wup[i] - (n - i + 1) * (up[uptill] - up[i])); ans[i] <<= 1; //printf("%lld considered %lld downtill %lld uptill %lld\n", i, // considered, downtill, uptill); if (a[i]) { if ((down[n] - down[i]) > (up[0] – up[i]))
ans[i] += i;
else
ans[i] += (n – i + 1);
} else {
if ((down[n] – down[i]) >= (up[0] – up[i]))
ans[i] += i;
else
ans[i] += (n – i + 1);
}
}
for (i = 1; i <= n; i++) printf("%lld ", ans[i]); return 0; }

Samsung Display Unveils New...

Samsung Display Prepares to Dazzle CES 2026 with Next-Gen...

What do male octopus...

The Fateful Finale: What Happens to Male Octopuses After...

From Broadway to Banana...

The Ultimate Crossover: Broadway Star Derek Klena Joins the...

Stock Market Highlights 1...

A Flat Start to the New Year: Sensex and...

Why the Mineral Resources...

The Forces Behind Mineral Resources’ 10% Share Price Surge Shareholders...

Personal Injury Attorneys Ross...

Buffalo Lawyers Ross and Timothy Cellino Detail the Risks...

Samsung Display Unveils New OLED Tech for Robots & Wearables at CES 2026

Samsung Display Prepares to Dazzle CES 2026 with Next-Gen OLED Innovations As the tech world gears up for CES 2026, all eyes are turning toward...

What do male octopus do after mating?

The Fateful Finale: What Happens to Male Octopuses After Mating? The ocean is home to countless wonders, but few natural phenomena are as dramatically tragic...

From Broadway to Banana Ball! Derek Klena Joins Savannah Bananas in Viral Baseball League

The Ultimate Crossover: Broadway Star Derek Klena Joins the Savannah Bananas In a surprising twist that perfectly captures the unique spirit of the viral baseball...

Stock Market Highlights 1 January 2026: Sensex, Nifty close flat on first trading day of new year

A Flat Start to the New Year: Sensex and Nifty End January 1, 2026 Trading Muted The dawn of the new year, January 1, 2026,...

Why the Mineral Resources share price is up 10% in a month

The Forces Behind Mineral Resources’ 10% Share Price Surge Shareholders of Mineral Resources Ltd (ASX: MIN) are breathing a collective sigh of relief, if not...

Personal Injury Attorneys Ross Cellino and Timothy Cellino of Buffalo, NY, Break Down Construction Site Accidents for HelloNation

Buffalo Lawyers Ross and Timothy Cellino Detail the Risks of Construction Site Accidents Construction sites, while essential for urban development, remain some of the most...

War on drug resistance goes undersea

The Deep Sea: The New Battlefield Against Superbugs Antimicrobial Resistance (AMR) is unequivocally one of the most pressing global health crises of our time. As...

Supreme Court takes suo motu cognisance of concerns surrounding definition of Aravalli range

Supreme Court Intervenes Over Controversial Aravalli Range Definition In a significant move highlighting the escalating concerns over environmental protection, the Supreme Court of India has...

How Dhurandhar’s repurposed qawwali with Pakistani origins engages with a shared cultural past, one the film ignores

The Forgotten Heritage: How Dhurandhar’s Qawwali Connects to Sufi Masters and a Shared Cultural Past The contemporary cinematic landscape often borrows and repurposes older melodies,...