답안 #955117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955117 2024-03-29T12:14:46 Z LCJLY 별자리 3 (JOI20_constellation3) C++14
100 / 100
674 ms 144764 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int arr[200005];
int n;

//fw
int fw[200005];
void upd(int x, int y){
	while(x<200005){
		fw[x]+=y;
		x+=x&(-x);
	}
}

int sum(int x){
	int counter=0;
	while(x>0){
		counter+=fw[x];
		x-=x&(-x);
	}
	return counter;
}

void rangeUpd(int x, int y, int c){
	if(x>y) return;
	//show3(x,x,y,y,c,c);
	upd(x,c);
	upd(y+1,-c);
}

//sparse table
struct SparseTable{
	vector<vector<pii>>st;
	int n,k;
	void init(int nn, int arr[]){
		n=nn;
		k=MSB(n);
		st.resize(k);
		for(int x=0;x<n;x++){
			st[0].push_back({arr[x],x});
		}
		for(int x=1;x<k;x++){
			for(int y=0;y+(1<<x)<=n;y++){
				st[x].push_back(min(st[x-1][y],st[x-1][y+(1<<(x-1))]));
			}
		}
	}
	
	inline int MSB(unsigned int x){ return 32-__builtin_clz(x);}
	
	pii query(int x, int y){
		int k=MSB(y-x+1)-1;
		return min(st[k][x],st[k][y-(1<<k)+1]);
	}
};

//cartesian tree
vector<int>adj[200005];
int two[22][200005];
SparseTable st;
int rt=0;
int ptr=0;
int in[400005];
int out[400005];

void dnc(int l, int r, int par){
	pii hold=st.query(l,r);
	//show2(l,l,r,r);
	in[hold.second]=++ptr;
	
	if(par!=-1){
		adj[par].push_back(hold.second);
		adj[hold.second].push_back(par);
		two[0][hold.second]=par;
	}
	else{
		rt=hold.second;
	}
	
	for(int x=0;x<20;x++){
		two[x+1][hold.second]=two[x][two[x][hold.second]];
	}
	if(hold.second!=l){
		dnc(l,hold.second-1,hold.second);
	}
	if(hold.second!=r){
		dnc(hold.second+1,r,hold.second);
	}
	out[hold.second]=ptr;
	//show3(hold.second,hold.second,in[hold.second],in[hold.second],out[hold.second],out[hold.second]);
}

vector<pii>star[200005];
void add(int index, int h, int cost){
	int hold=index;
	for(int x=19;x>=0;x--){
		int nxt=two[x][index];
		if(arr[nxt]>=h){
			index=nxt;
		}
	}
	star[index].push_back({hold,cost});
}

int dp[200005];

void dfs(int index, int par){
	for(auto it:adj[index]){
		if(it==par) continue;
		dfs(it,index);
	}
	//show(index,index);
	int counter=0;
	for(auto it:adj[index]){
		if(it==par) continue;
		counter+=dp[it];
		//show(it,it);
	}
	//for(int y=1;y<=n;y++){
		//cout << sum(y) << " ";
	//}
	//cout << endl;
	rangeUpd(in[index],out[index],counter);
	//show2(in[index],in[index],out[index],out[index]);
	//for(int y=1;y<=n;y++){
		//cout << sum(y) << " ";
	//}
	//cout << endl;
	//show3(l,in[index],r,out[index],counter,counter);
	dp[index]=counter;
	//show(counter,counter);
	for(auto it:star[index]){
		//show2(it.first,in[it.first],it.second,it.second);
		//show(val,sum(in[it.first])-sum2(in[it.first]));
		//show(val2,sum2(in[it.first]));
		//show(in[it.first],in[it.first]);
		dp[index]=max(dp[index],sum(in[it.first])+it.second);
	}
	
	rangeUpd(in[index],out[index],-dp[index]);
	//show3(l,in[index],r,out[index],dp[index],dp[index]);
	//show2(index,index,dp[index],dp[index]);
}

