답안 #236452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236452 2020-06-02T10:09:22 Z nishkarsh 별자리 3 (JOI20_constellation3) C++14
100 / 100
748 ms 105592 KB
#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

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);
                                 ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 11 ms 9856 KB Output is correct
4 Correct 11 ms 9856 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 10 ms 9856 KB Output is correct
13 Correct 11 ms 9856 KB Output is correct
14 Correct 10 ms 9856 KB Output is correct
15 Correct 10 ms 9856 KB Output is correct
16 Correct 10 ms 9984 KB Output is correct
17 Correct 11 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 11 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 11 ms 9856 KB Output is correct
4 Correct 11 ms 9856 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 10 ms 9856 KB Output is correct
13 Correct 11 ms 9856 KB Output is correct
14 Correct 10 ms 9856 KB Output is correct
15 Correct 10 ms 9856 KB Output is correct
16 Correct 10 ms 9984 KB Output is correct
17 Correct 11 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 11 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 12 ms 10368 KB Output is correct
24 Correct 13 ms 10368 KB Output is correct
25 Correct 13 ms 10368 KB Output is correct
26 Correct 13 ms 10368 KB Output is correct
27 Correct 13 ms 10368 KB Output is correct
28 Correct 14 ms 10368 KB Output is correct
29 Correct 13 ms 10368 KB Output is correct
30 Correct 15 ms 10368 KB Output is correct
31 Correct 13 ms 10368 KB Output is correct
32 Correct 13 ms 10496 KB Output is correct
33 Correct 13 ms 10496 KB Output is correct
34 Correct 12 ms 10496 KB Output is correct
35 Correct 13 ms 10496 KB Output is correct
36 Correct 16 ms 10564 KB Output is correct
37 Correct 13 ms 10496 KB Output is correct
38 Correct 13 ms 10624 KB Output is correct
39 Correct 15 ms 10328 KB Output is correct
40 Correct 14 ms 10600 KB Output is correct
41 Correct 13 ms 10368 KB Output is correct
42 Correct 13 ms 10368 KB Output is correct
43 Correct 14 ms 10624 KB Output is correct
44 Correct 13 ms 10368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 11 ms 9856 KB Output is correct
4 Correct 11 ms 9856 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 10 ms 9856 KB Output is correct
13 Correct 11 ms 9856 KB Output is correct
14 Correct 10 ms 9856 KB Output is correct
15 Correct 10 ms 9856 KB Output is correct
16 Correct 10 ms 9984 KB Output is correct
17 Correct 11 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 11 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 12 ms 10368 KB Output is correct
24 Correct 13 ms 10368 KB Output is correct
25 Correct 13 ms 10368 KB Output is correct
26 Correct 13 ms 10368 KB Output is correct
27 Correct 13 ms 10368 KB Output is correct
28 Correct 14 ms 10368 KB Output is correct
29 Correct 13 ms 10368 KB Output is correct
30 Correct 15 ms 10368 KB Output is correct
31 Correct 13 ms 10368 KB Output is correct
32 Correct 13 ms 10496 KB Output is correct
33 Correct 13 ms 10496 KB Output is correct
34 Correct 12 ms 10496 KB Output is correct
35 Correct 13 ms 10496 KB Output is correct
36 Correct 16 ms 10564 KB Output is correct
37 Correct 13 ms 10496 KB Output is correct
38 Correct 13 ms 10624 KB Output is correct
39 Correct 15 ms 10328 KB Output is correct
40 Correct 14 ms 10600 KB Output is correct
41 Correct 13 ms 10368 KB Output is correct
42 Correct 13 ms 10368 KB Output is correct
43 Correct 14 ms 10624 KB Output is correct
44 Correct 13 ms 10368 KB Output is correct
45 Correct 434 ms 76628 KB Output is correct
46 Correct 425 ms 76152 KB Output is correct
47 Correct 448 ms 76024 KB Output is correct
48 Correct 416 ms 76384 KB Output is correct
49 Correct 442 ms 75624 KB Output is correct
50 Correct 419 ms 75512 KB Output is correct
51 Correct 432 ms 75616 KB Output is correct
52 Correct 436 ms 76664 KB Output is correct
53 Correct 418 ms 76224 KB Output is correct
54 Correct 748 ms 95140 KB Output is correct
55 Correct 709 ms 89196 KB Output is correct
56 Correct 657 ms 85868 KB Output is correct
57 Correct 646 ms 83748 KB Output is correct
58 Correct 537 ms 88696 KB Output is correct
59 Correct 560 ms 88420 KB Output is correct
60 Correct 339 ms 105592 KB Output is correct
61 Correct 426 ms 75128 KB Output is correct
62 Correct 707 ms 95728 KB Output is correct
63 Correct 430 ms 74568 KB Output is correct
64 Correct 410 ms 73900 KB Output is correct
65 Correct 709 ms 96116 KB Output is correct
66 Correct 423 ms 74336 KB Output is correct