Submission #673525

# Submission time Handle Problem Language Result Execution time Memory
673525 2022-12-20T21:02:30 Z Carmel_Ab1 Examination (JOI19_examination) C++17
100 / 100
1432 ms 555492 KB
/*
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
 */
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;
using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int>vi;
typedef vector<vector<int>>vvi;
typedef vector<ll>vl;
typedef vector<vl> vvl;
typedef pair<int,int>pi;
typedef pair<ll,ll> pl;
typedef vector<pl> vpl;
typedef vector<ld> vld;
typedef pair<ld,ld> pld;
typedef vector<pi> vpi;

//typedef tree<ll, null_type, less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
template<typename T> ostream& operator<<(ostream& os, vector<T>& a){os<<"[";for(int i=0; i<ll(a.size()); i++){os << a[i] << ((i!=ll(a.size()-1)?" ":""));}os << "]\n"; return os;}

#define all(x) x.begin(),x.end()
#define YES out("YES")
#define NO out("NO")
#define out(x){cout << x << "\n"; return;}
#define outfl(x){cout << x << endl;return;}
#define GLHF ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define print(x){for(auto ait:x) cout << ait << " "; cout << "\n";}
#define pb push_back
#define umap unordered_map


template<typename T>
void read(vector<T>& v){
    int n=v.size();
    for(int i=0; i<n; i++)
        cin >> v[i];
}
template<typename T>
vector<T>UNQ(vector<T>a){
    vector<T>ans;
    for(T t:a)
        if(ans.empty() || t!=ans.back())
            ans.push_back(t);
    return ans;
}



void solve();
int main(){
    GLHF;
    int t=1;
    //cin >> t;
    while(t--)
        solve();
}
struct seg{
    seg* lp=0,*rp=0;
    int l,r,m;
    int sum=0;

    seg(int l,int r):l(l),r(r),m((l+r)/2){}

    void expand(){
        if(!lp)lp=new seg(l,m);
        if(!rp)rp=new seg(m,r);
    }
    void upd(int i,int v){
        sum+=v;
        if(l+1==r)
            return;
        expand();
        if(i<m)
            lp->upd(i,v);
        else
            rp->upd(i,v);
    }

    int qur(int a,int b){
        if(b<=l || r<=a)
            return 0;
        if(a<=l && r<=b)
            return sum;
        expand();
        return lp->qur(a,b) + rp->qur(a,b);
    }
};

/*
 Basically what we do is split the queries into 2 types:
 1. queries where A+B>=C, then we only need to count S>=A, T>=B, (and then it applies that S+T>=A+B>=C).
 So we only need to count how many fulfil S>=A, T>=B.
 This can be done offline - sorting queries and pairs by S/A, then querying a segment tree on [B,inf].


 */
