답안 #464799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
464799 2021-08-14T07:23:43 Z jamezzz Dungeon 3 (JOI21_ho_t5) C++17
100 / 100
990 ms 70388 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));

#define maxn 200005

int n,q,a[maxn],b[maxn],s,t,u,r[maxn];
ll x[maxn],ans[maxn];
vi disc;
stack<int> st;

int get(ll i){
	return upper_bound(all(disc),i)-disc.begin();
}

struct query{
	int u,i,mult;
};

vector<query> qrys[maxn];

struct maxseg{
	int s,e,m,v;maxseg *l,*r;
	maxseg(int _s,int _e){
		s=_s;e=_e;m=(s+e)>>1;v=0;
		if(s!=e)l=new maxseg(s,m),r=new maxseg(m+1,e);
	}
	void up(int x,int nv){
		if(s==x&&e==x){ v=nv;return; }
		if(x<=m)l->up(x,nv);
		else r->up(x,nv);
		v=max(l->v,r->v);
	}
	int qry(int x,int y){
		if(s==x&&e==y)return v;
		if(y<=m)return l->qry(x,y);
		if(x>m)return r->qry(x,y);
		return max(l->qry(x,m),r->qry(m+1,y));
	}
}*die;

struct minseg{
	int s,e,m;ii v;minseg *l,*r;
	minseg(int _s,int _e){
		s=_s;e=_e;m=(s+e)>>1;v={0,0};
		if(s!=e)l=new minseg(s,m),r=new minseg(m+1,e);
	}
	void up(int x,ii nv){
		if(s==x&&e==x){ v=nv;return; }
		if(x<=m)l->up(x,nv);
		else r->up(x,nv);
		v=min(l->v,r->v);
	}
	ii qry(int x,int y){
		if(s==x&&e==y)return v;
		if(y<=m)return l->qry(x,y);
		if(x>m)return r->qry(x,y);
		return min(l->qry(x,m),r->qry(m+1,y));
	}
}*getmin;

struct fw{
	ll ft[maxn];
	void init(){
		for(int i=1;i<=n+1;++i){
			ft[i]=0;
		}
	}
	void up(int x,int y,ll v){
		while(x<=n+1){
			ft[x]+=v;x+=x&-x;
		}
		++y;
		while(y<=n+1){
			ft[y]-=v;y+=y&-y;
		}
	}
	ll qry(int x){
		ll res=0;
		while(x){
			res+=ft[x];x-=x&-x;
		}
		return res;
	}
}*m=new fw(),*c=new fw();

int main(){
	sf("%d%d",&n,&q);
	
	die=new maxseg(1,n+1);
	getmin=new minseg(1,n+1);
	
	for(int i=1;i<=n;++i){
		sf("%d",&a[i]);
		die->up(i+1,a[i]);//max jump
		x[i+1]=x[i]+a[i];
	}
	for(int i=1;i<=n;++i){
		sf("%d",&b[i]);
		getmin->up(i,{b[i],i});
	}
	
	for(int i=0;i<q;++i){
		sf("%d%d%d",&s,&t,&u);
		if(die->qry(s+1,t)>u){
			//max distance is more than u, impossible
			ans[i]=-1;
		}
		else{
			qrys[s].pb({u,i,1});
			
			//answer is cost from s to n+1 - cost from minpos to n+1 + 
			//cost to get from minpos to t, buying from minpos
			int leftpos=lower_bound(x+1,x+n+1,x[t]-u)-x;
			int minpos=getmin->qry(max(s,leftpos),t-1).se;
			ans[i]=(x[t]-x[minpos])*b[minpos];
			qrys[minpos].pb({u,i,-1});
			
			disc.pb(u);
		}
	}
	
	disc.pb(INF);
	sort(all(disc));
	disc.erase(unique(all(disc)),disc.end());

	m->init();c->init();
	b[n+1]=-INF;st.push(n+1);
	
	for(int i=n;i>=1;--i){
		while(b[st.top()]>=b[i]){
			int j=st.top();st.pop();
			
			int dl=get(x[j]-x[i]);
			int dr=get(x[r[j]]-x[i]);
			
			m->up(dl+1,dr,-b[j]);
			c->up(dl+1,dr,(x[j]-x[i])*b[j]);
			c->up(dr+1,maxn-1,-(x[r[j]]-x[j])*b[j]);
		}
		
		r[i]=st.top();
		int d=get(x[r[i]]-x[i]);
		m->up(1,d,b[i]);//x<d:y=b[i]*x
		c->up(d+1,maxn-1,(x[r[i]]-x[i])*b[i]);//x>=d:y=d*b[i]
		
		st.push(i);
		
		for(query q:qrys[i]){
			ans[q.i]+=q.mult*(q.u*m->qry(get(q.u))+c->qry(get(q.u)));
		}
	}
	
	for(int i=0;i<q;++i){
		pf("%lld\n",ans[i]);
	}
}

