hdu5618 Jam's problem again

想用动态开点的二维线段树水一下,然而TLE了。。。有人线段树套平衡树都过了。。。可能线段树套线段树再加动态开点常数确实大。。。

留着等刷完第三章习题后再搞树套树,和cdq分治一起搞,等学完cdq分治我一定会回来用正解过这题的,今天没过的代码先留着。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
//const int maxn=1000100;
const int INF=1e9+10;

const int N=100010;
int n;
struct Point
{
    int x,y,z;
    int id;
    int ans;
    friend bool operator<(Point A,Point B)
    {
        return A.z<B.z;
    }
};Point p[N];
struct Node
{
    int l,r;
    int lz,rz;
    int cnt;
    int lx,rx;
    int ly,ry;
};Node tr[N*8];int trn;
int s[N*8],trn2;
int rt;

bool cmp(Point A,Point B)
{
    return A.id<B.id;
}

int newnode(int l,int r,int lz,int rz)
{
    int k;
    if(trn2) k=s[trn2--];
    else k=++trn;
    tr[k]={l,r,lz,rz,0,-1,-1,-1,-1};
    return k;
}

void Init(int l,int r,int lz,int rz)
{
    trn=trn2=0;
    rt=newnode(l,r,lz,rz);
}

void Free(int &k)
{
    if(k==-1) return;
    Free(tr[k].lx);
    Free(tr[k].rx);
    Free(tr[k].ly);
    Free(tr[k].ry);
    s[++trn2]=k;
    k=-1;
}

void upy(int rt)
{
    tr[rt].cnt=0;
    if(~tr[rt].ly){
        tr[rt].cnt+=tr[tr[rt].ly].cnt;
        if(tr[tr[rt].ly].cnt==0) Free(tr[rt].ly);
    }
    if(~tr[rt].ry){
        tr[rt].cnt+=tr[tr[rt].ry].cnt;
        if(tr[tr[rt].ry].cnt==0) Free(tr[rt].ry);
    }
}

void updatey(int p,int c,int rt)
{
    int l=tr[rt].l,r=tr[rt].r;
    int lz=tr[rt].lz,rz=tr[rt].rz;
    if(lz==rz){
        tr[rt].cnt++;
        return;
    }
    int m=(lz+rz)>>1;
    if(p<=m){
        if(tr[rt].ly==-1) tr[rt].ly=newnode(l,r,lz,m);
        updatey(p,c,tr[rt].ly);
    }
    else{
        if(tr[rt].ry==-1) tr[rt].ry=newnode(l,r,m+1,rz);
        updatey(p,c,tr[rt].ry);
    }
    upy(rt);
}

int queryy(int L,int R,int &rt)
{
    if(tr[rt].cnt==0&&rt!=1){
        Free(rt);
        return 0;
    }
    int l=tr[rt].l,r=tr[rt].r;
    int lz=tr[rt].lz,rz=tr[rt].rz;
    if(L<=lz&&rz<=R) return tr[rt].cnt;
    int m=(lz+rz)>>1;
    int res=0;
    if(L<=m){
        if(~tr[rt].ly) res+=queryy(L,R,tr[rt].ly);
    }
    if(R>m){
        if(~tr[rt].ry) res+=queryy(L,R,tr[rt].ry);
    }
    return res;
}

void updatex(int x,int y,int c,int rt)
{
    //cout<<"x="<<x<<" y="<<y<<" c="<<c<<" rt="<<rt<<endl;
    int l=tr[rt].l,r=tr[rt].r;
    int lz=tr[rt].lz,rz=tr[rt].rz;
    //cout<<"x="<<x<<" y="<<y<<" c="<<c<<" l="<<l<<" r="<<r<<" lz="<<lz<<" rz="<<rz<<endl;
    updatey(y,c,rt);
    if(l==r) return;
    int m=(l+r)>>1;
    if(x<=m){
        if(tr[rt].lx==-1) tr[rt].lx=newnode(l,m,lz,rz);
        updatex(x,y,c,tr[rt].lx);
    }
    else{
        if(tr[rt].rx==-1) tr[rt].rx=newnode(m+1,r,lz,rz);
        updatex(x,y,c,tr[rt].rx);
    }
}