void solve() {
    int n,q;
    cin >> n >> q;
    vvl a(n,vl(2));
    for(int i=0 ;i<n; i++)
        cin >> a[i][0] >> a[i][1];

    vvl queries(q,vl(4));
    for(int i=0; i<q; i++){
        cin >>queries[i][0] >> queries[i][1] >> queries[i][2];
        queries[i][3]=i;
    }
    sort(all(a));
    sort(all(queries));

    vi ans(q);
    seg ST(0,1e9 +10);

    for(int i=0; i<n; i++)
        ST.upd(a[i][1],1);

    for(int i=0,j=0; i<q; i++){
        while(j<n && a[j][0]<queries[i][0])
            ST.upd(a[j++][1],-1);
        if(queries[i][0]+queries[i][1]>=queries[i][2])
            ans[queries[i][3]] = ST.qur(queries[i][1],1e9 + 2);
    }

    sort(all(a),[&](vl v1,vl v2){
        return v1[0]+v1[1] < v2[0]+v2[1];
    });
    sort(all(queries),[&](vl v1,vl v2){
       return v1[2]<v2[2];
    });
    ST = seg(0,1e9 + 10);
    for(int i=0; i<n;i++)
        ST.upd(a[i][1],1);

    for(int i=0,j=0; i<q; i++){
        ll A=queries[i][0],B=queries[i][1],C=queries[i][2],ix=queries[i][3];
        if(A+B>=C)
            continue;
        while(j<n && a[j][0]+a[j][1]<C)
            ST.upd(a[j][1],-1), j++;
        ans[ix] = ST.qur(B,1e9 + 2);
    }


    ST = seg(0,1e9 + 10);
    for(int i=0; i<n;i++)
        ST.upd(a[i][0],1);
    for(int i=0,j=0; i<q; i++){
        ll A=queries[i][0],B=queries[i][1],C=queries[i][2],ix=queries[i][3];
        if(A+B>=C)
            continue;
        while(j<n && a[j][0]+a[j][1]<C)
            ST.upd(a[j][0],-1), j++;
        ans[ix] -= ST.qur(0,A);
    }
    

    for(int x:ans)
        cout << x << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 31 ms 22988 KB Output is correct
8 Correct 39 ms 23160 KB Output is correct
9 Correct 31 ms 23076 KB Output is correct
10 Correct 20 ms 8212 KB Output is correct
11 Correct 23 ms 15624 KB Output is correct
12 Correct 8 ms 728 KB Output is correct
13 Correct 26 ms 20760 KB Output is correct
14 Correct 27 ms 20756 KB Output is correct
15 Correct 26 ms 20712 KB Output is correct
16 Correct 21 ms 15484 KB Output is correct
17 Correct 16 ms 8264 KB Output is correct
18 Correct 7 ms 752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 494 ms 41536 KB Output is correct
2 Correct 491 ms 41476 KB Output is correct
3 Correct 540 ms 41712 KB Output is correct
4 Correct 352 ms 23600 KB Output is correct
5 Correct 450 ms 32804 KB Output is correct
6 Correct 316 ms 14512 KB Output is correct
7 Correct 424 ms 41556 KB Output is correct
8 Correct 508 ms 41188 KB Output is correct
9 Correct 422 ms 41136 KB Output is correct
10 Correct 359 ms 32588 KB Output is correct
11 Correct 372 ms 23372 KB Output is correct
12 Correct 256 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 494 ms 41536 KB Output is correct
2 Correct 491 ms 41476 KB Output is correct
3 Correct 540 ms 41712 KB Output is correct
4 Correct 352 ms 23600 KB Output is correct
5 Correct 450 ms 32804 KB Output is correct
6 Correct 316 ms 14512 KB Output is correct
7 Correct 424 ms 41556 KB Output is correct
8 Correct 508 ms 41188 KB Output is correct
9 Correct 422 ms 41136 KB Output is correct
10 Correct 359 ms 32588 KB Output is correct
11 Correct 372 ms 23372 KB Output is correct
12 Correct 256 ms 14156 KB Output is correct
13 Correct 618 ms 42380 KB Output is correct
14 Correct 553 ms 43468 KB Output is correct
15 Correct 466 ms 42828 KB Output is correct
16 Correct 423 ms 24864 KB Output is correct
17 Correct 500 ms 33868 KB Output is correct
18 Correct 331 ms 14776 KB Output is correct
19 Correct 604 ms 43804 KB Output is correct
20 Correct 659 ms 43656 KB Output is correct
21 Correct 609 ms 43488 KB Output is correct
22 Correct 370 ms 33292 KB Output is correct
23 Correct 326 ms 24088 KB Output is correct
24 Correct 256 ms 14328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 31 ms 22988 KB Output is correct
8 Correct 39 ms 23160 KB Output is correct
9 Correct 31 ms 23076 KB Output is correct
10 Correct 20 ms 8212 KB Output is correct
11 Correct 23 ms 15624 KB Output is correct
12 Correct 8 ms 728 KB Output is correct
13 Correct 26 ms 20760 KB Output is correct
14 Correct 27 ms 20756 KB Output is correct
15 Correct 26 ms 20712 KB Output is correct
16 Correct 21 ms 15484 KB Output is correct
17 Correct 16 ms 8264 KB Output is correct
18 Correct 7 ms 752 KB Output is correct
19 Correct 494 ms 41536 KB Output is correct
20 Correct 491 ms 41476 KB Output is correct
21 Correct 540 ms 41712 KB Output is correct
22 Correct 352 ms 23600 KB Output is correct
23 Correct 450 ms 32804 KB Output is correct
24 Correct 316 ms 14512 KB Output is correct
25 Correct 424 ms 41556 KB Output is correct
26 Correct 508 ms 41188 KB Output is correct
27 Correct 422 ms 41136 KB Output is correct
28 Correct 359 ms 32588 KB Output is correct
29 Correct 372 ms 23372 KB Output is correct
30 Correct 256 ms 14156 KB Output is correct
31 Correct 618 ms 42380 KB Output is correct
32 Correct 553 ms 43468 KB Output is correct
33 Correct 466 ms 42828 KB Output is correct
34 Correct 423 ms 24864 KB Output is correct
35 Correct 500 ms 33868 KB Output is correct
36 Correct 331 ms 14776 KB Output is correct
37 Correct 604 ms 43804 KB Output is correct
38 Correct 659 ms 43656 KB Output is correct
39 Correct 609 ms 43488 KB Output is correct
40 Correct 370 ms 33292 KB Output is correct
41 Correct 326 ms 24088 KB Output is correct
42 Correct 256 ms 14328 KB Output is correct
43 Correct 1432 ms 547076 KB Output is correct
44 Correct 1219 ms 547368 KB Output is correct
45 Correct 1099 ms 512776 KB Output is correct
46 Correct 598 ms 193152 KB Output is correct
47 Correct 837 ms 368924 KB Output is correct
48 Correct 375 ms 14592 KB Output is correct
49 Correct 1063 ms 536600 KB Output is correct
50 Correct 1262 ms 555492 KB Output is correct
51 Correct 1099 ms 554444 KB Output is correct
52 Correct 792 ms 368668 KB Output is correct
53 Correct 422 ms 142336 KB Output is correct