CF12D Ball(cdq)

Similarly pigeon dimensional partial order for a long time, to ensure that no repeated except that the triples, but larger than is strictly required.

The basic idea of ​​the equal sign with cdq with exactly the same, but to the extreme attention to detail

There are two such triples (111) and (123), in the case where three elements strictly greater than, apparently can not be greater than the second triple of the first triad

This means that in the initial sequence of execution for the first time the sort of time, first by the first dimension descending order

However, if the first dimension are equal, then the third dimension be smaller top surface

In fact statistics answer was always on the left side to the right answers triples contribute

So for the above two-dimensional equivalent of the first triad, we can not let (111) at (123) on the left side, otherwise it will answer error statistics

Know this trick then you can happily get rid of this problem.

Code:

#include <bits/stdc++.h>
#define int long long
#define sc(a) scanf("%lld",&a)
#define scc(a,b) scanf("%lld %lld",&a,&b)
#define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define schar(a) scanf("%c",&a)
#define pr(a) printf("%lld",a)
#define fo(i,a,b) for(int i=a;i<b;++i)
#define re(i,a,b) for(int i=a;i<=b;++i)
#define rfo(i,a,b) for(int i=a;i>b;--i)
#define rre(i,a,b) for(int i=a;i>=b;--i)
#define prn() printf("\n")
#define prs() printf(" ")
#define mkp make_pair
#define pii pair<int,int>
#define pub(a) push_back(a)
#define pob() pop_back()
#define puf(a) push_front(a)
#define pof() pop_front()
#define fst first
#define snd second
#define frt front()
#define bak back()
#define mem0(a) memset(a,0,sizeof(a))
#define memmx(a) memset(a,0x3f3f,sizeof(a))
#define memmn(a) memset(a,-0x3f3f,sizeof(a))
#define debug
#define db double
#define yyes cout<<"YES"<<endl;
#define nno cout<<"NO"<<endl;
using namespace std;
typedef vector<int> vei;
typedef vector<pii> vep;
typedef map<int,int> mpii;
typedef map<char,int> mpci;
typedef map<string,int> mpsi;
typedef deque<int> deqi;
typedef deque<char> deqc;
typedef priority_queue<int> mxpq;
typedef priority_queue<int,vector<int>,greater<int> > mnpq;
typedef priority_queue<pii> mxpqii;
typedef priority_queue<pii,vector<pii>,greater<pii> > mnpqii;
const int maxn=1000005;
const int inf=0x3f3f3f3f3f3f3f3f;
const int MOD=100000007;
const db eps=1e-10;
int qpow(int a,int b){int tmp=a%MOD,ans=1;while(b){if(b&1){ans*=tmp,ans%=MOD;}tmp*=tmp,tmp%=MOD,b>>=1;}return ans;}
int lowbit(int x){return x&-x;}
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
int mmax(int a,int b,int c){return max(a,max(b,c));}
int mmin(int a,int b,int c){return min(a,min(b,c));}
void mod(int &a){a+=MOD;a%=MOD;}
bool chk(int now){}
int half(int l,int r){while(l<=r){int m=(l+r)/2;if(chk(m))r=m-1;else l=m+1;}return l;}
int ll(int p){return p<<1;}
int rr(int p){return p<<1|1;}
int mm(int l,int r){return (l+r)/2;}
int lg(int x){if(x==0) return 1;return (int)log2(x)+1;}
bool smleql(db a,db b){if(a<b||fabs(a-b)<=eps)return true;return false;}
db len(db a,db b,db c,db d){return sqrt((a-c)*(a-c)+(b-d)*(b-d));}
bool isp(int x){if(x==1)return false;if(x==2)return true;for(int i=2;i*i<=x;++i)if(x%i==0)return false;return true;}

struct node{
    int a,b,c,cnt;
}o[maxn],t[maxn];
int n,c[maxn];

bool cmp(node a,node b){
    if(a.a!=b.a) return a.a>b.a;
    else return a.c<b.c;
}

int tr[maxn];
void add(int x,int y){
    for(;x<=n;x+=lowbit(x)) tr[x]+=y;
}
int sum(int x){
    int res=0;
    for(;x;x-=lowbit(x)) res+=tr[x];
    return res;
}

void cdq(int l,int r){
    if(l==r) return;
    int m=mm(l,r);
    cdq(l,m);
    cdq(m+1,r);
    int p=l,q=m+1,tot=l;
    while(p<=m&&q<=r){
        if(o[p].b>o[q].b) add(o[p].c,1),t[tot++]=o[p++];
        else o[q].cnt+=sum(n)-sum(o[q].c),t[tot++]=o[q++];
    }
    while(p<=m) add(o[p].c,1),t[tot++]=o[p++];
    while(q<=r) o[q].cnt+=sum(n)-sum(o[q].c),t[tot++]=o[q++];
    re(i,l,m) add(o[i].c,-1);
    re(i,l,r) o[i]=t[i];
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    re(i,1,n)
        cin>>o[i].a;
    re(i,1,n)
        cin>>o[i].b;
    re(i,1,n)
        cin>>o[i].c,c[i]=o[i].c;
    sort(c+1,c+1+n);
    re(i,1,n) o[i].c=lower_bound(c+1,c+1+n,o[i].c)-c;
    sort(o+1,o+1+n,cmp);
    cdq(1,n);
    int ans=0;
    re(i,1,n){
        if(o[i].cnt){
//            cout<<o[i].a<<' '<<o[i].b<<' '<<c[o[i].c]<<endl;
            ans++;
        }
    }
    cout<<ans;
    return 0;
}
/*
2
1 1
1 2
1 3
*/

 

Guess you like

Origin www.cnblogs.com/oneman233/p/11515497.html