Submission #632192

# Submission time Handle Problem Language Result Execution time Memory
632192 2022-08-19T15:30:54 Z minhcool 도로 건설 사업 (JOI13_construction) C++17
100 / 100
3697 ms 218872 KB
#include<bits/stdc++.h>
using namespace std;
 
#define y1 y11
#define double long double
#define int long long
#define fi first
#define se second
#define pb push_back
//#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
 
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
 
const int N = 6e5 + 5;
 
const int oo = 1e18 + 7, mod = 1e9 + 7;
 
int n, m, c;
int x[N], y[N];
 
int a[N], b[N], ccc[N], d[N];
 
map<int, vector<ii>> mp1, mp2;
 
vector<pair<int, ii>> edges;
 
int cc[N], mx[N];
 
int tol[N];
 
int sz[N], root[N];
 
int rt(int x){
    return (x == root[x] ? x : rt(root[x]));
}
 
bool merge(int x, int y){
    x = rt(x), y = rt(y);
    if(x == y) return 0;
    if(sz[x] < sz[y]) swap(x, y);
    sz[x] += sz[y];
    root[y] = x;
    return 1;
}

vector<int> vc1, vc2;
map<int, int> m1, m2;

int IT[N << 2], lazy[N << 2];
int diff[N];

void laz(int id, int l, int r){
    int t = lazy[id];
    int mid = (l + r) >> 1;
    lazy[id << 1] += t;
    lazy[id << 1 | 1] += t;
    IT[id << 1] += t * (mid - l + 1);
    IT[id << 1 | 1] += t * (r - mid);
    lazy[id] = 0;
}

void upd(int id, int l, int r, int L, int R, int val){
    if(l > R || r < L) return;
    if(l >= L && r <= R){
        IT[id] += val * (r - l + 1);
        lazy[id] += val;
        return;
    }
    laz(id, l, r);
    int mid = (l + r) >> 1;
    upd(id << 1, l, mid, L, R, val);
    upd(id << 1 | 1, mid + 1, r, L, R, val);
    IT[id] = IT[id << 1] + IT[id << 1 | 1];
}

int get(int id, int l, int r, int L, int R){
    if(l > R || r < L) return 0;
    if(l >= L && r <= R) return IT[id];
    laz(id, l, r);
    int mid = (l + r) >> 1;
    return get(id << 1, l, mid, L, R) + get(id << 1 | 1, mid + 1, r, L, R);
}
 