int queryx(int xL,int xR,int yL,int yR,int &rt)
{
    if(tr[rt].cnt==0&&rt!=1){
        Free(rt);
        return 0;
    }
    int l=tr[rt].l,r=tr[rt].r;
    int lz=tr[rt].lz,rz=tr[rt].rz;
    //cout<<"xL="<<xL<<" xR="<<xR<<" yL="<<yL<<" yR="<<yR<<" l="<<l<<" r="<<r<<" lz="<<lz<<" rz="<<rz<<endl;
    if(xL<=l&&r<=xR) return queryy(yL,yR,rt);
    int m=(l+r)>>1;
    int res=0;
    if(xL<=m){
        if(~tr[rt].lx) return res+=queryx(xL,xR,yL,yR,tr[rt].lx);
    }
    if(xR>m){
        if(~tr[rt].rx) return res+=queryx(xL,xR,yL,yR,tr[rt].rx);
    }
    return res;
}

int main()
{
    freopen("in.txt","r",stdin);
    int T;cin>>T;
    while(T--){
        scanf("%d",&n);
        REP(i,1,n) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z),p[i].id=i;
        sort(p+1,p+n+1);
        Init(1,N,1,N);
        REP(i,1,n){
            //cout<<p[i].x<<" "<<p[i].y<<" "<<p[i].z<<endl;
            p[i].ans=queryx(1,p[i].x,1,p[i].y,rt);
            updatex(p[i].x,p[i].y,1,rt);
            //cout<<p[i].ans<<endl;
        }
        for(int i=n-1;i>=1;i--) if(p[i].x==p[i+1].x&&p[i].y==p[i+1].y&&p[i].z==p[i+1].z) p[i].ans=p[i+1].ans;
        sort(p+1,p+n+1,cmp);
        REP(i,1,n) printf("%d\n",p[i].ans);
    }
    return 0;
}

hdu5618 Jam's problem again

时间: 2024-10-05 05:58:26

hdu5618 Jam's problem again的相关文章

HDU 5618:Jam&#39;s problem again(CDQ分治+树状数组处理三维偏序)

http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:-- 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #d

HDU 5618 Jam&#39;s problem again

题意: 三维坐标,对于1个点,找出有多少个点,3个坐标都比该点小! Sample Input 1 4 10 4 7 10 6 6 8 2 5 7 3 10 Sample Output 1 1 0 0 首先是方法一: 很常见的三维偏序做法,先将所有输入点按要求排序,然后向数据结构插入的时候就可以确定先插入的x值一定比后插入的x小,这样就将三维转化为2维. 2维的话就用树状数组套线段树.根据y建树状数组,根据z建线段树,每一个树状数组的点都对应着一个线段树. #include<cstdio> #i

CDQ

左边为原oj的链接,右边为VJ上的链接 入门: CodeForces-669E Little Artem and Time Machine   题解 CodeForces-97B Superset Bzoj-3110  K大数查询 Bzoj-2001 City 城市建设    题解 Bzoj-4553 序列 UVA-11990 "Dynamic'' Inversion HDU-4742 Pinball Game 3D UVA-6667 Longest Chain   题解 HDU-5618 Ja

Online Judge for ACM-ICPC etc.

原文链接:http://blog.csdn.net/tigerisland45/article/details/52134189 Virtual Judge ACM-ICPC Live Archive - Home UVa Online Judge - Home Welcome To PKU JudgeOnline(POJ) Welcome to Hangzhou Dianzi University Online Judge(HDU) OpenJudge - 百练 - 首页(PKU) Codef

HDU 5615 Jam&#39;s math problem

Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is trying to factorize ax^2+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m).He could only solve the problem in which p,q,m,k are positive numbers.Ple

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

hdu 5615 Jam&#39;s math problem(十字相乘判定)

d. Jam有道数学题想向你请教一下,他刚刚学会因式分解比如说,x^2+6x+5=(x+1)(x+5) 就好像形如 ax^2+bx+c => pqx^2+(qk+mp)x+km=(px+k)(qx+m) 但是他很蠢,他只会做p,q,m,kp,q,m,k为正整数的题目 请你帮助他,问可不可以分解 题意就是问一个一元二次方程能不能进行十字相乘的分解? s. 官方题解:第一道题比较简单,可以说是简单的模拟题,我们考虑到a,b,c都是10^9??的,所以我们决定要把时间复杂度降下来, 对于每一个数,因为

Jam&#39;s math problem(思维)

Jam's math problem Submit Status Practice HDU 5615 Description Jam has a math problem. He just learned factorization.  He is trying to factorize  into the form of .  He could only solve the problem in which p,q,m,k are positive numbers.  Please help

google code jam Round 1A 2015 Problem C. Logging

Problem A certain forest consists of N trees, each of which is inhabited by a squirrel. The boundary of the forest is the convex polygon of smallest area which contains every tree, as if a giant rubber band had been stretched around the outside of th