Codeforces Round #419 (Div. 1), problem: (D) Karen and Cards Solution In C/C++

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 500010;
ll maxn[N<<2],minn[N<<2],sum[N<<2];
int n,p,q,r,tot=1,x[N],y[N],z[N];
ll ret=0;
vector<int> c[N];
void update(int a,int b,int x,int l,int r,int i)
{
if(a>r || l>b || minn[i]>x) return;
if(a<=l && r<=b && maxn[i]<=x)
{
maxn[i]=minn[i]=x;
sum[i]=(ll)(r-l+1)*x;
return;
}
int mid=(l+r)>>1;
if(maxn[i]==minn[i])
{
maxn[i<<1]=maxn[i<<1|1]=minn[i<<1]=minn[i<<1|1]=minn[i];
sum[i<<1]=sum[i<<1|1]=(r-mid)*minn[i];
}
update(a,b,x,l,mid,i<<1);
update(a,b,x,mid+1,r,i<<1|1);
maxn[i]=max(maxn[i<<1],maxn[i<<1|1]);
minn[i]=min(minn[i<<1],minn[i<<1|1]);
sum[i]=sum[i<<1]+sum[i<<1|1];
}
int main()
{
scanf(“%d%d%d%d”,&n,&p,&q,&r);
while(tot<=p) tot<<=1;
for(int i=0;i<n;i++)
{
scanf(“%d%d%d”,&x[i],&y[i],&z[i]);
c[z[i]].push_back(i);
update(1,x[i],y[i],1,tot,1);
}
for(int i=r;i;i–)
{
for(int k=0;k<c[i].size();k++)
{
int j=c[i][k];
update(1,x[j],q,1,tot,1);
update(1,p,y[j],1,tot,1);
}
ret+=sum[1];
}
cout<<(ll)(p)*q*r-ret;
return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 500010;
ll maxn[N<<2],minn[N<<2],sum[N<<2];
int n,p,q,r,tot=1,x[N],y[N],z[N];
ll ret=0;
vector<int> c[N];
void update(int a,int b,int x,int l,int r,int i)
{
if(a>r || l>b || minn[i]>x) return;
if(a<=l && r<=b && maxn[i]<=x)
{
maxn[i]=minn[i]=x;
sum[i]=(ll)(r-l+1)*x;
return;
}
int mid=(l+r)>>1;
if(maxn[i]==minn[i])
{
maxn[i<<1]=maxn[i<<1|1]=minn[i<<1]=minn[i<<1|1]=minn[i];
sum[i<<1]=sum[i<<1|1]=(r-mid)*minn[i];
}
update(a,b,x,l,mid,i<<1);
update(a,b,x,mid+1,r,i<<1|1);
maxn[i]=max(maxn[i<<1],maxn[i<<1|1]);
minn[i]=min(minn[i<<1],minn[i<<1|1]);
sum[i]=sum[i<<1]+sum[i<<1|1];
}
int main()
{
scanf(“%d%d%d%d”,&n,&p,&q,&r);
while(tot<=p) tot<<=1;
for(int i=0;i<n;i++)
{
scanf(“%d%d%d”,&x[i],&y[i],&z[i]);
c[z[i]].push_back(i);
update(1,x[i],y[i],1,tot,1);
}
for(int i=r;i;i–)
{
for(int k=0;k<c[i].size();k++)
{
int j=c[i][k];
update(1,x[j],q,1,tot,1);
update(1,p,y[j],1,tot,1);
}
ret+=sum[1];
}
cout<<(ll)(p)*q*r-ret;
return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Everything You Need to Know About Viral Kitchen Gadgets Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Gavin Newsom finally admits closing beaches during COVID was “insane,” and Bill Maher says he should have figured that out WHEN IT MATTERED!

California Governor Gavin Newsom has stirred up renewed political debate after finally acknowledging that shutting down outdoor spaces during the pandemic was a massive...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!