Submission #216316

# Submission time Handle Problem Language Result Execution time Memory
216316 2020-03-27T05:12:30 Z model_code Harvest (JOI20_harvest) C++17
100 / 100
1183 ms 100932 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>

class Segment_Tree{
	public:
	int n;
	std::vector<long long> data;
	void Init(int n_){
		n=1;
		while(n<n_) n*=2;
		data=std::vector<long long>(2*n-1);
	}
	void Update(int k){
		k+=n-1;
		data[k]++;
		while(k>0){
			k=(k-1)/2;
			data[k]=data[k*2+1]+data[k*2+2];
		}
	}
	long long Query(int a,int b){
		a+=n-1;b+=n-1;
		long long m=0;
		while(a<b){
			if(a%2==0) m+=data[a++];
			if(b%2==0) m+=data[--b];
			a/=2;b/=2;
		}
		return m;
	}
};

int N,M,L,C,Q;
std::vector<int> A;

std::vector<int> nx,right;
std::vector<std::vector<int>> rev;
std::vector<long long> cost;
std::vector<std::vector<long long>> a;
std::vector<std::vector<std::pair<long long,int>>> c;

void dfs(int v,long long dep,int rt,int &id,std::vector<std::pair<long long,std::pair<int,int>>> &query){
	int pre=id;
	for(auto i:a[v]){
		a[nx[rt]].push_back(i+dep+cost[rt]);
		query.push_back(std::make_pair(i+dep,std::make_pair(-1,id)));
	}
	for(auto p:c[v]) query.push_back(std::make_pair(p.first+dep,std::make_pair(p.second,id)));
	for(auto u:rev[v]){
		id++;
		dfs(u,dep+cost[u],rt,id,query);
	}
	right[pre]=id;
}

int main(){
	scanf("%d%d%d%d",&N,&M,&L,&C);
	A=std::vector<int>(N);
	for(int i=0;i<N;i++) scanf("%d",&A[i]);
	int id=0;
	right=nx=std::vector<int>(N);
	rev=std::vector<std::vector<int>>(N);
	cost=std::vector<long long>(N);
	a=std::vector<std::vector<long long>>(N);
	std::vector<int> deg(N),vis(N);
	for(int i=0;i<N;i++){
		int x=(A[i]-C%L+L)%L;
		auto it=std::upper_bound(A.begin(),A.end(),x);
		nx[i]=(it==A.begin()?N-1:it-A.begin()-1);
		rev[nx[i]].push_back(i);
		cost[i]=C+(x-A[nx[i]]+L)%L;
		deg[nx[i]]++;
	}
	std::queue<int> que;
	for(int i=0;i<N;i++) if(!deg[i]) que.push(i);
	while(!que.empty()){
		int v=que.front(),u=nx[v];
		que.pop();
		deg[u]--;
		if(!deg[u]) que.push(u);
	}
	for(int i=0;i<M;i++){
		int x;
		scanf("%d",&x);
		while(id<N&&A[id]<x) id++;
		int v=(id-1+N)%N;
		long long t=(x-A[v]+L)%L;
		a[v].push_back(t);
	}
	scanf("%d",&Q);
	std::vector<long long> res(Q);
	c=std::vector<std::vector<std::pair<long long,int>>>(N);
	for(int i=0;i<Q;i++){
		int v;
		long long t;
		scanf("%d%lld",&v,&t);
		v--;
		c[v].push_back(std::make_pair(t,i));
	}
	for(int i=0;i<N;i++) if(!vis[i]&&deg[i]){
		int v=i;
		long long cycle=0,dis=0;
		Segment_Tree st;
		do{
			cycle+=cost[v];
			vis[v]++;
			for(auto j:rev[v]) if(!deg[j]){
				std::vector<std::pair<long long,std::pair<int,int>>> query;
				int sz=0;
				dfs(j,0,j,sz,query);
				std::sort(query.begin(),query.end());
				st.Init(sz+1);
				for(auto pp:query){
					auto p=pp.second;
					int ind=p.second;
					if(p.first>=0) res[p.first]=st.Query(ind,right[ind]+1);
					else st.Update(ind);
				}
			}
			v=nx[v];
		}while(v!=i);
		std::vector<std::pair<long long,int>> query;
		std::vector<long long> b;
		std::map<long long,int> mp;
		do{
			for(auto j:a[v]){
				query.push_back(std::make_pair(j-dis+cycle,-1));
				b.push_back((j-dis+cycle)%cycle);
			}
			for(auto p:c[v]) if(p.first>=dis){
				query.push_back(std::make_pair(p.first-dis,p.second));
				b.push_back((p.first-dis)%cycle);
			}
			dis+=cost[v];
			v=nx[v];
		}while(v!=i);
		std::sort(query.begin(),query.end());
		std::sort(b.begin(),b.end());
		b.erase(std::unique(b.begin(),b.end()),b.end());
		int sz=b.size();
		for(int i=0;i<sz;i++) mp[b[i]]=i;
		st.Init(sz);
		long long sum=0;
		for(auto p:query){
			int j=mp[p.first%cycle];
			long long tmp=p.first/cycle;
			if(p.second>=0) res[p.second]=st.Query(0,j+1)*(tmp+1)+st.Query(j+1,sz)*tmp-sum;
			else{
				st.Update(j);
				sum+=tmp;
			}
		}
		b.clear();
		mp.clear();
		dis=0;
		do{
			for(auto j:a[v]) b.push_back(j-dis);
			for(auto p:c[v]) b.push_back(p.first-dis);
			dis+=cost[v];
			v=nx[v];
		}while(v!=i);
		std::sort(b.begin(),b.end());
		b.erase(std::unique(b.begin(),b.end()),b.end());
		sz=b.size();
		for(int i=0;i<sz;i++) mp[b[i]]=i;
		st.Init(sz);
		dis=0;
		do{
			for(auto j:a[v]) st.Update(mp[j-dis]);
			for(auto p:c[v]){
				res[p.second]+=st.Query(0,mp[p.first-dis]+1);
			}
			dis+=cost[v];
			v=nx[v];
		}while(v!=i);
	}
	for(int i=0;i<Q;i++) printf("%lld\n",res[i]);
}

