Submission #236452

#TimeUsernameProblemLanguageResultExecution timeMemory
236452nishkarshConstellation 3 (JOI20_constellation3)C++14
100 / 100
748 ms105592 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define vi vector <int>
#define vl vector <ll>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d",x)
#define plld(x) printf("%lld",x)
#define pds(x) printf("%d ",x)
#define pllds(x) printf("%lld ",x)
#define pdn(x) printf("%d\n",x)
#define plldn(x) printf("%lld\n",x)
#define INF 2e9
#define INFLL 4e18
using namespace std;
ll powmod(ll base,ll exponent,ll mod){ // with mod < 1e9
	ll ans=1;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
ll gcd(ll a, ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
const int upperlimit = 2e5+1;
const int mod = 998244353;
const int logn = 20;
struct star
{
	int x,y,c;
};
struct node_data{
	ll ssum=0,dpsum=0,child_dpsum=0;
	bool lazy=false;
	ll ssm=0,dsm=0,cdsm=0;
};
int a[upperlimit];
int next_greater[upperlimit];
int prev_greater[upperlimit];
int etl[upperlimit];
int etr[upperlimit];
vi child[upperlimit];
star stars[upperlimit];
ll sum[upperlimit];
ll dp[upperlimit];
vector<pii> ranges[upperlimit];
int sparse[upperlimit][logn];
stack<int> temp;
node_data segtree[4*upperlimit];
int node_cnt=0;
int retnd(int x,int y){
	for(int i = logn-1; i >= 0; i--){
		if(a[sparse[x][i]]<y) x=sparse[x][i];
	}
	return x;
}
void et(int node){
	etl[node]=++node_cnt;
	for(int i = 0; i < child[node].size(); i++) et(child[node][i]);
	etr[node]=node_cnt;
}
void lazy_func(int node,int i,int j){
	segtree[node].ssum+=segtree[node].ssm;
	segtree[node].dpsum+=segtree[node].dsm;
	segtree[node].child_dpsum+=segtree[node].cdsm;
	if(i!=j){
		segtree[2*node].ssm+=segtree[node].ssm;
		segtree[2*node+1].ssm+=segtree[node].ssm;
		segtree[2*node].dsm+=segtree[node].dsm;
		segtree[2*node+1].dsm+=segtree[node].dsm;
		segtree[2*node].cdsm+=segtree[node].cdsm;
		segtree[2*node+1].cdsm+=segtree[node].cdsm;
		segtree[2*node].lazy=true;segtree[2*node+1].lazy=true;
	}
	segtree[node].ssm=0;
	segtree[node].dsm=0;
	segtree[node].cdsm=0;
	segtree[node].lazy=false;
}
void update(int node,int i,int j,int l,int r,ll s,ll d,ll cds){
	if(segtree[node].lazy) lazy_func(node,i,j);
	if((i>r)||(l>j)||(i>j)||(l>r)) return;
	if((i>=l)&&(j<=r)){
		segtree[node].ssm=s;segtree[node].dsm=d;segtree[node].cdsm=cds;
		lazy_func(node,i,j);
		return;
	}
	int mid=(i+j)>>1;
	update(2*node,i,mid,l,r,s,d,cds);update(2*node+1,mid+1,j,l,r,s,d,cds);
}
node_data query(int node,int i,int j,int ind){
	if(segtree[node].lazy) lazy_func(node,i,j);
	if(i==j) return segtree[node];
	int mid=(i+j)>>1;
	if(ind<=mid) return(query(2*node,i,mid,ind));
	else return(query(2*node+1,mid+1,j,ind));
}
int n;
void dfs(int node){
	ll cds=0;
	for(int i = 0; i < child[node].size(); i++){
		dfs(child[node][i]);
		cds+=dp[child[node][i]];
	}
	dp[node]=cds+sum[node];
	int l=etl[node],r=etr[node];
	for(int i = 0; i < ranges[node].size(); i++){
		node_data gg=query(1,1,n,etl[ranges[node][i].F]);
		dp[node]=min(dp[node],gg.child_dpsum-gg.dpsum+cds+sum[node]+gg.ssum-ranges[node][i].S);
	}
	update(1,1,n,l,r,sum[node],dp[node],cds);
//	pds(node);pllds(sum[node]);plldn(dp[node]);
}
int main() {
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	int m,x;
	star st;
	sd(n);
	a[0]=1000000;a[n+1]=1000000;
	for(int i = 1; i <= n; i++) sd(a[i]);
	sd(m);
	for(int i = 1; i <= m; i++){
		sd(stars[i].x);sd(stars[i].y);sd(stars[i].c);
	}
	for(int i = 1; i <= n; i++){
		while((! temp.empty())&&(a[temp.top()]<a[i])) temp.pop();
		if(temp.empty()) prev_greater[i]=0;
		else prev_greater[i]=temp.top();
		temp.push(i);
	}
	while(! temp.empty()) temp.pop();
	for(int i = n; i > 0; i--){
		while((! temp.empty())&&(a[temp.top()]<=a[i])) temp.pop();
		if(temp.empty()) next_greater[i]=n+1;
		else next_greater[i]=temp.top();
		temp.push(i);
	}
	for(int i = 1; i <= n; i++){
		if(a[prev_greater[i]]<=a[next_greater[i]]){
			sparse[i][0]=prev_greater[i];
			child[prev_greater[i]].pb(i);
		}
		else{
			sparse[i][0]=next_greater[i];
			child[next_greater[i]].pb(i);
		}
	}
	int gg=child[0][0];
	for(int j = 1; j < logn; j++){
		for(int i = 1; i <= n; i++){
			sparse[i][j]=sparse[sparse[i][j-1]][j-1];
		}
	}
	for(int i = 1; i <= m; i++){
		int node=retnd(stars[i].x,stars[i].y);
		ranges[node].pb(mp(stars[i].x,stars[i].c));
		sum[node]+=stars[i].c;
	}
	et(gg);
	dfs(gg);
	plld(dp[gg]);
	return 0;
}

Compilation message (stderr)

constellation3.cpp: In function 'void et(int)':
constellation3.cpp:70:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < child[node].size(); i++) et(child[node][i]);
                 ~~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp: In function 'void dfs(int)':
constellation3.cpp:112:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < child[node].size(); i++){
                 ~~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:118:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ranges[node].size(); i++){
                 ~~^~~~~~~~~~~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:127:8: warning: unused variable 'x' [-Wunused-variable]
  int m,x;
        ^
constellation3.cpp:128:7: warning: unused variable 'st' [-Wunused-variable]
  star st;
       ^~
constellation3.cpp:12:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define sd(x) scanf("%d",&x)
               ~~~~~^~~~~~~~~
constellation3.cpp:129:2: note: in expansion of macro 'sd'
  sd(n);
  ^~
constellation3.cpp:12:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define sd(x) scanf("%d",&x)
               ~~~~~^~~~~~~~~
constellation3.cpp:131:30: note: in expansion of macro 'sd'
  for(int i = 1; i <= n; i++) sd(a[i]);
                              ^~
constellation3.cpp:12:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define sd(x) scanf("%d",&x)
               ~~~~~^~~~~~~~~
constellation3.cpp:132:2: note: in expansion of macro 'sd'
  sd(m);
  ^~
constellation3.cpp:12:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define sd(x) scanf("%d",&x)
               ~~~~~^~~~~~~~~
constellation3.cpp:134:3: note: in expansion of macro 'sd'
   sd(stars[i].x);sd(stars[i].y);sd(stars[i].c);
   ^~
constellation3.cpp:12:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define sd(x) scanf("%d",&x)
               ~~~~~^~~~~~~~~
constellation3.cpp:134:18: note: in expansion of macro 'sd'
   sd(stars[i].x);sd(stars[i].y);sd(stars[i].c);
                  ^~
constellation3.cpp:12:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define sd(x) scanf("%d",&x)
               ~~~~~^~~~~~~~~
constellation3.cpp:134:33: note: in expansion of macro 'sd'
   sd(stars[i].x);sd(stars[i].y);sd(stars[i].c);
                                 ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...