Submission #292166

# Submission time Handle Problem Language Result Execution time Memory
292166 2020-09-06T13:12:45 Z TadijaSebez Harvest (JOI20_harvest) C++11
100 / 100
4216 ms 290000 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pli pair<ll,int>
#define pb push_back
const int N=400050;
int a[N],b[N],go[N],len[N*2],was[N],csz,on_chn[N*2],n,m,l,c,h,lid[N*2],rid[N*2],tid;
ll dep[N*2],chn_len[N],my_chn_len[N],ans[N];
vector<int> E[N*2];
void DFS(int u,ll cl){
	dep[u]+=len[u];
	lid[u]=tid+1;
	if(u>n&&u<=h||u>h+n&&u<=2*h)tid++,my_chn_len[tid]=cl;
	for(int v:E[u])if(!on_chn[v])dep[v]=dep[u],DFS(v,cl);
	rid[u]=tid;
}
namespace Treap{
	const int M=N*20;
	mt19937 rng(time(0));
	int ls[M],rs[M],tsz,sz[M],pri[M];
	ll key[M];
	void pull(int c){sz[c]=sz[ls[c]]+1+sz[rs[c]];}
	int make(ll x){tsz++;sz[tsz]=1;key[tsz]=x;pri[tsz]=rng();return tsz;}
	void rot_l(int&c){int a=rs[c],b=ls[a];rs[c]=b;ls[a]=c;pull(c);pull(a);c=a;}
	void rot_r(int&c){int a=ls[c],b=rs[a];ls[c]=b;rs[a]=c;pull(c);pull(a);c=a;}
	void Ins(int&c,ll x){
		if(!c)c=make(x);
		else if(key[c]<x){
			Ins(rs[c],x);
			if(pri[rs[c]]>pri[c])rot_l(c);
			else pull(c);
		}else{
			Ins(ls[c],x);
			if(pri[ls[c]]>pri[c])rot_r(c);
			else pull(c);
		}
	}
	int Lo(int c,ll x){
		if(!c)return 0;
		if(key[c]<=x)return Lo(rs[c],x)+sz[ls[c]]+1;
		else return Lo(ls[c],x);
	}
}
const int M=2*N;
int ls[M],rs[M],tsz,root,sz[M];
int treap[M];
ll sub[M];
void Set(int&c,int ss,int se,int qi,ll x,ll y){
	if(!c)c=++tsz;
	sz[c]++;
	sub[c]+=x;
	Treap::Ins(treap[c],y);
	if(ss==se)return;
	int mid=ss+se>>1;
	if(qi<=mid)Set(ls[c],ss,mid,qi,x,y);
	else Set(rs[c],mid+1,se,qi,x,y);
}
int GetCnt(int c,int ss,int se,int qs,int qe){
	if(qs>qe||qs>se||ss>qe)return 0;
	if(qs<=ss&&qe>=se)return sz[c];
	int mid=ss+se>>1;
	return GetCnt(ls[c],ss,mid,qs,qe)+GetCnt(rs[c],mid+1,se,qs,qe);
}
ll Get(int c,int ss,int se,int qs,int qe){
	if(qs>qe||qs>se||ss>qe)return 0;
	if(qs<=ss&&qe>=se)return sub[c];
	int mid=ss+se>>1;
	return Get(ls[c],ss,mid,qs,qe)+Get(rs[c],mid+1,se,qs,qe);
}
int Lo(int c,int ss,int se,int qs,int qe,ll y){
	if(qs>qe||qs>se||ss>qe)return 0;
	if(qs<=ss&&qe>=se)return Treap::Lo(treap[c],y);
	int mid=ss+se>>1;
	return Lo(ls[c],ss,mid,qs,qe,y)+Lo(rs[c],mid+1,se,qs,qe,y);
}
void Add(pli p){
	Set(root,1,tid,p.second,p.first/my_chn_len[p.second],p.first%my_chn_len[p.second]);
}
int Count(int l,int r){return GetCnt(root,1,tid,l,r);}
ll Get(int l,int r){return Get(root,1,tid,l,r);}
int Lo(int l,int r,ll y){return Lo(root,1,tid,l,r,y);}
int q,V[N],ord[N];
ll T[N];
int main(){
	scanf("%i %i %i %i",&n,&m,&l,&c);
	for(int i=1;i<=n;i++)scanf("%i",&a[i]),a[i]=l-a[i];
	reverse(a+1,a+1+n);
	for(int i=1;i<=n;i++)a[i+n]=a[i]+l;
	for(int i=1;i<=n;i++){
		int j=lower_bound(a+i,a+i+1+n,a[i]+c%l)-a;
		len[i]=c/l*l+a[j]-a[i];
		if(j>n)j-=n;
		go[i]=j;
	}
	for(int i=1;i<=m;i++){
		scanf("%i",&b[i]);
		b[i]=l-b[i];
		int j=lower_bound(a+1,a+1+2*n,b[i])-a;
		len[n+i]=a[j]-b[i];
		go[n+i]=j>n?j-n:j;
	}
	h=n+m;
	for(int i=1;i<=h;i++){
		E[go[i]].pb(i);
		E[go[i]+h].pb(i+h);
		len[i+h]=len[i];
	}
	for(int i=1;i<=n;i++)if(!was[i]){
		int j=i;csz++;
		while(!was[j])was[j]=csz,j=go[j];
		if(was[j]!=csz)continue;
		int k=j;
		vector<int> chn;
		do{
			chn.pb(k);
			on_chn[k]=on_chn[k+h]=1;
			chn_len[csz]+=len[k];
			k=go[k];
		}while(k!=j);
		reverse(chn.begin(),chn.end());
		ll sum=0;
		for(int k:chn){
			dep[k]=sum;
			DFS(k,chn_len[csz]);
			sum+=len[k];
		}
		for(int k:chn){
			dep[k+h]=sum;
			DFS(k+h,chn_len[csz]);
			sum+=len[k];
		}
	}
	vector<pli> leaf;
	for(int i=n+1;i<=h;i++){
		leaf.pb({dep[i],lid[i]});
		leaf.pb({dep[i+h],lid[i+h]});
	}
	sort(leaf.begin(),leaf.end());
	scanf("%i",&q);
	for(int i=1;i<=q;i++){
		scanf("%i %lld",&V[i],&T[i]);
		V[i]=n-V[i]+1;
		T[i]+=dep[V[i]];
		ord[i]=i;
	}
	sort(ord+1,ord+1+q,[&](int i,int j){return T[i]<T[j];});
	for(int z=1,j=0;z<=q;z++){
		int i=ord[z];
		while(j<leaf.size()&&leaf[j].first<=T[i])Add(leaf[j]),j++;
		if(on_chn[V[i]]){
			int L=lid[V[i]],R=lid[V[i]+h]-1;
			int cnt=Count(L,R);
			ll now=cnt*(T[i]/chn_len[was[V[i]]])-Get(L,R);
			now+=Lo(L,R,T[i]%chn_len[was[V[i]]]);
			ans[i]=now;
		}else{
			ans[i]=Count(lid[V[i]],rid[V[i]]);
		}
	}
	for(int i=1;i<=q;i++)printf("%lld\n",ans[i]);
	return 0;
}

