Submission #989986

# Submission time Handle Problem Language Result Execution time Memory
989986 2024-05-29T10:02:25 Z PedroBigMan Pairs (IOI07_pairs) C++14
100 / 100
143 ms 27104 KB
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro "<<i<<endl
#define INF 1000000000000000000LL
#define EPS ((ld)0.00000000001)
#define pi ((ld)3.141592653589793)
#define VV(vvvv,NNNN,xxxx); REP(iiiii,0,NNNN) {vvvv.pb(xxxx);}
ll mod=1000000007;

template<class A=ll> 
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} 

class DynamicST
{
    public:
    ll N; 
    
    class SV //seg value
    {
        public:
        ll a; 
        SV() {a=0LL;}
        SV(ll x) {a=x;}
        
        SV operator & (SV X) {SV ANS(a+X.a); return ANS;}
    };
      
    class LV //lazy value
    {
        public:
        ll a;
        LV() {a=0LL;}
        LV(ll x) {a=x;}
        
        LV operator & (LV X) {LV ANS(a+X.a); return ANS;}
    };
    
    SV neuts; LV neutl;
    
    class node
    {
        public:
        ll ind;
        SV sv; LV lv;
        ll l,r; //range
        
        node *lson, *rson;
        
        node(ll ind2, SV sv2, LV lv2, node * par) 
        {
            ind=ind2; sv=sv2; lv=lv2; lson=nullptr; rson=nullptr; 
            if(ind==1) {l=0LL;}
            else
            {
                if(ind%2==0) 
                {
                    par->lson=this;
                    l=par->l; r=(par->l+par->r)/2LL;
                }
                else 
                {
                    par->rson=this;
                    l=(par->l+par->r+1)/2; r=par->r;
                }
            }
        }
    };
    
    node *root;
    
    SV upval(node *X) //how lazy values modify a seg value inside a node, c=current node
    {
        SV ANS((X->sv).a+(X->r-X->l+1)*(X->lv).a);
        return ANS;
    }
    
    DynamicST(ll n)
    {
        N = (ll) 1<<(ll) ceil(log2(n));
        node *X = new node(1,neuts,neutl,nullptr);
        root=X; root->r=N-1LL;
    }
    
    void prop(node *Y) //how lazy values propagate
    {
        Y->lson->lv=Y->lv&Y->lson->lv; Y->rson->lv=Y->lv&Y->rson->lv;
        Y->lv=neutl;
    }
    
    SV query(ll a,ll b, node *Y) //range [a,b], current node. initially: query(a,b,root)
    {
        if(a>b) {return neuts;}
        ll x=Y->l; ll y=Y->r;
        if(y<a || x>b) {return neuts;}
        if(x>=a && y<=b) {return upval(Y);}
        if(Y->lson==nullptr) {node *X=new node(2*Y->ind,neuts,neutl,Y);}
        if(Y->rson==nullptr) {node *X=new node(2*Y->ind+1,neuts,neutl,Y);}
        prop(Y); 
		Y->sv=upval(Y->lson)&upval(Y->rson);
        SV ans = query(a,b,Y->lson)&query(a,b,Y->rson);
        return ans;
    }
    
    void update(LV s, ll a, ll b, node *Y) //update LV, range [a,b], current node, current range. initially: update(s,a,b,root)
    {
		if(a>b) {return;}
        ll x=Y->l; ll y=Y->r;
        if(y<a || x>b) {return ;}
        if(x>=a && y<=b) 
        {
            Y->lv=s&Y->lv; 
            return;
        }
        if(Y->lson==nullptr) {node *X=new node(2*Y->ind,neuts,neutl,Y);}
        if(Y->rson==nullptr) {node *X=new node(2*Y->ind+1,neuts,neutl,Y);}
		prop(Y);
        update(s,a,b,Y->lson); update(s,a,b,Y->rson);
        Y->sv=upval(Y->lson)&upval(Y->rson);
    }
};

class Queries_2D
{
    public:
    ll M; ll N; vector<pl> p; 
    vector<vector<ll> > ps; 
    ll newx, newy;
    
    Queries_2D() {M=0;}
    Queries_2D(ll thisM, vector<pl> thisp)
    {
        M=thisM; p=thisp; N=p.size();
        REP(i,0,N) {p[i]={p[i].ff+p[i].ss,p[i].ff-p[i].ss+M};}
        sort(whole(p));
        ps = vector<vector<ll> >(2*M,vector<ll>(2*M,0));
        REP(i,0,N) {ps[p[i].ff][p[i].ss]++;}
        REP(i,0,2*M)
        {
            REP(j,0,2*M)
            {
                if(i>0) {ps[i][j]+=ps[i-1][j];}
                if(j>0) {ps[i][j]+=ps[i][j-1];}
                if(i>0 && j>0) {ps[i][j]-=ps[i-1][j-1];}
            }
        }
    }

    ll PS(ll x, ll y)
    {
        if(x<0 || y<0) {return 0;}
        x=min(x,2*M-1); y=min(y,2*M-1);
        return ps[x][y];
    }

