답안 #102428

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102428 2019-03-25T00:53:46 Z silxikys Examination (JOI19_examination) C++14
100 / 100
2730 ms 514724 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
/*
 * Problem is essentially a series of 3-d queries, i.e,
 * for a set of points in R^3 (x,y,x+y), find those with
 * z-coordinate above z, y coordinate above y, and x-coord
 * above x.
 * One dimension, z, can be removed by answering the queries
 * offline and simulating the queries with a line sweep and 
 * answering the queries using 2-d segtree.
 * I used a BIT for x-coordinate and dynamic segtree for 
 * y-axis. However, due to memory constraints, I had to 
 * coordinate-compress both the x and y coordinates
 * to fit under the 1G limit. (Note that after sorting
 * the points/queries by z-coordinate, coordinate-compression
 * does not affect the 2-d queries.)
*/
struct SegDyn
{
	int s, e, m; //represents range [s,e]
	SegDyn *l, *r;
	int sum; //sum for this interval
	SegDyn(int _s, int _e) {
		s = _s;
		e = _e;
		m = (s+e)/2;
		l = NULL;
		r = NULL;
		sum = 0;
	}

	void prepareL() { if (l == NULL) l = new SegDyn(s,m); }
	void prepareR() { if (r == NULL) r = new SegDyn(m+1,e); }

	void pull() {
		sum = 0;
		if (l) sum += l->sum;
		if (r) sum += r->sum;
	}

	void add(int idx, int del) { //a[idx] += del
		//cout << s << ' ' << e << '\n';
		if (s == e && s == idx) {
			//at the node, stop
			sum += del;
			return;
		}
		if (idx <= m) {
			prepareL();
			//assert(l);
			l->add(idx,del);
		}
		else {
			prepareR();
			r->add(idx,del);
		}
		pull(); //updates current node based on the children
	}

	int getsum(int se, int en) {
		if (se <= s && e <= en) return sum;
		int res = 0;
		if (l && se <= m) res += l->getsum(se,en);
		if (r && en > m) res += r->getsum(se,en);
		return res;	
	}
};

const int maxn = 1e5+5, MAX = 1e9+5;
int N, Q;
SegDyn* xs[2*maxn];
void add(int x, int y) {
    for (; x <= N+Q; x += x & (-x)) {
        xs[x]->add(y,1); 
    }
}
int sum(int x, int y1) {
    int r = 0;
    for (; x; x -= x & (-x)) {
        r += xs[x]->getsum(y1,N+Q);
    }
    return r;
}
struct Query
{
    int x, y, z; //z = x + y;
    bool isQuery;
    int id; //id # for queries
} queries[2*maxn];

