Submission #189439

# Submission time Handle Problem Language Result Execution time Memory
189439 2020-01-14T03:40:40 Z dndhk None (JOI16_matryoshka) C++14
100 / 100
1353 ms 44896 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_N = 200000;

int N, Q, K;
struct S{
	int a, b, idx;
	bool operator <(const S &x)const{
		return a<x.a;
	}
};
vector<S> query;
vector<pii> vt;
int ans[MAX_N+1];
vector<int> v;
map<int, int> mp;

void input(){
	scanf("%d%d", &N, &Q);
	for(int i=1; i<=N; i++){
		int a, b; scanf("%d%d", &a, &b);
		v.pb(b);
		vt.pb({a, b});
	}
	for(int i=1; i<=Q; i++){
		int a, b; scanf("%d%d", &a, &b);
		v.pb(b);
		query.pb({a, b, i});
	}
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	K = v.size();
	for(int i=0 ;i<v.size(); i++){
		mp[v[i]] = i+1;
	}
	for(int i=0; i<vt.size(); i++){
		vt[i].second = mp[vt[i].second];
	}
	for(int i=0; i<query.size(); i++){
		query[i].b = mp[query[i].b];
	}
	sort(query.begin(), query.end());
	sort(vt.begin(), vt.end());}
struct SEG{
	struct NODE{
		int l, r;
		int sum;
	};
	vector<NODE> seg;
	int SZ;
	void add(){
		seg.pb({-1, -1, 0});
	}
	void Init(int x){
		SZ = x;
		add();
		init(0, 1, SZ);
	}
	void init(int idx, int s, int e){
		if(s==e)	return;
		seg[idx].l = seg.size(); add();
		seg[idx].r = seg.size(); add();
		init(seg[idx].l, s, (s+e)/2);
		init(seg[idx].r, (s+e)/2+1, e);
	}
	void Update(int x, int y){
		update(0, 1, SZ, x, y);
	}
	void update(int idx, int s, int e, int x, int y){
		seg[idx].sum+=y;
		if(s==e)	return;
		if(x<=(s+e)/2){
			update(seg[idx].l, s, (s+e)/2, x, y);
		}else{
			update(seg[idx].r, (s+e)/2+1, e, x, y);
		}
	}
	int Sum(int x, int y){
		return sum(0, 1, SZ, x, y);
	}
	int sum(int idx, int s, int e, int x, int y){
		if(x<=s && e<=y)	return seg[idx].sum;
		if(x>e || y<s)	return 0;
		return sum(seg[idx].l, s, (s+e)/2, x, y) + sum(seg[idx].r, (s+e)/2+1, e, x, y);
	}
}Seg;

multiset<int> st;
void upd(pii x){
	multiset<int>::iterator it = st.upper_bound(x.second);
	if(it!=st.end()){
		st.erase(it);
		int k = (*it);
		Seg.Update(k, -1);
	}
	Seg.Update(x.second, 1);
}

vector<int> prv;

int main(){
	input();
	Seg.Init(K);
	while(!query.empty()){
		int pa = -1;
		S q = query.back(); query.pop_back();
		while(!vt.empty() && vt.back().first>=q.a){
			if(pa!=-1){
				if(pa!=vt.back().first){
					while(!prv.empty()){
						st.insert(prv.back()); prv.pop_back();
					}
				}
			}
			pa = vt.back().first;
			upd(vt.back());
			prv.pb(vt.back().second);
			vt.pop_back();
		}
		while(!prv.empty()){
			st.insert(prv.back()); prv.pop_back();
		}
		ans[q.idx] = Seg.Sum(1, q.b);
	}
	for(int i=1; i<=Q; i++){
		printf("%d\n", ans[i]);
	}
}

Compilation message

matryoshka.cpp: In function 'void input()':
matryoshka.cpp:40:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0 ;i<v.size(); i++){
               ~^~~~~~~~~
matryoshka.cpp:43:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<vt.size(); i++){
               ~^~~~~~~~~~
matryoshka.cpp:46:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<query.size(); i++){
               ~^~~~~~~~~~~~~
matryoshka.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &Q);
  ~~~~~^~~~~~~~~~~~~~~~
matryoshka.cpp:28:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~
matryoshka.cpp:33:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 10 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 7 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 10 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 7 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 252 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 10 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 7 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 252 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 380 KB Output is correct
21 Correct 3 ms 256 KB Output is correct
22 Correct 4 ms 632 KB Output is correct
23 Correct 7 ms 1020 KB Output is correct
24 Correct 2 ms 960 KB Output is correct
25 Correct 8 ms 1016 KB Output is correct
26 Correct 7 ms 988 KB Output is correct
27 Correct 8 ms 1016 KB Output is correct
28 Correct 9 ms 984 KB Output is correct
29 Correct 7 ms 1016 KB Output is correct
30 Correct 9 ms 760 KB Output is correct
31 Correct 8 ms 1016 KB Output is correct
32 Correct 8 ms 1016 KB Output is correct
33 Correct 8 ms 1008 KB Output is correct
34 Correct 7 ms 1016 KB Output is correct
35 Correct 8 ms 960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 10 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 7 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 252 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 380 KB Output is correct
21 Correct 3 ms 256 KB Output is correct
22 Correct 4 ms 632 KB Output is correct
23 Correct 7 ms 1020 KB Output is correct
24 Correct 2 ms 960 KB Output is correct
25 Correct 8 ms 1016 KB Output is correct
26 Correct 7 ms 988 KB Output is correct
27 Correct 8 ms 1016 KB Output is correct
28 Correct 9 ms 984 KB Output is correct
29 Correct 7 ms 1016 KB Output is correct
30 Correct 9 ms 760 KB Output is correct
31 Correct 8 ms 1016 KB Output is correct
32 Correct 8 ms 1016 KB Output is correct
33 Correct 8 ms 1008 KB Output is correct
34 Correct 7 ms 1016 KB Output is correct
35 Correct 8 ms 960 KB Output is correct
36 Correct 785 ms 24828 KB Output is correct
37 Correct 293 ms 13392 KB Output is correct
38 Correct 284 ms 18460 KB Output is correct
39 Correct 299 ms 11776 KB Output is correct
40 Correct 299 ms 12356 KB Output is correct
41 Correct 360 ms 13356 KB Output is correct
42 Correct 203 ms 11472 KB Output is correct
43 Correct 179 ms 19772 KB Output is correct
44 Correct 1031 ms 44680 KB Output is correct
45 Correct 1353 ms 44436 KB Output is correct
46 Correct 1329 ms 44508 KB Output is correct
47 Correct 959 ms 44756 KB Output is correct
48 Correct 1016 ms 44824 KB Output is correct
49 Correct 1002 ms 44896 KB Output is correct
50 Correct 991 ms 44416 KB Output is correct