답안 #1068590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1068590 2024-08-21T10:49:21 Z AdamGS 수확 (JOI20_harvest) C++17
5 / 100
5000 ms 62248 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=4e5+7;
vector<ll>S[LIM];
vector<pair<ll,ll>>V[LIM], pyt[LIM], pyt2[LIM];
vector<pair<pair<ll,ll>,ll>>M;
ll A[LIM], B[LIM], n, m, L, C;
pair<ll,ll>T[2*LIM];
ll nxt[LIM], kraw[LIM], odw[LIM], odl[LIM], cykl[LIM], pre[LIM], post[LIM], wynik[LIM], lpre=-1;
ll korzen[LIM], tr[4*LIM], suma[LIM], N=1;
void upd(int v, ll x) {
	v+=N;
	while(v) {
		tr[v]+=x;
		v/=2;
	}
}
ll cnt(int l, int r) {
	if(l>r) return 0;
	l+=N; r+=N;
	ll ans=tr[l];
	if(l!=r) ans+=tr[r];
	while(l/2!=r/2) {
		if(l%2==0) ans+=tr[l+1];
		if(r%2==1) ans+=tr[r-1];
		l/=2; r/=2;
	}
	return ans;
}
void DFS(int x) {
	pre[x]=++lpre;
	for(auto i : V[x]) {
		odl[i.st]=odl[x]+i.nd;
		korzen[i.st]=korzen[x];
		DFS(i.st);
	}
	post[x]=lpre;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m >> L >> C;
	while(N<n) N*=2;
	rep(i, n) cin >> A[i];
	rep(i, m) cin >> B[i];
	ll l=n-1, d=0;
	for(int i=n-1; i>=0; --i) {
		while(d<C%L) {
			d+=(A[l]-A[(l-1+n)%n]+L)%L;
			l=(l-1+n)%n;
		}
		nxt[i]=l;
		kraw[i]=d;
		if(d<C) kraw[i]+=((C-d+L-1)/L)*L;
		d-=(A[i]-A[(i-1+n)%n]+L)%L;
	}
	rep(i, n) korzen[i]=-1;
	rep(i, n) if(!odw[i]) {
		int p=i;
		while(!odw[p]) {
			odw[p]=i+1;
			p=nxt[p];
		}
		if(odw[p]==i+1) {
			korzen[p]=p;
			int q=p;
			do {
				cykl[q]=1;
				q=nxt[q];
			} while(q!=p);
		}
	}
	rep(i, n) if(korzen[i]!=i) V[nxt[i]].pb({i, kraw[i]});
	rep(i, n) if(korzen[i]==i) DFS(i);
	int q;
	cin >> q;
	rep(i, q) {
		ll v, t;
		cin >> v >> t; --v;
		pyt[v].pb({t, i});
	}
	rep(i, n) T[i]={A[i], i};
	rep(i, m) T[n+i]={B[i], n+i};
	sort(T, T+n+m);
	int lst=0;
	rep(i, n+m) if(T[i].nd<n) lst=T[i].nd;
	rep(i, n+m) {
		if(T[i].nd<n) lst=T[i].nd;
		else {
			S[korzen[lst]].pb(odl[lst]+(T[i].st-A[lst]+L)%L);
			M.pb({{odl[lst]+(T[i].st-A[lst]+L)%L, -1}, lst});
		}
	}
	rep(i, n) if(korzen[i]!=i) {
		for(auto j : pyt[i]) M.pb({{odl[i]+j.st, j.nd}, i});
	}
	sort(all(M));
	for(auto i : M) {
		if(i.st.nd==-1) {
			upd(pre[i.nd], 1);
		} else {
			wynik[i.st.nd]=cnt(pre[i.nd], post[i.nd]);
		}
	}
	rep(i, n) if(cykl[i]) suma[korzen[i]]+=kraw[i];
	rep(i, n) if(cykl[i]) {
		ll m=suma[korzen[i]], d=(m-odl[i])%m;
		for(auto j : pyt[i]) {
			pyt2[korzen[i]].pb({j.st-d, j.nd});
		}
	}
	rep(i, n) if(korzen[i]==i && S[i].size()) {
		ll m=suma[i];
		vector<ll>K;
		for(auto j : S[i]) K.pb(j%m);
		sort(all(K));
		sort(all(S[i]));
		sort(all(pyt2[i]));
		l=0;
		ll ilek=0, sumk=0;
		N=1;
		while(N<K.size()) N*=2;
		rep(j, 2*N) tr[j]=0;
		for(auto j : pyt2[i]) {
			while(l<S[i].size() && S[i][l]<=j.st) {
				++ilek;
				sumk+=S[i][l]/m;
				ll po=0, ko=K.size()-1;
				while(po<ko) {
					ll sr=(po+ko+1)/2;
					if(K[sr]>S[i][l]%m) ko=sr-1; else po=sr;
				}
				upd(po, 1);
				++l;
			}
			ll po=0, ko=K.size()-1;
			while(po<ko) {
				ll sr=(po+ko+1)/2;
				if(K[sr]>j.st%m) ko=sr-1; else po=sr;
			}
			if(K[po]<=j.st%m) wynik[j.nd]+=cnt(0, po);
			wynik[j.nd]+=ilek*(j.st/m);
			wynik[j.nd]-=sumk;
		}
	}
	rep(i, q) cout << wynik[i] << '\n';
}

