Submission #495782

# Submission time Handle Problem Language Result Execution time Memory
495782 2021-12-20T05:08:00 Z zaneyu Pairs (IOI07_pairs) C++14
100 / 100
254 ms 9656 KB
/*input
3 8 10 20 10 10 10 10 10 20 10 20 10 10 20 20 20 10 10 20 10 20 20 20 10 20 20 20
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=3e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
    out<<P.f+1<<' '<<P.s+1;
    return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
    REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?"\n":"");
    return out;
}
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mult(ll a,ll b,ll mod){
    ll res=0;
    while(b){
        if(b&1) res=(res+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return res;
}
ll mypow(ll a,ll b,ll mod){
    if(b<=0) return 1;
    a%=mod;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}

ll mypow(ll a,ll b){
    a%=MOD;
    if(a==0) return 0;
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
int a[maxn];
pii arr[maxn];
int d,m;
struct bit{
    int bit[maxn];
    void upd(int x,int p){
        x+=m;
        while(x<maxn){
            bit[x]+=p;
            x+=lowb(x);
        }
    }
    int query(int x){
        x+=m;
        int ans=0;
        while(x){
            ans+=bit[x];
            x-=lowb(x);
        }
        return ans;
    }
}ls,mr;
ll ans=0;
void rec(int l,int r){
    if(l==r) return;
    int mid=(l+r)/2;
    vector<pii> v;
    //arr[j].s>arr[i].s
    for(int i=mid+1;i<=r;i++){
        v.pb({arr[i].s,i});
        mr.upd(arr[i].f+arr[i].s,1);
    }
    vector<pii> v2;
    for(int i=mid;i>=l;i--) v2.pb({arr[i].s,i});
    sort(ALL(v)),sort(ALL(v2));
    reverse(ALL(v));
    for(auto x:v2){
        int i=x.s;
        while(sz(v) and v.back().f<=x.f){
            int j=v.back().s;
            mr.upd(arr[j].f+arr[j].s,-1);
            ls.upd(arr[j].f-arr[j].s,1);
            v.pop_back();
        }
        ans+=mr.query(d+arr[i].f+arr[i].s)+ls.query(d+arr[i].f-arr[i].s);
    }
    while(sz(v)){
        int j=v.back().s;
        mr.upd(arr[j].f+arr[j].s,-1);
        ls.upd(arr[j].f-arr[j].s,1);
        v.pop_back();
    }
    for(int i=mid+1;i<=r;i++) ls.upd(arr[i].f-arr[i].s,-1);
    rec(l,mid),rec(mid+1,r);
}
int pref[80][160][160];
pair<int,pii> b[maxn];
int get(int i,int a,int b,int c,int d){
    MXTO(a,0),MXTO(b,0),MNTO(c,2*m),MNTO(d,2*m);
    return pref[i][c][d]-pref[i][a][d]-pref[i][c][b]+pref[i][a][b];
}
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int B,n;
    cin>>B>>n>>d>>m;
    if(B==1){
        REP(i,n) cin>>a[i];
        sort(a,a+n);
        ll ans=0; 
        REP(i,n) ans+=upper_bound(a,a+n,a[i]+d)-lower_bound(a,a+n,a[i]-d);
        cout<<(ans-n)/2;
        return 0;
    }
    if(B==2){
        REP(i,n) cin>>arr[i].f>>arr[i].s;
        sort(arr,arr+n);
        rec(0,n-1);
        cout<<ans;
        return 0;
    }
    REP(i,n){
        int x,y,z;
        cin>>x>>y>>z;
        pref[x][y-z+m][y+z]++;
        b[i]={x,{y-z+m,y+z}};
    }
    REP1(i,m){
        REP1(j,2*m){
            REP1(k,2*m){
                pref[i][j][k]+=pref[i][j-1][k]+pref[i][j][k-1]-pref[i][j-1][k-1];
            }
        }
    }
    REP(i,n){
        REP1(j,m){
            int D=d-abs(b[i].f-j);
            if(D<0) continue;
            ans+=get(j,b[i].s.f-D-1,b[i].s.s-D-1,b[i].s.f+D,b[i].s.s+D);
        }
    }
    cout<<(ans-n)/2;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 1072 KB Output is correct
2 Correct 17 ms 1064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 1488 KB Output is correct
2 Correct 21 ms 1464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1496 KB Output is correct
2 Correct 24 ms 1568 KB Output is correct
3 Correct 21 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1488 KB Output is correct
2 Correct 2 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 195 ms 3428 KB Output is correct
2 Correct 187 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 3620 KB Output is correct
2 Correct 218 ms 3532 KB Output is correct
3 Correct 198 ms 3652 KB Output is correct
4 Correct 217 ms 3724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 214 ms 5056 KB Output is correct
2 Correct 226 ms 5080 KB Output is correct
3 Correct 209 ms 5096 KB Output is correct
4 Correct 210 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7504 KB Output is correct
2 Correct 7 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2256 KB Output is correct
2 Correct 24 ms 2168 KB Output is correct
3 Correct 22 ms 2240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 7152 KB Output is correct
2 Correct 141 ms 7128 KB Output is correct
3 Correct 66 ms 7148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 9540 KB Output is correct
2 Correct 254 ms 9656 KB Output is correct
3 Correct 94 ms 9484 KB Output is correct