void process(){
    cin >> n >> m >> c;
    for(int i = 1; i <= n; i++){
        cin >> x[i] >> y[i];
        mp1[x[i]].pb({y[i], i});
        mp2[y[i]].pb({x[i], i});
        vc1.pb(x[i]), vc2.pb(y[i]);
    }
    vector<iii> events;
    for(int i = 1; i <= m; i++){
        cin >> a[i] >> b[i] >> ccc[i] >> d[i];
        events.pb({{a[i], i}, 1});
        events.pb({{ccc[i] + 1, i}, -1});
        vc1.pb(a[i]);
        vc2.pb(b[i]);
        vc1.pb(ccc[i]);
        vc2.pb(d[i]);
    }
    sort(vc1.begin(), vc1.end());
    sort(vc2.begin(), vc2.end());
    for(int i = 0; i < vc1.size(); i++) m1[vc1[i]] = i + 1;
    for(int i = 0; i < vc2.size(); i++) m2[vc2[i]] = i + 1;
    sort(events.begin(), events.end());
    int itr = 0;
    //return;
    for(map<int, vector<ii>>::iterator it = mp1.begin(); it != mp1.end(); it++){
        while(itr < events.size() && events[itr].fi.fi <= (*it).fi){
            int ind = events[itr].fi.se;
            upd(1, 1, vc2.size(), m2[b[ind]], m2[d[ind]], events[itr].se);
            itr++;
        }
        vector<ii> temp = (*it).se;
        sort(temp.begin(), temp.end());
        for(int i = 1; i < temp.size(); i++){
            //cout << (*it).fi << " " << temp[i - 1].fi << " " << temp[i].fi << "\n";
            if(get(1, 1, vc2.size(), m2[temp[i - 1].fi], m2[temp[i].fi]) > 0) continue;
            //cout << (*it).fi << " " << temp[i - 1].fi << " " << temp[i].fi << "\n";
            //cout << "1 " << temp[i].se << " " << temp[i - 1].se << "\n";
            edges.pb({temp[i].fi - temp[i - 1].fi, {temp[i].se, temp[i - 1].se}});
        }
    }
    for(int i = 1; i <= (vc2.size() << 2); i++) IT[i] = lazy[i] = 0;
    events.clear();
    for(int i = 1; i <= m; i++){
        events.pb({{b[i], i}, 1});
        events.pb({{d[i] + 1, i}, -1});
    }
    sort(events.begin(), events.end());
    itr = 0;
    for(map<int, vector<ii>>::iterator it = mp2.begin(); it != mp2.end(); it++){
        while(itr < events.size() && events[itr].fi.fi <= (*it).fi){
            int ind = events[itr].fi.se;
            upd(1, 1, vc1.size(), m1[a[ind]], m1[ccc[ind]], events[itr].se);
            itr++;
        }
        vector<ii> temp = (*it).se;
        sort(temp.begin(), temp.end());
        for(int i = 1; i < temp.size(); i++){
            //cout << (*it).fi << " " << temp[i - 1].fi << " " << temp[i].fi << "\n";
            if(get(1, 1, vc1.size(), m1[temp[i - 1].fi], m1[temp[i].fi]) > 0) continue;
            //cout << (*it).fi << " " << temp[i - 1].fi << " " << temp[i].fi << "\n";
            //cout << "2 " << (*it).fi << " " << temp[i].se << " " << temp[i - 1].se << "\n";
            edges.pb({temp[i].fi - temp[i - 1].fi, {temp[i].se, temp[i - 1].se}});
        }
    }
    //return;
    sort(edges.begin(), edges.end());
    int cnt = n;
    for(int i = 1; i <= n; i++){
        sz[i] = 1, root[i] = i;
    }
    diff[n] = -oo;
    for(auto it : edges){
        //cout << it.se.fi << " " << it.se.se << "\n";
        //continue;
        if(merge(it.se.fi, it.se.se)){
            cnt--;
            assert(cnt >= 0);
            tol[cnt] = tol[cnt + 1] + it.fi;
            diff[cnt] = it.fi;
            //cout << cnt << " " << tol[cnt] << " " << diff[cnt] << "\n";
        }
    }
    //return;
    for(int i = 0; i < cnt; i++) tol[i] = oo;
    //for(int i = 0; i <= n; i++) cout << tol[i] << "\n";
    for(int i = 1; i <= c; i++){
        cin >> cc[i] >> mx[i];
        int answer = oo;
        if(mx[i] < cnt){
            cout << -1 << "\n";
            continue;
        }
        int l = cnt, r = n;
        while(l < r){
            int mid = (l + r) >> 1;
            //cout << l << " " << r << " " << mid << " " << cc[i] << " " << diff[cnt] << "\n";
            if(cc[i] < diff[mid]) l = mid + 1;
            else r = mid;
        }
        l = min(l, mx[i]);
        //cout << l << "\n";
        //for(int j = 0; j <= mx[i]; j++) answer = min(answer, cc[i] * j + tol[j]);
        cout << cc[i] * l + tol[l] << "\n";
    }
}
 
signed main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) process();
}

Compilation message

construction.cpp: In function 'void process()':
construction.cpp:108:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     for(int i = 0; i < vc1.size(); i++) m1[vc1[i]] = i + 1;
      |                    ~~^~~~~~~~~~~~
construction.cpp:109:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int i = 0; i < vc2.size(); i++) m2[vc2[i]] = i + 1;
      |                    ~~^~~~~~~~~~~~