Compilation message

harvest.cpp: In function 'void DFS(int, long long int)':
harvest.cpp:13:8: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   13 |  if(u>n&&u<=h||u>h+n&&u<=2*h)tid++,my_chn_len[tid]=cl;
      |     ~~~^~~~~~
harvest.cpp: In function 'void Set(int&, int, int, int, long long int, long long int)':
harvest.cpp:54:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   54 |  int mid=ss+se>>1;
      |          ~~^~~
harvest.cpp: In function 'int GetCnt(int, int, int, int, int)':
harvest.cpp:61:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |  int mid=ss+se>>1;
      |          ~~^~~
harvest.cpp: In function 'long long int Get(int, int, int, int, int)':
harvest.cpp:67:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |  int mid=ss+se>>1;
      |          ~~^~~
harvest.cpp: In function 'int Lo(int, int, int, int, int, long long int)':
harvest.cpp:73:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |  int mid=ss+se>>1;
      |          ~~^~~
harvest.cpp: In function 'int main()':
harvest.cpp:149:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |   while(j<leaf.size()&&leaf[j].first<=T[i])Add(leaf[j]),j++;
      |         ~^~~~~~~~~~~~
harvest.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |  scanf("%i %i %i %i",&n,&m,&l,&c);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
harvest.cpp:86:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |  for(int i=1;i<=n;i++)scanf("%i",&a[i]),a[i]=l-a[i];
      |                       ~~~~~^~~~~~~~~~~~
harvest.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   96 |   scanf("%i",&b[i]);
      |   ~~~~~^~~~~~~~~~~~
harvest.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  139 |  scanf("%i",&q);
      |  ~~~~~^~~~~~~~~
