답안 #445408

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445408 2021-07-18T01:51:56 Z kig9981 수확 (JOI20_harvest) C++17
100 / 100
999 ms 107640 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

typedef __gnu_pbds::tree<pair<long long,int>,__gnu_pbds::null_type,less<pair<long long,int>>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update> ordered_set;
ordered_set S[200000];
vector<pair<long long,int>> qry[200000];
int A[200000], P[200000], cnt[200000], R[200000], PW[200000];
long long ans[200000], O[200000];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, M, L, C;
	queue<int> Q;
	cin>>N>>M>>L>>C;
	for(int i=0;i<N;i++) cin>>A[R[i]=i];
	for(int i=0;i<N;i++) {
		cnt[P[i]=(upper_bound(A,A+N,(L+(A[i]-C)%L)%L)-A+N-1)%N]++;
		PW[i]=C+(L+(A[i]+3LL*L-A[P[i]]-C)%L)%L;
	}
	for(int i=0;i<M;i++) {
		int b, p;
		cin>>b;
		p=(upper_bound(A,A+N,b)-A+N-1)%N;
		S[p].insert({(b-A[p]+L)%L,i});
	}
	cin>>M;
	for(int i=0;i<M;i++) {
		int v;
		long long t;
		cin>>v>>t;
		qry[--v].emplace_back(t,i);
	}
	for(int i=0;i<N;i++) if(cnt[i]==0) Q.push(i);
	while(!Q.empty()) {
		int c=Q.front();
		Q.pop();
		for(auto[t,i]: qry[c]) ans[i]=S[R[c]].order_of_key({t+1-O[c],0});
		O[c]+=PW[c];
		if(S[R[P[c]]].size()<S[R[c]].size()) {
			swap(R[c],R[P[c]]);
			swap(O[c],O[P[c]]);
		}
		for(auto[t,i]: S[R[c]]) S[R[P[c]]].insert({t+O[c]-O[P[c]],i});
		if(--cnt[P[c]]==0) Q.push(P[c]);
	}
	for(int r=0;r<N;r++) if(cnt[r]) {
		vector<int> V;
		ordered_set C;
		long long tot=PW[r], sum=PW[r];
		V.push_back(r); cnt[r]=0;
		for(auto[t,j]: S[R[r]]) C.insert({t+O[r],j});
		for(int i=P[r];cnt[i];i=P[i]) {
			V.push_back(i);
			cnt[i]=0;
			tot+=PW[i];
		}
		for(int i=1;i<V.size();i++) {
			for(auto[t,j]: S[R[V[i]]]) C.insert({t+tot-sum+O[V[i]],j});
			sum+=PW[V[i]];
		}
		sum=0;
		for(auto v: V) {
			for(auto[t,i]: qry[v]) {
				long long rd=t%tot;
				ans[i]=(t/tot+1)*C.order_of_key({rd+1-sum,0});
				for(int m=0;C.size() && m*tot+rd-sum<=C.rbegin()->first;m++) ans[i]+=max(t/tot-m,0LL)*(C.order_of_key({(m+1)*tot+rd+1-sum,0})-C.order_of_key({m*tot+rd+1-sum,0}));
			}
			sum+=PW[v];
			for(auto[t,i]: S[R[P[v]]]) {
				C.erase({t+O[P[v]]+tot-sum,i});
				C.insert({t+O[P[v]]-sum,i});
			}
		}
	}
	for(int i=0;i<M;i++) cout<<ans[i]<<'\n';
	return 0;
}

Compilation message