/*
5 4
3 4 1 1 4
2 5 1 2 1
1 6 3
1 6 4
3 5 1
2 5 9
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:114:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |  sf("%d%d",&n,&q);
      |    ^
Main.cpp:120:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |   sf("%d",&a[i]);
      |     ^
Main.cpp:125:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |   sf("%d",&b[i]);
      |     ^
Main.cpp:130:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |   sf("%d%d%d",&s,&t,&u);
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8896 KB Output is correct
2 Correct 11 ms 8952 KB Output is correct
3 Correct 11 ms 8908 KB Output is correct
4 Correct 11 ms 8908 KB Output is correct
5 Correct 11 ms 8908 KB Output is correct
6 Correct 11 ms 8940 KB Output is correct
7 Correct 12 ms 8908 KB Output is correct
8 Correct 11 ms 8864 KB Output is correct
9 Correct 14 ms 8936 KB Output is correct
10 Correct 16 ms 8908 KB Output is correct
11 Correct 12 ms 8924 KB Output is correct
12 Correct 14 ms 8980 KB Output is correct
13 Correct 11 ms 8908 KB Output is correct
14 Correct 11 ms 8908 KB Output is correct
15 Correct 11 ms 8908 KB Output is correct
16 Correct 12 ms 8908 KB Output is correct
17 Correct 11 ms 8892 KB Output is correct
18 Correct 11 ms 8908 KB Output is correct
19 Correct 11 ms 8908 KB Output is correct
20 Correct 10 ms 8908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 21888 KB Output is correct
2 Correct 147 ms 23232 KB Output is correct
3 Correct 157 ms 23532 KB Output is correct
4 Correct 154 ms 23480 KB Output is correct
5 Correct 152 ms 23276 KB Output is correct
6 Correct 826 ms 69008 KB Output is correct
7 Correct 770 ms 67424 KB Output is correct
8 Correct 874 ms 69620 KB Output is correct
9 Correct 806 ms 70388 KB Output is correct
10 Correct 854 ms 67996 KB Output is correct
11 Correct 864 ms 67168 KB Output is correct
12 Correct 816 ms 69732 KB Output is correct
13 Correct 832 ms 67604 KB Output is correct
14 Correct 855 ms 67376 KB Output is correct
15 Correct 443 ms 64956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 672 ms 60840 KB Output is correct
2 Correct 633 ms 68076 KB Output is correct
3 Correct 614 ms 68200 KB Output is correct
4 Correct 675 ms 66648 KB Output is correct
5 Correct 573 ms 66952 KB Output is correct
6 Correct 598 ms 65912 KB Output is correct
7 Correct 621 ms 65076 KB Output is correct
8 Correct 517 ms 64532 KB Output is correct
9 Correct 551 ms 65268 KB Output is correct
10 Correct 464 ms 66472 KB Output is correct
11 Correct 547 ms 66272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8896 KB Output is correct
2 Correct 11 ms 8952 KB Output is correct
3 Correct 11 ms 8908 KB Output is correct
4 Correct 11 ms 8908 KB Output is correct
5 Correct 11 ms 8908 KB Output is correct
6 Correct 11 ms 8940 KB Output is correct
7 Correct 12 ms 8908 KB Output is correct
8 Correct 11 ms 8864 KB Output is correct
9 Correct 14 ms 8936 KB Output is correct
10 Correct 16 ms 8908 KB Output is correct
11 Correct 12 ms 8924 KB Output is correct
12 Correct 14 ms 8980 KB Output is correct
13 Correct 11 ms 8908 KB Output is correct
14 Correct 11 ms 8908 KB Output is correct
15 Correct 11 ms 8908 KB Output is correct
16 Correct 12 ms 8908 KB Output is correct
17 Correct 11 ms 8892 KB Output is correct
18 Correct 11 ms 8908 KB Output is correct
19 Correct 11 ms 8908 KB Output is correct
20 Correct 10 ms 8908 KB Output is correct
21 Correct 166 ms 21888 KB Output is correct
22 Correct 147 ms 23232 KB Output is correct
23 Correct 157 ms 23532 KB Output is correct
24 Correct 154 ms 23480 KB Output is correct
25 Correct 152 ms 23276 KB Output is correct
26 Correct 826 ms 69008 KB Output is correct
27 Correct 770 ms 67424 KB Output is correct
28 Correct 874 ms 69620 KB Output is correct
29 Correct 806 ms 70388 KB Output is correct
30 Correct 854 ms 67996 KB Output is correct
31 Correct 864 ms 67168 KB Output is correct
32 Correct 816 ms 69732 KB Output is correct
33 Correct 832 ms 67604 KB Output is correct
34 Correct 855 ms 67376 KB Output is correct
35 Correct 443 ms 64956 KB Output is correct
36 Correct 672 ms 60840 KB Output is correct
37 Correct 633 ms 68076 KB Output is correct
38 Correct 614 ms 68200 KB Output is correct
39 Correct 675 ms 66648 KB Output is correct
40 Correct 573 ms 66952 KB Output is correct
41 Correct 598 ms 65912 KB Output is correct
42 Correct 621 ms 65076 KB Output is correct
43 Correct 517 ms 64532 KB Output is correct
44 Correct 551 ms 65268 KB Output is correct
45 Correct 464 ms 66472 KB Output is correct
46 Correct 547 ms 66272 KB Output is correct
47 Correct 952 ms 67420 KB Output is correct
48 Correct 928 ms 68400 KB Output is correct
49 Correct 938 ms 68856 KB Output is correct
50 Correct 990 ms 68068 KB Output is correct
51 Correct 950 ms 67504 KB Output is correct
52 Correct 975 ms 67160 KB Output is correct
53 Correct 865 ms 66136 KB Output is correct
54 Correct 863 ms 65980 KB Output is correct
55 Correct 800 ms 67128 KB Output is correct
56 Correct 495 ms 66004 KB Output is correct
57 Correct 794 ms 67000 KB Output is correct