Submission #99894

# Submission time Handle Problem Language Result Execution time Memory
99894 2019-03-08T13:21:35 Z TadijaSebez New Home (APIO18_new_home) C++11
47 / 100
5000 ms 662536 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mt make_tuple
const int N=300050;
const int M=4*N;
const int lim=1e8;
int x[N],t[N],l[N],r[N],qx[N],qy[N],active,ans[N];
set<pair<int,int>> all[N];
map<pair<int,int>,int> T[N];
vector<pair<pair<int,int>,pair<int,int>>> L,R;
void Put(int t, int l, int r, int ins, int tme)
{
	int mid=l+r>>1;
	if(l>r) return;
	if(ins==0)
	{
		if(l!=-2*lim) T[t][mp(l,mid)]=tme;
		if(r!=2*lim) T[t][mp(mid+1,r)]=tme;
	}
	else
	{
		if(l!=-2*lim) L.pb(mp(mp(T[t][mp(l,mid)],tme-1),mp(l,mid)));
		if(r!=2*lim) R.pb(mp(mp(T[t][mp(mid+1,r)],tme-1),mp(mid+1,r)));
	}
}
int root,ls[M],rs[M],tsz;
vector<pair<int,int>> LST,RST;
vector<pair<pair<int,int>,int>> STL[M],STR[M];
/*void AddL(int &c, int ss, int se, int qs, int qe, int l, int r)
{
	if(qs>qe || qs>se || ss>qe) return;
	if(!c) c=++tsz;
	if(qs<=ss && qe>=se){ LST[c].pb(mp(l,r));return;}
	int mid=ss+se>>1;
	AddL(ls[c],ss,mid,qs,qe,l,r);
	AddL(rs[c],mid+1,se,qs,qe,l,r);
}
void AddR(int &c, int ss, int se, int qs, int qe, int l, int r)
{
	if(qs>qe || qs>se || ss>qe) return;
	if(!c) c=++tsz;
	if(qs<=ss && qe>=se){ RST[c].pb(mp(l,r));return;}
	int mid=ss+se>>1;
	AddR(ls[c],ss,mid,qs,qe,l,r);
	AddR(rs[c],mid+1,se,qs,qe,l,r);
}*/
int lsz[M],rsz[M];
void Build(int &c, int ss, int se, vector<int> l, vector<int> r)
{
	c=++tsz;int mid=ss+se>>1;
	vector<int> PLL,PRL,PLR,PRR;
	LST.clear();RST.clear();
	for(int j=0;j<l.size();j++)
	{
		int i=l[j];
		if(L[i].first.first<=ss && L[i].first.second>=se) LST.pb(L[i].second);
		else
		{
			if(L[i].first.first<=mid) PLL.pb(i);
			if(L[i].first.second>mid) PRL.pb(i);
		}
	}
	for(int j=0;j<r.size();j++)
	{
		int i=r[j];
		if(R[i].first.first<=ss && R[i].first.second>=se) RST.pb(R[i].second);
		else
		{
			if(R[i].first.first<=mid) PLR.pb(i);
			if(R[i].first.second>mid) PRR.pb(i);
		}
	}
	STL[c].reserve(LST.size());
	int mx=-lim;
	for(int i=0;i<LST.size();i++)
	{
		if(LST[i].second>=mx)
		{
			STL[c].pb(mp(mp(max(LST[i].first,mx),LST[i].second),LST[i].first));
			mx=LST[i].second+1;
			//printf("%i %i %i\n",get<0>(STL[c].back()),get<1>(STL[c].back()),get<2>(STL[c].back()));
		}
	}
	lsz[c]=STL[c].size();
	STR[c].reserve(RST.size());
	int mn=lim*2;
	for(int i=0;i<RST.size();i++)
	{
		if(RST[i].first<=mn)
		{
			STR[c].pb(mp(mp(RST[i].first,min(RST[i].second,mn)),RST[i].second));
			mn=RST[i].first-1;
			//printf("%i %i %i\n",get<0>(STR[c].back()),get<1>(STR[c].back()),get<2>(STR[c].back()));
		}
	}
	reverse(STR[c].begin(),STR[c].end());
	rsz[c]=STR[c].size();
	LST.clear();RST.clear();R.clear();L.clear();
	if(ss==se) return;
	Build(ls[c],ss,mid,PLL,PLR);
	PLL.clear();PLR.clear();
	Build(rs[c],mid+1,se,PRL,PRR);
	PRL.clear();PRR.clear();
}
int Solve(int c, int ss, int se, int qi, int x)
{
	int ans=0;
	while(lsz[c] && STL[c][lsz[c]-1].first.first>x) lsz[c]--;
	while(rsz[c] && STR[c][rsz[c]-1].first.first>x)	rsz[c]--;
	if(lsz[c] && STL[c][lsz[c]-1].first.second>=x) ans=max(ans,x-STL[c][lsz[c]-1].second);
	if(rsz[c] && STR[c][rsz[c]-1].first.second>=x) ans=max(ans,STR[c][rsz[c]-1].second-x);
	if(ss==se) return ans;
	int mid=ss+se>>1;
	if(qi<=mid) return max(ans,Solve(ls[c],ss,mid,qi,x));
	else return max(ans,Solve(rs[c],mid+1,se,qi,x));
}
int main()
{
	int n,k,q,i;
	scanf("%i %i %i",&n,&k,&q);
	vector<tuple<int,int,int>> events;
	for(i=1;i<=n;i++)
	{
		scanf("%i %i %i %i",&x[i],&t[i],&l[i],&r[i]);
		events.pb(mt(l[i],0,i));
		events.pb(mt(r[i]+1,1,i));
	}
	for(i=1;i<=q;i++)
	{
		scanf("%i %i",&qx[i],&qy[i]);
		events.pb(mt(qy[i],2,i));
	}
	for(i=1;i<=k;i++)
	{
		all[i].insert(mp(-2*lim,-1));
		all[i].insert(mp(2*lim,0));
		Put(i,-2*lim,2*lim,0,2*lim);
	}
	sort(events.begin(),events.end());
	vector<int> work;
	for(auto e:events)
	{
		int tme=get<0>(e);
		int type=get<1>(e);
		int i=get<2>(e);
		if(type!=2)
		{
			if(type==0) all[t[i]].insert(mp(x[i],i));
			auto it=all[t[i]].find(mp(x[i],i));
			int lid=0,rid=0;
			if(it!=all[t[i]].begin()) it--,lid=it->first,it++;
			it++;if(it!=all[t[i]].end()) rid=it->first;it--;
			if(type==0) Put(t[i],lid,rid,type^1,tme);
			Put(t[i],lid,x[i],type,tme);
			Put(t[i],x[i],rid,type,tme);
			if(type==1) Put(t[i],lid,rid,type^1,tme);
			if(all[t[i]].size()==3) active+=type==0?1:-1;
			if(type==1) all[t[i]].erase(mp(x[i],i));
		}
		else
		{
			if(active!=k) ans[i]=-1;
			else work.pb(i);
		}
		//printf("%i %i %i %i\n",tme,type,i,active);
	}
	sort(L.begin(),L.end(),[](pair<pair<int,int>,pair<int,int>> a, pair<pair<int,int>,pair<int,int>> b){ return a.second.first<b.second.first;});
	sort(R.begin(),R.end(),[](pair<pair<int,int>,pair<int,int>> a, pair<pair<int,int>,pair<int,int>> b){ return a.second.second>b.second.second;});
	sort(work.begin(),work.end(),[&](int a, int b){ return qx[a]>qx[b];});
	vector<int> id;
	for(auto tp:L) id.pb(tp.first.first),id.pb(tp.first.second);
	for(auto tp:R) id.pb(tp.first.first),id.pb(tp.first.second);
	for(int idx:work) id.pb(qy[idx]);
	sort(id.begin(),id.end());
	id.erase(unique(id.begin(),id.end()),id.end());
	auto Get=[&](int x){ return lower_bound(id.begin(),id.end(),x)-id.begin()+1;};
	for(auto &tp:L) tp.first.first=Get(tp.first.first),tp.first.second=Get(tp.first.second);//tp=mt(Get(get<0>(tp)),Get(get<1>(tp)),get<2>(tp),get<3>(tp));
	for(auto &tp:R) tp.first.first=Get(tp.first.first),tp.first.second=Get(tp.first.second);//tp=mt(Get(get<0>(tp)),Get(get<1>(tp)),get<2>(tp),get<3>(tp));
	//for(auto tp:L) AddL(root,1,id.size(),Get(get<0>(tp)),Get(get<1>(tp)),get<2>(tp),get<3>(tp));
	//for(auto tp:R) AddR(root,1,id.size(),Get(get<0>(tp)),Get(get<1>(tp)),get<2>(tp),get<3>(tp));
	vector<int> lb,rb;
	for(int i=0;i<L.size();i++) lb.pb(i);
	for(int i=0;i<R.size();i++) rb.pb(i);
	Build(root,1,id.size(),lb,rb);
	for(int idx:work) ans[idx]=Solve(root,1,id.size(),Get(qy[idx]),qx[idx]);
	for(int i=1;i<=q;i++) printf("%i\n",ans[i]);
	return 0;
}