harvest.cpp: In function 'int main()':
harvest.cpp:72:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for(int i=1;i<V.size();i++) {
      |               ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24012 KB Output is correct
2 Correct 26 ms 24192 KB Output is correct
3 Correct 26 ms 24396 KB Output is correct
4 Correct 26 ms 24840 KB Output is correct
5 Correct 26 ms 24604 KB Output is correct
6 Correct 26 ms 24576 KB Output is correct
7 Correct 25 ms 24648 KB Output is correct
8 Correct 25 ms 24684 KB Output is correct
9 Correct 27 ms 24676 KB Output is correct
10 Correct 27 ms 24668 KB Output is correct
11 Correct 25 ms 24672 KB Output is correct
12 Correct 25 ms 24428 KB Output is correct
13 Correct 26 ms 24512 KB Output is correct
14 Correct 30 ms 24448 KB Output is correct
15 Correct 26 ms 24684 KB Output is correct
16 Correct 25 ms 24796 KB Output is correct
17 Correct 25 ms 24784 KB Output is correct
18 Correct 25 ms 24672 KB Output is correct
19 Correct 26 ms 24572 KB Output is correct
20 Correct 26 ms 24688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 171 ms 36660 KB Output is correct
2 Correct 212 ms 43576 KB Output is correct
3 Correct 215 ms 42056 KB Output is correct
4 Correct 237 ms 42632 KB Output is correct
5 Correct 204 ms 44484 KB Output is correct
6 Correct 235 ms 44908 KB Output is correct
7 Correct 188 ms 43716 KB Output is correct
8 Correct 198 ms 43632 KB Output is correct
9 Correct 258 ms 44700 KB Output is correct
10 Correct 179 ms 43952 KB Output is correct
11 Correct 324 ms 43604 KB Output is correct
12 Correct 288 ms 43556 KB Output is correct
13 Correct 313 ms 43852 KB Output is correct
14 Correct 203 ms 42924 KB Output is correct
15 Correct 262 ms 44588 KB Output is correct
16 Correct 209 ms 44484 KB Output is correct
17 Correct 215 ms 44128 KB Output is correct
18 Correct 161 ms 38244 KB Output is correct
19 Correct 160 ms 38132 KB Output is correct
20 Correct 189 ms 44752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24012 KB Output is correct
2 Correct 26 ms 24192 KB Output is correct
3 Correct 26 ms 24396 KB Output is correct
4 Correct 26 ms 24840 KB Output is correct
5 Correct 26 ms 24604 KB Output is correct
6 Correct 26 ms 24576 KB Output is correct
7 Correct 25 ms 24648 KB Output is correct
8 Correct 25 ms 24684 KB Output is correct
9 Correct 27 ms 24676 KB Output is correct
10 Correct 27 ms 24668 KB Output is correct
11 Correct 25 ms 24672 KB Output is correct
12 Correct 25 ms 24428 KB Output is correct
13 Correct 26 ms 24512 KB Output is correct
14 Correct 30 ms 24448 KB Output is correct
15 Correct 26 ms 24684 KB Output is correct
16 Correct 25 ms 24796 KB Output is correct
17 Correct 25 ms 24784 KB Output is correct
18 Correct 25 ms 24672 KB Output is correct
19 Correct 26 ms 24572 KB Output is correct
20 Correct 26 ms 24688 KB Output is correct
21 Correct 171 ms 36660 KB Output is correct
22 Correct 212 ms 43576 KB Output is correct
23 Correct 215 ms 42056 KB Output is correct
24 Correct 237 ms 42632 KB Output is correct
25 Correct 204 ms 44484 KB Output is correct
26 Correct 235 ms 44908 KB Output is correct
27 Correct 188 ms 43716 KB Output is correct
28 Correct 198 ms 43632 KB Output is correct
29 Correct 258 ms 44700 KB Output is correct
30 Correct 179 ms 43952 KB Output is correct
31 Correct 324 ms 43604 KB Output is correct
32 Correct 288 ms 43556 KB Output is correct
33 Correct 313 ms 43852 KB Output is correct
34 Correct 203 ms 42924 KB Output is correct
35 Correct 262 ms 44588 KB Output is correct
36 Correct 209 ms 44484 KB Output is correct
37 Correct 215 ms 44128 KB Output is correct
38 Correct 161 ms 38244 KB Output is correct
39 Correct 160 ms 38132 KB Output is correct
40 Correct 189 ms 44752 KB Output is correct
41 Correct 679 ms 104884 KB Output is correct
42 Correct 302 ms 53316 KB Output is correct
43 Correct 180 ms 39204 KB Output is correct
44 Correct 896 ms 65860 KB Output is correct
45 Correct 697 ms 80536 KB Output is correct
46 Correct 703 ms 81292 KB Output is correct
47 Correct 794 ms 82900 KB Output is correct
48 Correct 741 ms 82212 KB Output is correct
49 Correct 718 ms 82308 KB Output is correct
50 Correct 770 ms 80476 KB Output is correct
51 Correct 705 ms 79700 KB Output is correct
52 Correct 592 ms 67944 KB Output is correct
53 Correct 625 ms 69728 KB Output is correct
54 Correct 587 ms 68132 KB Output is correct
55 Correct 622 ms 67428 KB Output is correct
56 Correct 912 ms 86788 KB Output is correct
57 Correct 804 ms 87620 KB Output is correct
58 Correct 905 ms 89212 KB Output is correct
59 Correct 894 ms 88300 KB Output is correct
60 Correct 867 ms 88276 KB Output is correct
61 Correct 857 ms 88388 KB Output is correct
62 Correct 559 ms 64412 KB Output is correct
63 Correct 781 ms 74604 KB Output is correct
64 Correct 826 ms 74820 KB Output is correct
65 Correct 864 ms 75420 KB Output is correct
66 Correct 843 ms 73184 KB Output is correct
67 Correct 792 ms 73860 KB Output is correct
68 Correct 801 ms 72548 KB Output is correct
69 Correct 628 ms 103876 KB Output is correct
70 Correct 582 ms 98324 KB Output is correct
71 Correct 738 ms 104900 KB Output is correct
72 Correct 999 ms 107640 KB Output is correct