Submission #127598

# Submission time Handle Problem Language Result Execution time Memory
127598 2019-07-09T16:01:33 Z Mercenary Examination (JOI19_examination) C++14
100 / 100
894 ms 43372 KB
#include<bits/stdc++.h>

using namespace std;
#define taskname "A"
#define pb	push_back
#define mp 	make_pair
#ifndef LOCAL
#define cerr if(0)cout
#endif

typedef long double ld;
typedef long long ll;
typedef pair<int,int> ii;
const int N = 2e5 + 5;

int n , q;
vector<int> ID[N];
vector<int> BIT[N];

struct Node{
    int x , y , sum , id;
}a[N] , b[N];

void FakeUpdate(int x , int y){
    for( ; x > 0 ; x &= x - 1){
        ID[x].pb(y);
    }
}

void FakeQuery(int x , int y){
    for( ; x < N ; x += x & -x){
        ID[x].pb(y);
    }
}

int query(int x ,int yy){
    int res = 0;
    for( ; x < N ; x += x & -x){
        for(int y = lower_bound(ID[x].begin(),ID[x].end(),yy) - ID[x].begin() + 1
            ; y <= ID[x].size() ; y += y & -y){
            res += BIT[x][y - 1];
        }
    }
    return res;
}

void update(int x , int yy){
//    int res = 0;
    for( ; x > 0 ; x &= x - 1){
        for(int y = lower_bound(ID[x].begin(),ID[x].end(),yy) - ID[x].begin() + 1
            ; y > 0 ; y &= y - 1){
            ++BIT[x][y - 1];
        }
    }
//    return res;
}

void compress(){
    vector<int> X , Y , SUM;
    X.reserve(n + q);Y.reserve(n + q);SUM.reserve(n + q);
    for(int i = 1 ; i <= n ; ++i){
        X.pb(a[i].x);Y.pb(a[i].y);SUM.pb(a[i].x + a[i].y);
        a[i].sum = a[i].x + a[i].y;
    }
    for(int i = 1 ; i <= q ; ++i){
        X.pb(b[i].x);Y.pb(b[i].y);SUM.pb(b[i].sum);
    }
    sort(X.begin(),X.end());X.erase(unique(X.begin(),X.end()),X.end());
    sort(Y.begin(),Y.end());Y.erase(unique(Y.begin(),Y.end()),Y.end());
    sort(SUM.begin(),SUM.end());SUM.erase(unique(SUM.begin(),SUM.end()),SUM.end());
    for(int i = 1 ; i <= n ; ++i){
        a[i].x = lower_bound(X.begin(),X.end(),a[i].x) - X.begin() + 1;
        a[i].y = lower_bound(Y.begin(),Y.end(),a[i].y) - Y.begin() + 1;
        a[i].sum = lower_bound(SUM.begin(),SUM.end(),a[i].sum) - SUM.begin() + 1;
    }
    for(int i = 1 ; i <= q ; ++i){
        b[i].x = lower_bound(X.begin(),X.end(),b[i].x) - X.begin() + 1;
        b[i].y = lower_bound(Y.begin(),Y.end(),b[i].y) - Y.begin() + 1;
        b[i].sum = lower_bound(SUM.begin(),SUM.end(),b[i].sum) - SUM.begin() + 1;
    }
    for(int i = 1 ; i <= n ; ++i){
        FakeUpdate(a[i].x , a[i].y);
    }
    for(int i = 1 ; i <= q ; ++i){
        FakeQuery(b[i].x , b[i].y);
    }
    for(int i = 1 ; i < N ; ++i){
        sort(ID[i].begin(),ID[i].end());
        ID[i].erase(unique(ID[i].begin(),ID[i].end()),ID[i].end());
        BIT[i].resize(ID[i].size());
    }
}

int res[N];

void solve(){
    sort(b + 1 , b + q + 1 , [](const Node x , const Node y){
        return x.sum < y.sum;
         });
    sort(a + 1 , a + n + 1 , [](const Node x , const Node y){
        return x.sum < y.sum;
         });
    int j = n;
    for(int i = q ; i >= 1 ; --i){
        while(j >= 1 && a[j].sum >= b[i].sum){
            update(a[j].x,a[j].y);
//            cout << a[j].x << " " << a[j].y << endl;
            --j;
        }
//        cout << b[i].id << " " << b[i].x << " " << b[i].y << endl;
        res[b[i].id] = query(b[i].x , b[i].y);
    }
    for(int i = 1 ; i <= q ; ++i)cout << res[i] << '\n';
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen(taskname".INP","r")){
		freopen(taskname".INP", "r",stdin);
		freopen(taskname".OUT", "w",stdout);
    }
    cin >> n >> q;
    for(int i = 1 ; i <= n ; ++i){
        cin >> a[i].x >> a[i].y;
    }
    for(int i = 1 ; i <= q ; ++i){
        cin >> b[i].x >> b[i].y >> b[i].sum;
        b[i].id = i;
    }
    compress();
    solve();
}

Compilation message