Compilation message

harvest.cpp: In function 'int main()':
harvest.cpp:127:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |   while(N<K.size()) N*=2;
      |         ~^~~~~~~~~
harvest.cpp:130:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |    while(l<S[i].size() && S[i][l]<=j.st) {
      |          ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 59484 KB Output is correct
2 Correct 12 ms 59740 KB Output is correct
3 Correct 11 ms 59844 KB Output is correct
4 Correct 14 ms 61976 KB Output is correct
5 Correct 13 ms 62124 KB Output is correct
6 Correct 11 ms 62040 KB Output is correct
7 Correct 14 ms 62184 KB Output is correct
8 Correct 10 ms 61844 KB Output is correct
9 Correct 13 ms 59832 KB Output is correct
10 Correct 11 ms 61996 KB Output is correct
11 Correct 11 ms 61996 KB Output is correct
12 Correct 12 ms 62044 KB Output is correct
13 Correct 15 ms 62248 KB Output is correct
14 Correct 15 ms 62032 KB Output is correct
15 Correct 11 ms 60124 KB Output is correct
16 Correct 11 ms 62044 KB Output is correct
17 Correct 15 ms 62040 KB Output is correct
18 Correct 10 ms 61996 KB Output is correct
19 Correct 12 ms 61876 KB Output is correct
20 Correct 10 ms 61996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5064 ms 42844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 59484 KB Output is correct
2 Correct 12 ms 59740 KB Output is correct
3 Correct 11 ms 59844 KB Output is correct
4 Correct 14 ms 61976 KB Output is correct
5 Correct 13 ms 62124 KB Output is correct
6 Correct 11 ms 62040 KB Output is correct
7 Correct 14 ms 62184 KB Output is correct
8 Correct 10 ms 61844 KB Output is correct
9 Correct 13 ms 59832 KB Output is correct
10 Correct 11 ms 61996 KB Output is correct
11 Correct 11 ms 61996 KB Output is correct
12 Correct 12 ms 62044 KB Output is correct
13 Correct 15 ms 62248 KB Output is correct
14 Correct 15 ms 62032 KB Output is correct
15 Correct 11 ms 60124 KB Output is correct
16 Correct 11 ms 62044 KB Output is correct
17 Correct 15 ms 62040 KB Output is correct
18 Correct 10 ms 61996 KB Output is correct
19 Correct 12 ms 61876 KB Output is correct
20 Correct 10 ms 61996 KB Output is correct
21 Execution timed out 5064 ms 42844 KB Time limit exceeded
22 Halted 0 ms 0 KB -