    ll query(ll x, ll y, ll D)
    {
        newx = x+y; newy = x-y+M; x=newx; y=newy;
        return PS(x+D,y+D)-PS(x+D,y-D-1)-PS(x-D-1,y+D)+PS(x-D-1,y-D-1);
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	cout.precision(20);
	ll B, N, D, M; cin>>B>>N>>D>>M; ll ans = 0LL;
    vector<vector<ll> > p(N,vector<ll>(B,0));
    REP(i,0,N) {REP(j,0,B) {cin>>p[i][j];}}
    if(N<=10000)
    {
        ll dist;
        REP(i,0,N)
        {
            REP(j,i+1,N)
            {
                dist=0LL;
                REP(b,0,B) {dist+=abs(p[i][b]-p[j][b]);}
                if(dist<=D) {ans++;}
            }
        }
        cout<<ans<<endl; return 0;
    }
    if(B==1)
    {
        vector<ll> a; REP(i,0,N) {a.pb(p[i][0]);} sort(whole(a)); ll pos;
        REP(i,0,N)
        {   
            pos = (ll) (lower_bound(whole(a), a[i]-D) - a.begin());
            ans+=(i-pos);
        }   
        cout<<ans<<endl; return 0;
    }
    if(B==2)
    {
        vector<pl> a; REP(i,0,N) {a.pb({-p[i][0]+p[i][1],p[i][1]+p[i][0]});}
        sort(whole(a)); ll ind=0; ll Y;
        DynamicST S(200000);
        REP(i,0,N)
        {
            while(ind<N && a[ind].ff<a[i].ff-D) {Y=a[ind].ss; S.update(-1,Y,Y,S.root); ind++;}
            Y=a[i].ss;
            ans+=S.query(Y-D,Y+D,S.root).a;
            S.update(1,Y,Y,S.root);
        }
        cout<<ans<<endl;
    }
    if(B==3)
    {
        REP(i,0,N) {REP(b,0,B) {p[i][b]--;}}
        vector<vector<pl> > points_z(M,vector<pl>());
        REP(i,0,N) {points_z[p[i][2]].pb({p[i][0],p[i][1]});}
        vector<Queries_2D> Q;
        REP(i,0,M) {Q.pb(Queries_2D(M,points_z[i]));}
        ll X, Y, Z, thisD;
        REP(i,0,N)
        {
            X=p[i][0]; Y=p[i][1]; Z=p[i][2];
            for(ll z = max(0LL,Z-D); z<=min(M-1,Z+D); z++)
            {
                if(z>Z) {thisD = D-(z-Z);} else {thisD= D-(Z-z);}
                ans+=Q[z].query(X,Y,thisD);
            }
            ans--;
        }
        ans/=2LL;
        cout<<ans<<endl;
    }
    return 0;
}

Compilation message

pairs.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization ("O3")
      | 
pairs.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("unroll-loops")
      | 
pairs.cpp: In member function 'DynamicST::SV DynamicST::query(ll, ll, DynamicST::node*)':
pairs.cpp:127:37: warning: unused variable 'X' [-Wunused-variable]
  127 |         if(Y->lson==nullptr) {node *X=new node(2*Y->ind,neuts,neutl,Y);}
      |                                     ^
pairs.cpp:128:37: warning: unused variable 'X' [-Wunused-variable]
  128 |         if(Y->rson==nullptr) {node *X=new node(2*Y->ind+1,neuts,neutl,Y);}
      |                                     ^
pairs.cpp: In member function 'void DynamicST::update(DynamicST::LV, ll, ll, DynamicST::node*)':
pairs.cpp:145:37: warning: unused variable 'X' [-Wunused-variable]
  145 |         if(Y->lson==nullptr) {node *X=new node(2*Y->ind,neuts,neutl,Y);}
      |                                     ^
pairs.cpp:146:37: warning: unused variable 'X' [-Wunused-variable]
  146 |         if(Y->rson==nullptr) {node *X=new node(2*Y->ind+1,neuts,neutl,Y);}
      |                                     ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 6864 KB Output is correct
2 Correct 15 ms 6868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6868 KB Output is correct
2 Correct 21 ms 6864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6864 KB Output is correct
2 Correct 19 ms 6868 KB Output is correct
3 Correct 18 ms 6868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 7888 KB Output is correct
2 Correct 44 ms 7888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 7888 KB Output is correct
2 Correct 83 ms 8108 KB Output is correct
3 Correct 64 ms 7884 KB Output is correct
4 Correct 72 ms 8108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 23236 KB Output is correct
2 Correct 137 ms 27104 KB Output is correct
3 Correct 70 ms 12704 KB Output is correct
4 Correct 75 ms 11936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 9556 KB Output is correct
2 Correct 28 ms 10320 KB Output is correct
3 Correct 24 ms 10324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 16468 KB Output is correct
2 Correct 99 ms 17236 KB Output is correct
3 Correct 43 ms 17244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 23388 KB Output is correct
2 Correct 143 ms 23440 KB Output is correct
3 Correct 67 ms 23388 KB Output is correct