int ans[maxn];

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> Q;
    int pt = 0;
    for (int i = 0; i < N; i++) {
        int s, t; cin >> s >> t;
        queries[pt++] = {s,t,s+t,false,-1};
    }
    for (int i = 0; i < Q; i++) {
        int x, y, z; cin >> x >> y >> z;
        queries[pt++] = {x,y,z,true,i};
    }
    sort(queries,queries+N+Q,[](auto a, auto b) { 
            if (a.z != b.z) return a.z > b.z;
            return ((int)a.isQuery < (int)b.isQuery); });
    //coordinate compress x-coordinates
    vector<int> xcoords;
    vector<int> ycoords;
    for (int i = 0; i <= N+Q; i++) {
        xs[i] = new SegDyn(0,N+Q);
        if (i < N+Q) {
            xcoords.push_back(queries[i].x);
            ycoords.push_back(queries[i].y);
        }
    }
    sort(xcoords.begin(),xcoords.end());
    sort(ycoords.begin(),ycoords.end());
    xcoords.erase(unique(xcoords.begin(),xcoords.end()),xcoords.end());
    ycoords.erase(unique(ycoords.begin(),ycoords.end()),ycoords.end());
    map<int,int> mp;
    pt = 1;
    for (int x: xcoords) mp[x] = pt++;
    map<int,int> mp2;
    pt = 1;
    for (int y: ycoords) mp2[y] = pt++;
    //try to compress y-coordinates too (for memory reasons)
    for (int i = 0; i < N+Q; i++) {
        queries[i].x = mp[queries[i].x];            
        queries[i].y = mp2[queries[i].y];
        //cout << queries[i].x << ' ' << queries[i].y << ' ' << queries[i].z << ' ' << queries[i].isQuery << '\n';
    }
    //now process queries
    for (int i = 0; i < N + Q; i++) {
        auto q = queries[i];
        if (!q.isQuery) {
            add(q.x,q.y);
        }
        else {
            int res = sum(N+Q,q.y) - sum(q.x-1,q.y);
            ans[q.id] = res;
        }
    }
    //output queries
    for (int i = 0; i < Q; i++) {
        cout << ans[i] << '\n';
    }
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 27 ms 9336 KB Output is correct
8 Correct 28 ms 9336 KB Output is correct
9 Correct 31 ms 9256 KB Output is correct
10 Correct 18 ms 4096 KB Output is correct
11 Correct 17 ms 6264 KB Output is correct
12 Correct 7 ms 896 KB Output is correct
13 Correct 35 ms 9372 KB Output is correct
14 Correct 30 ms 9344 KB Output is correct
15 Correct 38 ms 9344 KB Output is correct
16 Correct 17 ms 6272 KB Output is correct
17 Correct 20 ms 3712 KB Output is correct
18 Correct 6 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2447 ms 417324 KB Output is correct
2 Correct 2548 ms 417340 KB Output is correct
3 Correct 2593 ms 416992 KB Output is correct
4 Correct 986 ms 101692 KB Output is correct
5 Correct 744 ms 155360 KB Output is correct
6 Correct 253 ms 18780 KB Output is correct
7 Correct 1922 ms 400204 KB Output is correct
8 Correct 2468 ms 402056 KB Output is correct
9 Correct 1867 ms 386712 KB Output is correct
10 Correct 646 ms 147680 KB Output is correct
11 Correct 841 ms 85340 KB Output is correct
12 Correct 219 ms 18400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2447 ms 417324 KB Output is correct
2 Correct 2548 ms 417340 KB Output is correct
3 Correct 2593 ms 416992 KB Output is correct
4 Correct 986 ms 101692 KB Output is correct
5 Correct 744 ms 155360 KB Output is correct
6 Correct 253 ms 18780 KB Output is correct
7 Correct 1922 ms 400204 KB Output is correct
8 Correct 2468 ms 402056 KB Output is correct
9 Correct 1867 ms 386712 KB Output is correct
10 Correct 646 ms 147680 KB Output is correct
11 Correct 841 ms 85340 KB Output is correct
12 Correct 219 ms 18400 KB Output is correct
13 Correct 2217 ms 417484 KB Output is correct
14 Correct 2496 ms 418020 KB Output is correct
15 Correct 2713 ms 416968 KB Output is correct
16 Correct 704 ms 102108 KB Output is correct
17 Correct 726 ms 155752 KB Output is correct
18 Correct 296 ms 18904 KB Output is correct
19 Correct 2298 ms 417304 KB Output is correct
20 Correct 2730 ms 417616 KB Output is correct
21 Correct 2039 ms 406624 KB Output is correct
22 Correct 613 ms 147928 KB Output is correct
23 Correct 797 ms 85388 KB Output is correct
24 Correct 243 ms 18348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 27 ms 9336 KB Output is correct
8 Correct 28 ms 9336 KB Output is correct
9 Correct 31 ms 9256 KB Output is correct
10 Correct 18 ms 4096 KB Output is correct
11 Correct 17 ms 6264 KB Output is correct
12 Correct 7 ms 896 KB Output is correct
13 Correct 35 ms 9372 KB Output is correct
14 Correct 30 ms 9344 KB Output is correct
15 Correct 38 ms 9344 KB Output is correct
16 Correct 17 ms 6272 KB Output is correct
17 Correct 20 ms 3712 KB Output is correct
18 Correct 6 ms 896 KB Output is correct
19 Correct 2447 ms 417324 KB Output is correct
20 Correct 2548 ms 417340 KB Output is correct
21 Correct 2593 ms 416992 KB Output is correct
22 Correct 986 ms 101692 KB Output is correct
23 Correct 744 ms 155360 KB Output is correct
24 Correct 253 ms 18780 KB Output is correct
25 Correct 1922 ms 400204 KB Output is correct
26 Correct 2468 ms 402056 KB Output is correct
27 Correct 1867 ms 386712 KB Output is correct
28 Correct 646 ms 147680 KB Output is correct
29 Correct 841 ms 85340 KB Output is correct
30 Correct 219 ms 18400 KB Output is correct
31 Correct 2217 ms 417484 KB Output is correct
32 Correct 2496 ms 418020 KB Output is correct
33 Correct 2713 ms 416968 KB Output is correct
34 Correct 704 ms 102108 KB Output is correct
35 Correct 726 ms 155752 KB Output is correct
36 Correct 296 ms 18904 KB Output is correct
37 Correct 2298 ms 417304 KB Output is correct
38 Correct 2730 ms 417616 KB Output is correct
39 Correct 2039 ms 406624 KB Output is correct
40 Correct 613 ms 147928 KB Output is correct
41 Correct 797 ms 85388 KB Output is correct
42 Correct 243 ms 18348 KB Output is correct
43 Correct 2342 ms 514284 KB Output is correct
44 Correct 2392 ms 514724 KB Output is correct
45 Correct 2703 ms 514468 KB Output is correct
46 Correct 975 ms 160968 KB Output is correct
47 Correct 901 ms 260744 KB Output is correct
48 Correct 249 ms 18656 KB Output is correct
49 Correct 2253 ms 484312 KB Output is correct
50 Correct 2135 ms 491480 KB Output is correct
51 Correct 1927 ms 461820 KB Output is correct
52 Correct 878 ms 262232 KB Output is correct
53 Correct 989 ms 144356 KB Output is correct