Compilation message

new_home.cpp: In function 'void Put(int, int, int, int, int)':
new_home.cpp:15:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=l+r>>1;
          ~^~
new_home.cpp: In function 'void Build(int&, int, int, std::vector<int>, std::vector<int>)':
new_home.cpp:52:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  c=++tsz;int mid=ss+se>>1;
                  ~~^~~
new_home.cpp:55:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j=0;j<l.size();j++)
              ~^~~~~~~~~
new_home.cpp:65:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j=0;j<r.size();j++)
              ~^~~~~~~~~
new_home.cpp:77:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<LST.size();i++)
              ~^~~~~~~~~~~
new_home.cpp:89:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<RST.size();i++)
              ~^~~~~~~~~~~
new_home.cpp: In function 'int Solve(int, int, int, int, int)':
new_home.cpp:115:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:184:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<L.size();i++) lb.pb(i);
              ~^~~~~~~~~
new_home.cpp:185:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<R.size();i++) rb.pb(i);
              ~^~~~~~~~~
new_home.cpp:122:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i %i",&n,&k,&q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
new_home.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i %i %i",&x[i],&t[i],&l[i],&r[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&qx[i],&qy[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 78 ms 84984 KB Output is correct
2 Correct 86 ms 84984 KB Output is correct
3 Correct 87 ms 84984 KB Output is correct
4 Correct 85 ms 84956 KB Output is correct
5 Correct 83 ms 85240 KB Output is correct
6 Correct 89 ms 85440 KB Output is correct
7 Correct 82 ms 85256 KB Output is correct
8 Correct 90 ms 85404 KB Output is correct
9 Correct 85 ms 85240 KB Output is correct
10 Correct 91 ms 85620 KB Output is correct
11 Correct 93 ms 85240 KB Output is correct
12 Correct 101 ms 85544 KB Output is correct
13 Correct 93 ms 85200 KB Output is correct
14 Correct 101 ms 85352 KB Output is correct
15 Correct 87 ms 85340 KB Output is correct
16 Correct 86 ms 85496 KB Output is correct
17 Correct 85 ms 85368 KB Output is correct
18 Correct 85 ms 85336 KB Output is correct
19 Correct 85 ms 85368 KB Output is correct
20 Correct 83 ms 85368 KB Output is correct
21 Correct 84 ms 85112 KB Output is correct
22 Correct 92 ms 85240 KB Output is correct
23 Correct 84 ms 85460 KB Output is correct
24 Correct 101 ms 85388 KB Output is correct
25 Correct 89 ms 85468 KB Output is correct
26 Correct 96 ms 85372 KB Output is correct
27 Correct 83 ms 85204 KB Output is correct
28 Correct 100 ms 85240 KB Output is correct
29 Correct 92 ms 85272 KB Output is correct
30 Correct 94 ms 85256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 84984 KB Output is correct
2 Correct 86 ms 84984 KB Output is correct
3 Correct 87 ms 84984 KB Output is correct
4 Correct 85 ms 84956 KB Output is correct
5 Correct 83 ms 85240 KB Output is correct
6 Correct 89 ms 85440 KB Output is correct
7 Correct 82 ms 85256 KB Output is correct
8 Correct 90 ms 85404 KB Output is correct
9 Correct 85 ms 85240 KB Output is correct
10 Correct 91 ms 85620 KB Output is correct
11 Correct 93 ms 85240 KB Output is correct
12 Correct 101 ms 85544 KB Output is correct
13 Correct 93 ms 85200 KB Output is correct
14 Correct 101 ms 85352 KB Output is correct
15 Correct 87 ms 85340 KB Output is correct
16 Correct 86 ms 85496 KB Output is correct
17 Correct 85 ms 85368 KB Output is correct
18 Correct 85 ms 85336 KB Output is correct
19 Correct 85 ms 85368 KB Output is correct
20 Correct 83 ms 85368 KB Output is correct
21 Correct 84 ms 85112 KB Output is correct
22 Correct 92 ms 85240 KB Output is correct
23 Correct 84 ms 85460 KB Output is correct
24 Correct 101 ms 85388 KB Output is correct
25 Correct 89 ms 85468 KB Output is correct
26 Correct 96 ms 85372 KB Output is correct
27 Correct 83 ms 85204 KB Output is correct
28 Correct 100 ms 85240 KB Output is correct
29 Correct 92 ms 85272 KB Output is correct
30 Correct 94 ms 85256 KB Output is correct
31 Correct 1752 ms 200964 KB Output is correct
32 Correct 303 ms 106960 KB Output is correct
33 Correct 1940 ms 204872 KB Output is correct
34 Correct 1662 ms 197044 KB Output is correct
35 Correct 1888 ms 204204 KB Output is correct
36 Correct 1773 ms 210040 KB Output is correct
37 Correct 1339 ms 193420 KB Output is correct
38 Correct 1362 ms 194164 KB Output is correct
39 Correct 1047 ms 178196 KB Output is correct
40 Correct 1156 ms 182348 KB Output is correct
41 Correct 1282 ms 166272 KB Output is correct
42 Correct 1095 ms 164804 KB Output is correct
43 Correct 205 ms 102224 KB Output is correct
44 Correct 1246 ms 165244 KB Output is correct
45 Correct 1205 ms 159776 KB Output is correct
46 Correct 1095 ms 150236 KB Output is correct
47 Correct 684 ms 141180 KB Output is correct
48 Correct 654 ms 141484 KB Output is correct
49 Correct 783 ms 150048 KB Output is correct
50 Correct 862 ms 157532 KB Output is correct
51 Correct 801 ms 148288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3353 ms 609040 KB Output is correct
2 Execution timed out 5067 ms 662536 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5058 ms 576976 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 84984 KB Output is correct
2 Correct 86 ms 84984 KB Output is correct
3 Correct 87 ms 84984 KB Output is correct
4 Correct 85 ms 84956 KB Output is correct
5 Correct 83 ms 85240 KB Output is correct
6 Correct 89 ms 85440 KB Output is correct
7 Correct 82 ms 85256 KB Output is correct
8 Correct 90 ms 85404 KB Output is correct
9 Correct 85 ms 85240 KB Output is correct
10 Correct 91 ms 85620 KB Output is correct
11 Correct 93 ms 85240 KB Output is correct
12 Correct 101 ms 85544 KB Output is correct
13 Correct 93 ms 85200 KB Output is correct
14 Correct 101 ms 85352 KB Output is correct
15 Correct 87 ms 85340 KB Output is correct
16 Correct 86 ms 85496 KB Output is correct
17 Correct 85 ms 85368 KB Output is correct
18 Correct 85 ms 85336 KB Output is correct
19 Correct 85 ms 85368 KB Output is correct
20 Correct 83 ms 85368 KB Output is correct
21 Correct 84 ms 85112 KB Output is correct
22 Correct 92 ms 85240 KB Output is correct
23 Correct 84 ms 85460 KB Output is correct
24 Correct 101 ms 85388 KB Output is correct
25 Correct 89 ms 85468 KB Output is correct
26 Correct 96 ms 85372 KB Output is correct
27 Correct 83 ms 85204 KB Output is correct
28 Correct 100 ms 85240 KB Output is correct
29 Correct 92 ms 85272 KB Output is correct
30 Correct 94 ms 85256 KB Output is correct
31 Correct 1752 ms 200964 KB Output is correct
32 Correct 303 ms 106960 KB Output is correct
33 Correct 1940 ms 204872 KB Output is correct
34 Correct 1662 ms 197044 KB Output is correct
35 Correct 1888 ms 204204 KB Output is correct
36 Correct 1773 ms 210040 KB Output is correct
37 Correct 1339 ms 193420 KB Output is correct
38 Correct 1362 ms 194164 KB Output is correct
39 Correct 1047 ms 178196 KB Output is correct
40 Correct 1156 ms 182348 KB Output is correct
41 Correct 1282 ms 166272 KB Output is correct
42 Correct 1095 ms 164804 KB Output is correct
43 Correct 205 ms 102224 KB Output is correct
44 Correct 1246 ms 165244 KB Output is correct
45 Correct 1205 ms 159776 KB Output is correct
46 Correct 1095 ms 150236 KB Output is correct
47 Correct 684 ms 141180 KB Output is correct
48 Correct 654 ms 141484 KB Output is correct
49 Correct 783 ms 150048 KB Output is correct
50 Correct 862 ms 157532 KB Output is correct
51 Correct 801 ms 148288 KB Output is correct
52 Correct 667 ms 140704 KB Output is correct
53 Correct 672 ms 137952 KB Output is correct
54 Correct 1088 ms 167044 KB Output is correct
55 Correct 1055 ms 161508 KB Output is correct
56 Correct 947 ms 158064 KB Output is correct
57 Correct 1177 ms 164688 KB Output is correct
58 Correct 1037 ms 160100 KB Output is correct
59 Correct 932 ms 156428 KB Output is correct
60 Correct 1174 ms 163940 KB Output is correct
61 Correct 177 ms 111176 KB Output is correct
62 Correct 603 ms 143760 KB Output is correct
63 Correct 838 ms 161464 KB Output is correct
64 Correct 1050 ms 166916 KB Output is correct
65 Correct 1212 ms 174000 KB Output is correct
66 Correct 1256 ms 170008 KB Output is correct
67 Correct 478 ms 119644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 84984 KB Output is correct
2 Correct 86 ms 84984 KB Output is correct
3 Correct 87 ms 84984 KB Output is correct
4 Correct 85 ms 84956 KB Output is correct
5 Correct 83 ms 85240 KB Output is correct
6 Correct 89 ms 85440 KB Output is correct
7 Correct 82 ms 85256 KB Output is correct
8 Correct 90 ms 85404 KB Output is correct
9 Correct 85 ms 85240 KB Output is correct
10 Correct 91 ms 85620 KB Output is correct
11 Correct 93 ms 85240 KB Output is correct
12 Correct 101 ms 85544 KB Output is correct
13 Correct 93 ms 85200 KB Output is correct
14 Correct 101 ms 85352 KB Output is correct
15 Correct 87 ms 85340 KB Output is correct
16 Correct 86 ms 85496 KB Output is correct
17 Correct 85 ms 85368 KB Output is correct
18 Correct 85 ms 85336 KB Output is correct
19 Correct 85 ms 85368 KB Output is correct
20 Correct 83 ms 85368 KB Output is correct
21 Correct 84 ms 85112 KB Output is correct
22 Correct 92 ms 85240 KB Output is correct
23 Correct 84 ms 85460 KB Output is correct
24 Correct 101 ms 85388 KB Output is correct
25 Correct 89 ms 85468 KB Output is correct
26 Correct 96 ms 85372 KB Output is correct
27 Correct 83 ms 85204 KB Output is correct
28 Correct 100 ms 85240 KB Output is correct
29 Correct 92 ms 85272 KB Output is correct
30 Correct 94 ms 85256 KB Output is correct
31 Correct 1752 ms 200964 KB Output is correct
32 Correct 303 ms 106960 KB Output is correct
33 Correct 1940 ms 204872 KB Output is correct
34 Correct 1662 ms 197044 KB Output is correct
35 Correct 1888 ms 204204 KB Output is correct
36 Correct 1773 ms 210040 KB Output is correct
37 Correct 1339 ms 193420 KB Output is correct
38 Correct 1362 ms 194164 KB Output is correct
39 Correct 1047 ms 178196 KB Output is correct
40 Correct 1156 ms 182348 KB Output is correct
41 Correct 1282 ms 166272 KB Output is correct
42 Correct 1095 ms 164804 KB Output is correct
43 Correct 205 ms 102224 KB Output is correct
44 Correct 1246 ms 165244 KB Output is correct
45 Correct 1205 ms 159776 KB Output is correct
46 Correct 1095 ms 150236 KB Output is correct
47 Correct 684 ms 141180 KB Output is correct
48 Correct 654 ms 141484 KB Output is correct
49 Correct 783 ms 150048 KB Output is correct
50 Correct 862 ms 157532 KB Output is correct
51 Correct 801 ms 148288 KB Output is correct
52 Correct 3353 ms 609040 KB Output is correct
53 Execution timed out 5067 ms 662536 KB Time limit exceeded
54 Halted 0 ms 0 KB -