Sponsors

Codeforces Round #381 (Div. 1), problem: (C) Alyona and towers Solution C/C++

Hi guys , I just solved the third problem of round 381 , Hope you like it , feel free to comment any better solution .

 

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
const int inf=1e9+99;

const int N=1<<19;
struct dat {
ll d;
int ans;
int dL,dR;
int lenL,lenR,len;

void inc(ll dd) {
d += dd;
dL=dR=(d>0)-(d<0);
lenL=lenR=ans=!!d;
len=1;
}
void merge(const dat &L,const dat &R) {
ans=max(L.ans,R.ans);
dL=L.dL;
dR=R.dR;
len=L.len+R.len;
lenL=L.lenL;
lenR=R.lenR;
if(L.dR >= R.dL && L.dR && R.dL) {
ans=max(ans,L.lenR+R.lenL);
if(lenL==L.len) lenL += R.lenL;
if(lenR==R.len) lenR += L.lenR;
}
}
} rmq[N+N];

void inc(int x,ll d) {
x+=N;
rmq[x].inc(d);
for(;x>>=1;) rmq[x].merge(rmq[x+x],rmq[x+x+1]);
}

int main() {
int n; scanf(“%d”,&n);
int pr=-1;
for(int i=1;i<=n;i++) {
int a;
scanf(“%d”,&a);
if(i>1) inc(i-1,a-pr);
pr=a;
}

int m; scanf(“%d”,&m); for(;m–;) {
int l,r,d; scanf(“%d%d%d”,&l,&r,&d);
if(l>1) inc(l-1,d);
if(r<n) inc(r,-d);
printf(“%d\n”,rmq[1].ans+1);
}
}

The true meaning of...

Decoding the Shift in Australian Media and Global Strategy Recent...

Beijing warns of more...

Beijing Warns of New Volatility in Global Semiconductor Markets The...

Should I fear raccoons?

Understanding Raccoon Behavior: Safety and Awareness Raccoons are some of...

Family, former presidents and...

Honoring a Legacy: Rev. Jesse Jackson’s Final Sendoff The world...

Humpback whale recovery is...

The Shifting Dynamics of Humpback Whale Paternity The recovery of...

Miracle Sheets Silver-Infused “Self-Cleaning”...

Understanding the Miracle Sheets Silver-Infused Technology The bedding industry is...

The true meaning of AUKUS, the ABC’s new Q+A is a turn off, and news.com’s TikTok mystery

Decoding the Shift in Australian Media and Global Strategy Recent developments across the Australian media and political landscape have sparked significant debate, ranging from the...

Beijing warns of more chip supply worries after Nexperia China claims it was cut off from SAP

Beijing Warns of New Volatility in Global Semiconductor Markets The global semiconductor landscape is facing a fresh wave of uncertainty as China’s Ministry of Commerce...

Should I fear raccoons?

Understanding Raccoon Behavior: Safety and Awareness Raccoons are some of the most recognizable urban wildlife in North America. With their distinctive black masks and ringed...

Family, former presidents and a Hall of Famer give Rev. Jesse Jackson a final sendoff

Honoring a Legacy: Rev. Jesse Jackson’s Final Sendoff The world paused to honor a titan of the civil rights movement as family members, former presidents,...

Humpback whale recovery is changing who fathers the calves

The Shifting Dynamics of Humpback Whale Paternity The recovery of humpback whale populations across the globe is often cited as a triumph for environmental protection...

Miracle Sheets Silver-Infused “Self-Cleaning” Claims Examined: 2026 Consumer Report on Miracle Made Cooling Fabric Positioning, Antimicrobial Testing Context, and What Shoppers Should Verify

Understanding the Miracle Sheets Silver-Infused Technology The bedding industry is currently undergoing a technological revolution, with Miracle Made (popularly known for Miracle Sheets) at the...

FG bans cash tax collections, revenue roadblocks

Nigeria Bans Cash Tax Collections and Revenue Roadblocks to Boost Transparency In a landmark move aimed at modernizing the nation’s fiscal operations, the Federal Government...

Iran vs US-Israel war: The legality, or illegality, of killing a foreign leader, explained

Understanding the Legal Implications of Targeted Strikes on Sovereign LeadersThe geopolitical landscape of the Middle East has been thrust into unprecedented turmoil following reports...

Sloane Stephens reveals Indian Wells kit

Sloane Stephens Unveils Stylish New Look for 'Tennis Paradise' at Indian Wells As the tennis world prepares for one of the most prestigious stops on...