void solve(){
	cin >> n;
	for(int x=1;x<=n;x++){
		cin >> arr[x];
		arr[x]=n-arr[x];
	}
	
	st.init(n+1,arr);
	dnc(1,n,-1);
	
	int temp,temp2,temp3;
	int q;
	cin >> q;
	
	int counter=0;
	for(int x=0;x<q;x++){
		cin >> temp >> temp2 >> temp3;
		temp2=n-temp2+1;
		add(temp,temp2,temp3);
		counter+=temp3;
	}
	
	dfs(rt,-1);
	cout << counter-dp[rt];
}	
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 49768 KB Output is correct
2 Correct 7 ms 49756 KB Output is correct
3 Correct 7 ms 49736 KB Output is correct
4 Correct 8 ms 49660 KB Output is correct
5 Correct 7 ms 49780 KB Output is correct
6 Correct 8 ms 49756 KB Output is correct
7 Correct 7 ms 49756 KB Output is correct
8 Correct 7 ms 49752 KB Output is correct
9 Correct 7 ms 49756 KB Output is correct
10 Correct 8 ms 49752 KB Output is correct
11 Correct 8 ms 49756 KB Output is correct
12 Correct 7 ms 49756 KB Output is correct
13 Correct 7 ms 49592 KB Output is correct
14 Correct 7 ms 49756 KB Output is correct
15 Correct 7 ms 49756 KB Output is correct
16 Correct 8 ms 49756 KB Output is correct
17 Correct 8 ms 49752 KB Output is correct
18 Correct 8 ms 49756 KB Output is correct
19 Correct 7 ms 50004 KB Output is correct
20 Correct 7 ms 49664 KB Output is correct
21 Correct 8 ms 49756 KB Output is correct
22 Correct 7 ms 49756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 49768 KB Output is correct
2 Correct 7 ms 49756 KB Output is correct
3 Correct 7 ms 49736 KB Output is correct
4 Correct 8 ms 49660 KB Output is correct
5 Correct 7 ms 49780 KB Output is correct
6 Correct 8 ms 49756 KB Output is correct
7 Correct 7 ms 49756 KB Output is correct
8 Correct 7 ms 49752 KB Output is correct
9 Correct 7 ms 49756 KB Output is correct
10 Correct 8 ms 49752 KB Output is correct
11 Correct 8 ms 49756 KB Output is correct
12 Correct 7 ms 49756 KB Output is correct
13 Correct 7 ms 49592 KB Output is correct
14 Correct 7 ms 49756 KB Output is correct
15 Correct 7 ms 49756 KB Output is correct
16 Correct 8 ms 49756 KB Output is correct
17 Correct 8 ms 49752 KB Output is correct
18 Correct 8 ms 49756 KB Output is correct
19 Correct 7 ms 50004 KB Output is correct
20 Correct 7 ms 49664 KB Output is correct
21 Correct 8 ms 49756 KB Output is correct
22 Correct 7 ms 49756 KB Output is correct
23 Correct 9 ms 50012 KB Output is correct
24 Correct 9 ms 50012 KB Output is correct
25 Correct 10 ms 50200 KB Output is correct
26 Correct 9 ms 50012 KB Output is correct
27 Correct 9 ms 50168 KB Output is correct
28 Correct 8 ms 50012 KB Output is correct
29 Correct 9 ms 50012 KB Output is correct
30 Correct 9 ms 50012 KB Output is correct
31 Correct 9 ms 50128 KB Output is correct
32 Correct 9 ms 50108 KB Output is correct
33 Correct 9 ms 50268 KB Output is correct
34 Correct 9 ms 50268 KB Output is correct
35 Correct 9 ms 50256 KB Output is correct
36 Correct 9 ms 50280 KB Output is correct
37 Correct 8 ms 50276 KB Output is correct
38 Correct 8 ms 50272 KB Output is correct
39 Correct 9 ms 50012 KB Output is correct
40 Correct 8 ms 50256 KB Output is correct
41 Correct 11 ms 50008 KB Output is correct
42 Correct 8 ms 50076 KB Output is correct
43 Correct 9 ms 50268 KB Output is correct
44 Correct 9 ms 50184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 49768 KB Output is correct
2 Correct 7 ms 49756 KB Output is correct
3 Correct 7 ms 49736 KB Output is correct
4 Correct 8 ms 49660 KB Output is correct
5 Correct 7 ms 49780 KB Output is correct
6 Correct 8 ms 49756 KB Output is correct
7 Correct 7 ms 49756 KB Output is correct
8 Correct 7 ms 49752 KB Output is correct
9 Correct 7 ms 49756 KB Output is correct
10 Correct 8 ms 49752 KB Output is correct
11 Correct 8 ms 49756 KB Output is correct
12 Correct 7 ms 49756 KB Output is correct
13 Correct 7 ms 49592 KB Output is correct
14 Correct 7 ms 49756 KB Output is correct
15 Correct 7 ms 49756 KB Output is correct
16 Correct 8 ms 49756 KB Output is correct
17 Correct 8 ms 49752 KB Output is correct
18 Correct 8 ms 49756 KB Output is correct
19 Correct 7 ms 50004 KB Output is correct
20 Correct 7 ms 49664 KB Output is correct
21 Correct 8 ms 49756 KB Output is correct
22 Correct 7 ms 49756 KB Output is correct
23 Correct 9 ms 50012 KB Output is correct
24 Correct 9 ms 50012 KB Output is correct
25 Correct 10 ms 50200 KB Output is correct
26 Correct 9 ms 50012 KB Output is correct
27 Correct 9 ms 50168 KB Output is correct
28 Correct 8 ms 50012 KB Output is correct
29 Correct 9 ms 50012 KB Output is correct
30 Correct 9 ms 50012 KB Output is correct
31 Correct 9 ms 50128 KB Output is correct
32 Correct 9 ms 50108 KB Output is correct
33 Correct 9 ms 50268 KB Output is correct
34 Correct 9 ms 50268 KB Output is correct
35 Correct 9 ms 50256 KB Output is correct
36 Correct 9 ms 50280 KB Output is correct
37 Correct 8 ms 50276 KB Output is correct
38 Correct 8 ms 50272 KB Output is correct
39 Correct 9 ms 50012 KB Output is correct
40 Correct 8 ms 50256 KB Output is correct
41 Correct 11 ms 50008 KB Output is correct
42 Correct 8 ms 50076 KB Output is correct
43 Correct 9 ms 50268 KB Output is correct
44 Correct 9 ms 50184 KB Output is correct
45 Correct 674 ms 125028 KB Output is correct
46 Correct 595 ms 130936 KB Output is correct
47 Correct 607 ms 128272 KB Output is correct
48 Correct 662 ms 126188 KB Output is correct
49 Correct 629 ms 132100 KB Output is correct
50 Correct 606 ms 122852 KB Output is correct
51 Correct 589 ms 138124 KB Output is correct
52 Correct 642 ms 131476 KB Output is correct
53 Correct 610 ms 140868 KB Output is correct
54 Correct 616 ms 144112 KB Output is correct
55 Correct 605 ms 142452 KB Output is correct
56 Correct 578 ms 128672 KB Output is correct
57 Correct 616 ms 128056 KB Output is correct
58 Correct 420 ms 136620 KB Output is correct
59 Correct 409 ms 136732 KB Output is correct
60 Correct 193 ms 144764 KB Output is correct
61 Correct 539 ms 124032 KB Output is correct
62 Correct 578 ms 143500 KB Output is correct
63 Correct 577 ms 131788 KB Output is correct
64 Correct 528 ms 121604 KB Output is correct
65 Correct 584 ms 135168 KB Output is correct
66 Correct 526 ms 139432 KB Output is correct