harvest.cpp:141:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  141 |   scanf("%i %lld",&V[i],&T[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19456 KB Output is correct
2 Correct 30 ms 22520 KB Output is correct
3 Correct 26 ms 22528 KB Output is correct
4 Correct 29 ms 22272 KB Output is correct
5 Correct 27 ms 22528 KB Output is correct
6 Correct 27 ms 22528 KB Output is correct
7 Correct 28 ms 22560 KB Output is correct
8 Correct 26 ms 22400 KB Output is correct
9 Correct 22 ms 21248 KB Output is correct
10 Correct 27 ms 22392 KB Output is correct
11 Correct 22 ms 21248 KB Output is correct
12 Correct 24 ms 21376 KB Output is correct
13 Correct 32 ms 22528 KB Output is correct
14 Correct 36 ms 22528 KB Output is correct
15 Correct 27 ms 22528 KB Output is correct
16 Correct 28 ms 22528 KB Output is correct
17 Correct 27 ms 22468 KB Output is correct
18 Correct 28 ms 22400 KB Output is correct
19 Correct 27 ms 22528 KB Output is correct
20 Correct 27 ms 22400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 227 ms 30420 KB Output is correct
2 Correct 304 ms 48504 KB Output is correct
3 Correct 293 ms 53972 KB Output is correct
4 Correct 323 ms 56280 KB Output is correct
5 Correct 314 ms 60664 KB Output is correct
6 Correct 271 ms 60792 KB Output is correct
7 Correct 233 ms 40032 KB Output is correct
8 Correct 254 ms 40032 KB Output is correct
9 Correct 644 ms 54736 KB Output is correct
10 Correct 269 ms 54676 KB Output is correct
11 Correct 727 ms 60240 KB Output is correct
12 Correct 721 ms 60376 KB Output is correct
13 Correct 722 ms 60368 KB Output is correct
14 Correct 319 ms 60236 KB Output is correct
15 Correct 652 ms 61392 KB Output is correct
16 Correct 319 ms 62456 KB Output is correct
17 Correct 269 ms 62456 KB Output is correct
18 Correct 204 ms 38264 KB Output is correct
19 Correct 191 ms 38392 KB Output is correct
20 Correct 237 ms 52728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19456 KB Output is correct
2 Correct 30 ms 22520 KB Output is correct
3 Correct 26 ms 22528 KB Output is correct
4 Correct 29 ms 22272 KB Output is correct
5 Correct 27 ms 22528 KB Output is correct
6 Correct 27 ms 22528 KB Output is correct
7 Correct 28 ms 22560 KB Output is correct
8 Correct 26 ms 22400 KB Output is correct
9 Correct 22 ms 21248 KB Output is correct
10 Correct 27 ms 22392 KB Output is correct
11 Correct 22 ms 21248 KB Output is correct
12 Correct 24 ms 21376 KB Output is correct
13 Correct 32 ms 22528 KB Output is correct
14 Correct 36 ms 22528 KB Output is correct
15 Correct 27 ms 22528 KB Output is correct
16 Correct 28 ms 22528 KB Output is correct
17 Correct 27 ms 22468 KB Output is correct
18 Correct 28 ms 22400 KB Output is correct
19 Correct 27 ms 22528 KB Output is correct
20 Correct 27 ms 22400 KB Output is correct
21 Correct 227 ms 30420 KB Output is correct
22 Correct 304 ms 48504 KB Output is correct
23 Correct 293 ms 53972 KB Output is correct
24 Correct 323 ms 56280 KB Output is correct
25 Correct 314 ms 60664 KB Output is correct
26 Correct 271 ms 60792 KB Output is correct
27 Correct 233 ms 40032 KB Output is correct
28 Correct 254 ms 40032 KB Output is correct
29 Correct 644 ms 54736 KB Output is correct
30 Correct 269 ms 54676 KB Output is correct
31 Correct 727 ms 60240 KB Output is correct
32 Correct 721 ms 60376 KB Output is correct
33 Correct 722 ms 60368 KB Output is correct
34 Correct 319 ms 60236 KB Output is correct
35 Correct 652 ms 61392 KB Output is correct
36 Correct 319 ms 62456 KB Output is correct
37 Correct 269 ms 62456 KB Output is correct
38 Correct 204 ms 38264 KB Output is correct
39 Correct 191 ms 38392 KB Output is correct
40 Correct 237 ms 52728 KB Output is correct
41 Correct 1254 ms 191484 KB Output is correct
42 Correct 2286 ms 280068 KB Output is correct
43 Correct 249 ms 58872 KB Output is correct
44 Correct 899 ms 181496 KB Output is correct
45 Correct 1577 ms 286432 KB Output is correct
46 Correct 1628 ms 287184 KB Output is correct
47 Correct 1660 ms 288720 KB Output is correct
48 Correct 1915 ms 287708 KB Output is correct
49 Correct 1914 ms 290000 KB Output is correct
50 Correct 1545 ms 273736 KB Output is correct
51 Correct 1499 ms 273752 KB Output is correct
52 Correct 2537 ms 279732 KB Output is correct
53 Correct 4104 ms 282552 KB Output is correct
54 Correct 2488 ms 280068 KB Output is correct
55 Correct 1245 ms 177724 KB Output is correct
56 Correct 1696 ms 281808 KB Output is correct
57 Correct 1702 ms 282676 KB Output is correct
58 Correct 1657 ms 284496 KB Output is correct
59 Correct 1889 ms 275980 KB Output is correct
60 Correct 1891 ms 278396 KB Output is correct
61 Correct 2017 ms 283344 KB Output is correct
62 Correct 4216 ms 282412 KB Output is correct
63 Correct 1652 ms 259412 KB Output is correct
64 Correct 1651 ms 259736 KB Output is correct
65 Correct 1673 ms 260124 KB Output is correct
66 Correct 1620 ms 260952 KB Output is correct
67 Correct 1666 ms 260448 KB Output is correct
68 Correct 1664 ms 259924 KB Output is correct
69 Correct 1843 ms 278016 KB Output is correct
70 Correct 1055 ms 186320 KB Output is correct
71 Correct 2128 ms 275924 KB Output is correct
72 Correct 2530 ms 276088 KB Output is correct