construction.cpp:114:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |         while(itr < events.size() && events[itr].fi.fi <= (*it).fi){
      |               ~~~~^~~~~~~~~~~~~~~
construction.cpp:121:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for(int i = 1; i < temp.size(); i++){
      |                        ~~^~~~~~~~~~~~~
construction.cpp:129:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for(int i = 1; i <= (vc2.size() << 2); i++) IT[i] = lazy[i] = 0;
      |                    ~~^~~~~~~~~~~~~~~~~~~~
construction.cpp:138:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |         while(itr < events.size() && events[itr].fi.fi <= (*it).fi){
      |               ~~~~^~~~~~~~~~~~~~~
construction.cpp:145:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |         for(int i = 1; i < temp.size(); i++){
      |                        ~~^~~~~~~~~~~~~
construction.cpp:176:13: warning: unused variable 'answer' [-Wunused-variable]
  176 |         int answer = oo;
      |             ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4616 KB Output is correct
2 Correct 712 ms 69180 KB Output is correct
3 Correct 720 ms 67444 KB Output is correct
4 Correct 481 ms 47252 KB Output is correct
5 Correct 825 ms 70440 KB Output is correct
6 Correct 707 ms 67676 KB Output is correct
7 Correct 276 ms 43968 KB Output is correct
8 Correct 397 ms 70972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4616 KB Output is correct
2 Correct 712 ms 69180 KB Output is correct
3 Correct 720 ms 67444 KB Output is correct
4 Correct 481 ms 47252 KB Output is correct
5 Correct 825 ms 70440 KB Output is correct
6 Correct 707 ms 67676 KB Output is correct
7 Correct 276 ms 43968 KB Output is correct
8 Correct 397 ms 70972 KB Output is correct
9 Correct 3534 ms 159776 KB Output is correct
10 Correct 3398 ms 159680 KB Output is correct
11 Correct 3392 ms 159896 KB Output is correct
12 Correct 3335 ms 159896 KB Output is correct
13 Correct 1389 ms 84264 KB Output is correct
14 Correct 758 ms 70560 KB Output is correct
15 Correct 3598 ms 159776 KB Output is correct
16 Correct 796 ms 92364 KB Output is correct
17 Correct 791 ms 92440 KB Output is correct
18 Correct 1159 ms 142740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4616 KB Output is correct
2 Correct 712 ms 69180 KB Output is correct
3 Correct 720 ms 67444 KB Output is correct
4 Correct 481 ms 47252 KB Output is correct
5 Correct 825 ms 70440 KB Output is correct
6 Correct 707 ms 67676 KB Output is correct
7 Correct 276 ms 43968 KB Output is correct
8 Correct 397 ms 70972 KB Output is correct
9 Correct 873 ms 90556 KB Output is correct
10 Correct 901 ms 93672 KB Output is correct
11 Correct 877 ms 92276 KB Output is correct
12 Correct 610 ms 66416 KB Output is correct
13 Correct 906 ms 102852 KB Output is correct
14 Correct 983 ms 103084 KB Output is correct
15 Correct 1058 ms 94244 KB Output is correct
16 Correct 976 ms 93032 KB Output is correct
17 Correct 437 ms 66404 KB Output is correct
18 Correct 600 ms 93480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4616 KB Output is correct
2 Correct 712 ms 69180 KB Output is correct
3 Correct 720 ms 67444 KB Output is correct
4 Correct 481 ms 47252 KB Output is correct
5 Correct 825 ms 70440 KB Output is correct
6 Correct 707 ms 67676 KB Output is correct
7 Correct 276 ms 43968 KB Output is correct
8 Correct 397 ms 70972 KB Output is correct
9 Correct 3534 ms 159776 KB Output is correct
10 Correct 3398 ms 159680 KB Output is correct
11 Correct 3392 ms 159896 KB Output is correct
12 Correct 3335 ms 159896 KB Output is correct
13 Correct 1389 ms 84264 KB Output is correct
14 Correct 758 ms 70560 KB Output is correct
15 Correct 3598 ms 159776 KB Output is correct
16 Correct 796 ms 92364 KB Output is correct
17 Correct 791 ms 92440 KB Output is correct
18 Correct 1159 ms 142740 KB Output is correct
19 Correct 873 ms 90556 KB Output is correct
20 Correct 901 ms 93672 KB Output is correct
21 Correct 877 ms 92276 KB Output is correct
22 Correct 610 ms 66416 KB Output is correct
23 Correct 906 ms 102852 KB Output is correct
24 Correct 983 ms 103084 KB Output is correct
25 Correct 1058 ms 94244 KB Output is correct
26 Correct 976 ms 93032 KB Output is correct
27 Correct 437 ms 66404 KB Output is correct
28 Correct 600 ms 93480 KB Output is correct
29 Correct 3684 ms 195104 KB Output is correct
30 Correct 1993 ms 134952 KB Output is correct
31 Correct 3409 ms 188496 KB Output is correct
32 Correct 896 ms 86460 KB Output is correct
33 Correct 3697 ms 188736 KB Output is correct
34 Correct 980 ms 90920 KB Output is correct
35 Correct 3518 ms 218872 KB Output is correct
36 Correct 3576 ms 193268 KB Output is correct
37 Correct 962 ms 117468 KB Output is correct
38 Correct 1401 ms 171700 KB Output is correct
39 Correct 689 ms 97156 KB Output is correct