Compilation message

harvest.cpp: In function 'int main()':
harvest.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d",&N,&M,&L,&C);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harvest.cpp:62:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0;i<N;i++) scanf("%d",&A[i]);
                       ~~~~~^~~~~~~~~~~~
harvest.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x);
   ~~~~~^~~~~~~~~
harvest.cpp:93:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&Q);
  ~~~~~^~~~~~~~~
harvest.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%lld",&v,&t);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 896 KB Output is correct
2 Correct 10 ms 1024 KB Output is correct
3 Correct 13 ms 1792 KB Output is correct
4 Correct 13 ms 1432 KB Output is correct
5 Correct 11 ms 1920 KB Output is correct
6 Correct 13 ms 1920 KB Output is correct
7 Correct 12 ms 1920 KB Output is correct
8 Correct 13 ms 1332 KB Output is correct
9 Correct 10 ms 1280 KB Output is correct
10 Correct 10 ms 1440 KB Output is correct
11 Correct 10 ms 1312 KB Output is correct
12 Correct 11 ms 1664 KB Output is correct
13 Correct 14 ms 1792 KB Output is correct
14 Correct 14 ms 1408 KB Output is correct
15 Correct 11 ms 1664 KB Output is correct
16 Correct 11 ms 1664 KB Output is correct
17 Correct 11 ms 1664 KB Output is correct
18 Correct 11 ms 1568 KB Output is correct
19 Correct 11 ms 1568 KB Output is correct
20 Correct 10 ms 1696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 652 ms 33352 KB Output is correct
2 Correct 281 ms 41168 KB Output is correct
3 Correct 329 ms 40588 KB Output is correct
4 Correct 806 ms 64260 KB Output is correct
5 Correct 326 ms 75744 KB Output is correct
6 Correct 339 ms 75768 KB Output is correct
7 Correct 259 ms 35436 KB Output is correct
8 Correct 267 ms 42216 KB Output is correct
9 Correct 849 ms 65748 KB Output is correct
10 Correct 761 ms 63876 KB Output is correct
11 Correct 937 ms 65792 KB Output is correct
12 Correct 913 ms 65936 KB Output is correct
13 Correct 941 ms 65840 KB Output is correct
14 Correct 770 ms 64004 KB Output is correct
15 Correct 680 ms 53108 KB Output is correct
16 Correct 298 ms 61544 KB Output is correct
17 Correct 306 ms 61540 KB Output is correct
18 Correct 193 ms 26088 KB Output is correct
19 Correct 190 ms 26088 KB Output is correct
20 Correct 216 ms 41348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 896 KB Output is correct
2 Correct 10 ms 1024 KB Output is correct
3 Correct 13 ms 1792 KB Output is correct
4 Correct 13 ms 1432 KB Output is correct
5 Correct 11 ms 1920 KB Output is correct
6 Correct 13 ms 1920 KB Output is correct
7 Correct 12 ms 1920 KB Output is correct
8 Correct 13 ms 1332 KB Output is correct
9 Correct 10 ms 1280 KB Output is correct
10 Correct 10 ms 1440 KB Output is correct
11 Correct 10 ms 1312 KB Output is correct
12 Correct 11 ms 1664 KB Output is correct
13 Correct 14 ms 1792 KB Output is correct
14 Correct 14 ms 1408 KB Output is correct
15 Correct 11 ms 1664 KB Output is correct
16 Correct 11 ms 1664 KB Output is correct
17 Correct 11 ms 1664 KB Output is correct
18 Correct 11 ms 1568 KB Output is correct
19 Correct 11 ms 1568 KB Output is correct
20 Correct 10 ms 1696 KB Output is correct
21 Correct 652 ms 33352 KB Output is correct
22 Correct 281 ms 41168 KB Output is correct
23 Correct 329 ms 40588 KB Output is correct
24 Correct 806 ms 64260 KB Output is correct
25 Correct 326 ms 75744 KB Output is correct
26 Correct 339 ms 75768 KB Output is correct
27 Correct 259 ms 35436 KB Output is correct
28 Correct 267 ms 42216 KB Output is correct
29 Correct 849 ms 65748 KB Output is correct
30 Correct 761 ms 63876 KB Output is correct
31 Correct 937 ms 65792 KB Output is correct
32 Correct 913 ms 65936 KB Output is correct
33 Correct 941 ms 65840 KB Output is correct
34 Correct 770 ms 64004 KB Output is correct
35 Correct 680 ms 53108 KB Output is correct
36 Correct 298 ms 61544 KB Output is correct
37 Correct 306 ms 61540 KB Output is correct
38 Correct 193 ms 26088 KB Output is correct
39 Correct 190 ms 26088 KB Output is correct
40 Correct 216 ms 41348 KB Output is correct
41 Correct 548 ms 69348 KB Output is correct
42 Correct 385 ms 43000 KB Output is correct
43 Correct 582 ms 62476 KB Output is correct
44 Correct 917 ms 90584 KB Output is correct
45 Correct 493 ms 99292 KB Output is correct
46 Correct 513 ms 100124 KB Output is correct
47 Correct 554 ms 100932 KB Output is correct
48 Correct 524 ms 99028 KB Output is correct
49 Correct 518 ms 99280 KB Output is correct
50 Correct 469 ms 66148 KB Output is correct
51 Correct 467 ms 65624 KB Output is correct
52 Correct 908 ms 93636 KB Output is correct
53 Correct 1183 ms 94804 KB Output is correct
54 Correct 893 ms 93756 KB Output is correct
55 Correct 705 ms 88976 KB Output is correct
56 Correct 595 ms 89432 KB Output is correct
57 Correct 594 ms 86168 KB Output is correct
58 Correct 605 ms 86972 KB Output is correct
59 Correct 570 ms 84328 KB Output is correct
60 Correct 563 ms 84952 KB Output is correct
61 Correct 576 ms 85056 KB Output is correct
62 Correct 1097 ms 71528 KB Output is correct
63 Correct 467 ms 52308 KB Output is correct
64 Correct 442 ms 52396 KB Output is correct
65 Correct 469 ms 52544 KB Output is correct
66 Correct 526 ms 52296 KB Output is correct
67 Correct 468 ms 52244 KB Output is correct
68 Correct 480 ms 51652 KB Output is correct
69 Correct 564 ms 71780 KB Output is correct
70 Correct 517 ms 68516 KB Output is correct
71 Correct 549 ms 69588 KB Output is correct
72 Correct 545 ms 69628 KB Output is correct