examination.cpp: In function 'int query(int, int)':
examination.cpp:40:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             ; y <= ID[x].size() ; y += y & -y){
               ~~^~~~~~~~~~~~~~~
examination.cpp: In function 'int main()':
examination.cpp:121:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".INP", "r",stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:122:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".OUT", "w",stdout);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9720 KB Output is correct
2 Correct 16 ms 9720 KB Output is correct
3 Correct 20 ms 9724 KB Output is correct
4 Correct 12 ms 9720 KB Output is correct
5 Correct 12 ms 9848 KB Output is correct
6 Correct 12 ms 9720 KB Output is correct
7 Correct 27 ms 10872 KB Output is correct
8 Correct 27 ms 10872 KB Output is correct
9 Correct 27 ms 10872 KB Output is correct
10 Correct 22 ms 10620 KB Output is correct
11 Correct 25 ms 10616 KB Output is correct
12 Correct 18 ms 10360 KB Output is correct
13 Correct 27 ms 10872 KB Output is correct
14 Correct 26 ms 10872 KB Output is correct
15 Correct 26 ms 10872 KB Output is correct
16 Correct 23 ms 10616 KB Output is correct
17 Correct 20 ms 10616 KB Output is correct
18 Correct 17 ms 10232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 727 ms 37220 KB Output is correct
2 Correct 737 ms 37156 KB Output is correct
3 Correct 722 ms 37188 KB Output is correct
4 Correct 382 ms 31528 KB Output is correct
5 Correct 622 ms 29932 KB Output is correct
6 Correct 211 ms 24444 KB Output is correct
7 Correct 728 ms 36332 KB Output is correct
8 Correct 693 ms 36512 KB Output is correct
9 Correct 700 ms 34996 KB Output is correct
10 Correct 592 ms 29924 KB Output is correct
11 Correct 335 ms 31076 KB Output is correct
12 Correct 168 ms 24948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 727 ms 37220 KB Output is correct
2 Correct 737 ms 37156 KB Output is correct
3 Correct 722 ms 37188 KB Output is correct
4 Correct 382 ms 31528 KB Output is correct
5 Correct 622 ms 29932 KB Output is correct
6 Correct 211 ms 24444 KB Output is correct
7 Correct 728 ms 36332 KB Output is correct
8 Correct 693 ms 36512 KB Output is correct
9 Correct 700 ms 34996 KB Output is correct
10 Correct 592 ms 29924 KB Output is correct
11 Correct 335 ms 31076 KB Output is correct
12 Correct 168 ms 24948 KB Output is correct
13 Correct 804 ms 37344 KB Output is correct
14 Correct 767 ms 37416 KB Output is correct
15 Correct 729 ms 37320 KB Output is correct
16 Correct 434 ms 31968 KB Output is correct
17 Correct 653 ms 30420 KB Output is correct
18 Correct 221 ms 24324 KB Output is correct
19 Correct 797 ms 37408 KB Output is correct
20 Correct 799 ms 37348 KB Output is correct
21 Correct 783 ms 35820 KB Output is correct
22 Correct 590 ms 29996 KB Output is correct
23 Correct 326 ms 31068 KB Output is correct
24 Correct 166 ms 24948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9720 KB Output is correct
2 Correct 16 ms 9720 KB Output is correct
3 Correct 20 ms 9724 KB Output is correct
4 Correct 12 ms 9720 KB Output is correct
5 Correct 12 ms 9848 KB Output is correct
6 Correct 12 ms 9720 KB Output is correct
7 Correct 27 ms 10872 KB Output is correct
8 Correct 27 ms 10872 KB Output is correct
9 Correct 27 ms 10872 KB Output is correct
10 Correct 22 ms 10620 KB Output is correct
11 Correct 25 ms 10616 KB Output is correct
12 Correct 18 ms 10360 KB Output is correct
13 Correct 27 ms 10872 KB Output is correct
14 Correct 26 ms 10872 KB Output is correct
15 Correct 26 ms 10872 KB Output is correct
16 Correct 23 ms 10616 KB Output is correct
17 Correct 20 ms 10616 KB Output is correct
18 Correct 17 ms 10232 KB Output is correct
19 Correct 727 ms 37220 KB Output is correct
20 Correct 737 ms 37156 KB Output is correct
21 Correct 722 ms 37188 KB Output is correct
22 Correct 382 ms 31528 KB Output is correct
23 Correct 622 ms 29932 KB Output is correct
24 Correct 211 ms 24444 KB Output is correct
25 Correct 728 ms 36332 KB Output is correct
26 Correct 693 ms 36512 KB Output is correct
27 Correct 700 ms 34996 KB Output is correct
28 Correct 592 ms 29924 KB Output is correct
29 Correct 335 ms 31076 KB Output is correct
30 Correct 168 ms 24948 KB Output is correct
31 Correct 804 ms 37344 KB Output is correct
32 Correct 767 ms 37416 KB Output is correct
33 Correct 729 ms 37320 KB Output is correct
34 Correct 434 ms 31968 KB Output is correct
35 Correct 653 ms 30420 KB Output is correct
36 Correct 221 ms 24324 KB Output is correct
37 Correct 797 ms 37408 KB Output is correct
38 Correct 799 ms 37348 KB Output is correct
39 Correct 783 ms 35820 KB Output is correct
40 Correct 590 ms 29996 KB Output is correct
41 Correct 326 ms 31068 KB Output is correct
42 Correct 166 ms 24948 KB Output is correct
43 Correct 868 ms 43276 KB Output is correct
44 Correct 855 ms 43372 KB Output is correct
45 Correct 850 ms 43344 KB Output is correct
46 Correct 512 ms 38256 KB Output is correct
47 Correct 747 ms 33324 KB Output is correct
48 Correct 230 ms 24440 KB Output is correct
49 Correct 894 ms 43064 KB Output is correct
50 Correct 847 ms 43244 KB Output is correct
51 Correct 842 ms 43116 KB Output is correct
52 Correct 707 ms 34032 KB Output is correct
53 Correct 446 ms 37704 KB Output is correct