Exemple #1
0
void solve2(int l,int r)
{
	int n = cyc.size();
	
	ans -= c.ask(1,1,n,l,r);
	//cerr<<ans<<' ';
	c.update(1,1,n,l,r);
	ans += c.ask(1,1,n,l,r);
	//cerr<<ans<<'\n';
}
Exemple #2
0
int main () {
    scanf("%d", &n);
    tree.build(1, 0, n);
    while (scanf("%d %d %d", &k, &a, &b) != EOF) {
        if (k == 1) {
            scanf("%d", &v);
            tree.add(1, a-1, b, v);
        } else {
            printf("%d\n", tree.getVal(1, a-1, b));
        }
    }
}
Exemple #3
0
	data ask(int _l,int _r)
	{
		if ( _l > rt || _r < lt )
			return data();
		if ( _l <= lt && rt <= _r )
			return x;
			
		int md = (lt+rt)/2;
		
		if ( l == NULL ) l = new seg(lt,md);
		if ( r == NULL ) r = new seg(md+1,rt);
		
		return l->ask(_l,_r) + r->ask(_l,_r);
	}
Exemple #4
0
int main () {
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        tree.criaAcc(n);
        tree.build(1, 0, n);

        scanf("%d", &q);
        for (int i = 0; i < q; i++) {
            scanf("%d %d", &a, &b);
            tree.res = tree.getQuery(1, a-1, b);
            printf("%d %d\n", tree.acc[tree.res.fm] - tree.acc[tree.res.im], tree.res.fm - tree.res.im);
        }
    }
}
Exemple #5
0
int
plane::
contains ( const seg& segIn ) const
{
	vec3 pos ( math::vec3::NoInit );
	vec3 dir ( math::vec3::NoInit );
	ValueType len;
	segIn.get ( pos, dir, len );
//	dir.normalize ();
//	vec3 endPos = pos + dir * len;
	vec3 endPos ( dir );
	endPos *= len;
	endPos += pos;

	int c1 = contains ( pos );
	int c2 = contains ( endPos );

	if ( c1 )
	{
		if ( c2 )
			return containsResult::AllIn | containsResult::SomeIn;

		return containsResult::SomeIn;
	}

	if ( c2 )
	{
		return containsResult::SomeIn;
	}

	return containsResult::NoneIn;
}
Exemple #6
0
	void update(int p,data v)
	{	
		if ( lt == rt )
		{
			x = v;
			return;
		}
	
		int md = (lt+rt)/2;
		if ( l == NULL ) l = new seg(lt,md);
		if ( r == NULL ) r = new seg(md+1,rt);
		
		if ( p <= md )
			l->update(p,v);
		else
			r->update(p,v);
		x = l->x + r->x;
	}
Exemple #7
0
int main () {
    scanf("%d", &tc);

    for (int t = 1; t <= tc; t++) {
        scanf("%d %d", &n, &q);

        printf("Scenario #%d:\n", t);

        for (int i = 0; i < n; i++)
            scanf("%d", inp+i);
        tree.build(1, 0, n);

        int lo, hi;
        for (int i = 0; i < q; i++) {
            scanf("%d %d", &lo, &hi);
            printf("%d\n", tree.query(lo-1, hi).v);
        }
    }
}
Exemple #8
0
int main(int argc, char *argv[])
{
    scanf("%d%d",&k,&s);
    for(int i = 1;i<=k;i++){
     scanf("%d%d",&p[i+1].l,&p[i+1].r);
     p[i+1].l += d;
     p[i+1].r += d;
    }
    for(int i = 1;i<=k;i++)
    {
     int l = o.f(p[i+1].l);
     int r = o.f(p[i+1].r);
     if(l == 0)
      f[i+1][1] = p[i+1].l*2;
     else
      f[i+1][1] = min(f[l][1]+abs(p[l].l-p[i+1].l),f[l][2]+abs(p[l].r-p[i+1].l));
     o.b(p[i+1].l,p[i+1].r,i);
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Exemple #9
0
	void print(int all)
	{
		if ( all )
		{
			cerr<<lt<<' '<<rt<<' ';
			x.print('\n');
		}
		
		if ( lt == rt ) 
		{
			if ( !all ) x.print(' ');
			return;
		}
	
		int md = (lt + rt)/2;
		if ( l == NULL ) l = new seg(lt,md);
		if ( r == NULL ) r = new seg(md+1,rt);
		
		l->print(all);
		r->print(all);
	}
Exemple #10
0
int
seg::
contains ( const seg& segIn ) const
{
	// TODO: Remove direction vector normalization??  User's responsibility???
	vec3 tDir ( segIn.dir );
	tDir.normalize ();
//	vec3 endPt = segIn.pos + tDir * segIn.length;
	vec3 endPt ( tDir );
	endPt *= segIn.length;
	endPt += segIn.pos;

	int c1 = contains ( segIn.pos );
	int c2 = contains ( endPt );

	if ( c1 )
	{
		if ( c2 )
			return containsResult::AllIn;

		return containsResult::SomeIn;
	}

	if ( c2 )
		return containsResult::SomeIn;


	// OPTIMIZE: Expensive...
	if ( segIn.contains ( pos ) )
		return containsResult::SomeIn;

	vec3 endPos = pos + dir * length;

	if ( segIn.contains ( endPos ) )
		return containsResult::SomeIn;

	return containsResult::NoneIn;
}
Exemple #11
0
void
plane::
extendBy ( const seg& segIn )
{
	vec3 pos ( math::vec3::NoInit );
	vec3 dir ( math::vec3::NoInit );
	ValueType len;
	segIn.get ( pos, dir, len );
//	dir.normalize ();
//	dir = pos + dir * len;
	dir *= len;
	dir += pos;
	extendBy ( pos );
	extendBy ( dir );
}
Exemple #12
0
int main () {
    scanf("%d %d", &n, &m);
    tree.build(1, 0, n);
    for (int i = 0; i < m; i++) {
        scanf("%d %d %d", &a, &b, &x);
        if (a == x) {
            tree.update(1, a, b, x);
        } else if (b == x) {
            tree.update(1, a-1, b-1, x);
        } else {
            tree.update(1, a-1, x-1, x);
            tree.update(1, x, b, x);
        }
    }
    tree.printit(1, 0);
    printf("\n");
}
Exemple #13
0
void
seg::
xform4 ( const seg& seg, const matrix4& mat )
{
	PNIMATHUNTESTED;

	// get endpoint of seg
	vec3 end ( math::vec3::NoInit );
	seg.lerp ( end, TraitType::oneVal );
	
	// xform seg's beginning and ending
	end.xformPt4 ( end, mat );
	pos.xformPt4 ( seg.pos, mat );
	
	// finish making this seg
	// the following code is equivalent to: set ( pos, end );
	// but it doesn't redundantly set pos again
	dir = end;
	dir -= pos;
	
	length = dir.length ();

	dir /= length;	
}
	bool operator < (const seg &a) const {
		ld x = max(min(p.x, q.x), min(a.p.x, a.q.x));
		return get_y(x) < a.get_y(x) - eps;
	}
Exemple #15
0
// OPTIMIZE: This is a HUGE/heavyweight function.  Probably several ways to optimize it.
int
sphere::
contains ( const seg& segIn ) const
{
	vec3 pos;
	vec3 dir;
	ValueType len;
	segIn.get ( pos, dir, len );
	dir.normalize ();
	vec3 endPos = pos + dir * len;

	// The segment begin and endpoint might lie in the sphere.
	int c1 = contains ( pos );
	int c2 = contains ( endPos );

	if ( c1 )
	{
		if ( c2 )
			return containsResult::AllIn | containsResult::SomeIn;

		return containsResult::SomeIn;
	}

	if ( c2 )
	{
		return containsResult::SomeIn;
	}



	ValueType a = dir.dot ( dir );
	//ValueType a = dir.vec[0] * dir.vec[0] + dir.vec[1] * dir.vec[1] + dir.vec[2] * dir.vec[2];
	ValueType b = static_cast< ValueType > ( 2.0 ) * dir.vec[0] * ( pos.vec[0] - center.vec[0] ) +
	              static_cast< ValueType > ( 2.0 ) * dir.vec[1] * ( pos.vec[1] - center.vec[1] ) +
	              static_cast< ValueType > ( 2.0 ) * dir.vec[2] * ( pos.vec[2] - center.vec[2] );
	ValueType tx = pos.vec[0] - center.vec[0];
	ValueType ty = pos.vec[1] - center.vec[1];
	ValueType tz = pos.vec[2] - center.vec[2];
	ValueType c = tx * tx + ty * ty + tz * tz - radius * radius;
	//ValueType c = ( pos.vec[0] - center.vec[0] ) * ( pos.vec[0] - center.vec[0] ) +
	//			  ( pos.vec[1] - center.vec[1] ) * ( pos.vec[0] - center.vec[0] ) +
	//			  ( pos.vec[0] - center.vec[0] ) * ( pos.vec[0] - center.vec[0] ) -
	//			  radius * radius;

	// Check the discriminant to see if there are any solutions.
	ValueType discriminant = b * b - static_cast< ValueType > ( 4.0 ) * a * c;
	if ( discriminant < TraitType::zeroVal )
	{
		// No solutions --> no intersections.
		return containsResult::NoneIn;
	}

	ValueType twoA = static_cast< ValueType > ( 2.0 ) * a;

	if ( discriminant == TraitType::zeroVal )
	{
		// One intersection.  Segment is tangential.
		ValueType t1 = -b / twoA;
		vec3 ip1 = pos + dir * t1;

		return segIn.contains ( ip1 );
	}

	// Two intersections.
	ValueType t1 = ( -b + TraitType::sqrt ( discriminant ) ) / twoA;
	ValueType t2 = ( -b - TraitType::sqrt ( discriminant ) ) / twoA;
	vec3 ip1 = pos + dir * t1;
	vec3 ip2 = pos + dir * t2;

	if ( segIn.contains ( ip1 ) || segIn.contains ( ip2 ) )
		return containsResult::SomeIn;
	else
		return containsResult::NoneIn;
}
Exemple #16
0
int main()
{
	ios::sync_with_stdio(0);
	#ifndef ONLINE_JUDGE
		ifstream F("p.in");
	#endif
	
	F>>n>>m;
	for (int i=1,x,y;i<=n;++i)
	{
		F>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	
	try 
	{
		find_cycle(1);
	}
	catch (pair<int,int> p) 
	{
		int x = p.first;
		int y = p.second;
		cyc.push_back(y);
		while ( x != y )
		{
			cyc.push_back(x);
			x = dd[x];
		}
	}
	for (int i=0;i<int(cyc.size());++i)
		ord[cyc[i]] = i+1;
	//for (int i=0;i<int(cyc.size());++i) cerr<<cyc[i]<<' '; cerr<<'\n';
	
	memset(mk,0,sizeof(mk));
	memset(dd,0,sizeof(dd));
	for (int i=0;i<int(cyc.size());++i) 
		mk[cyc[i]] = 1;
	for (int i=0;i<int(cyc.size());++i) 
	{
		act_tree = i;
		find_chains(cyc[i]);
	}
	for (int i=0;i<int(chains.size());++i)
		for (int j=0;j<int(chains[i].size());++j)
		{
			int x = chains[i][j];
			my[x] = i;
			pl[x] = j+1;
		}
	//for (int i=0;i<int(chains.size());++i,cerr<<'\n') { cerr<<i<<':'; for (int j=0;j<int(chains[i].size());++j) cerr<<chains[i][j]<<' '; }

	for (int i=0;i<int(chains.size());++i)
	{
		s[i] = seg(chains[i].size());
		s[i].build(1,1,chains[i].size());
	}
	c = seg(cyc.size());
	c.build(1,1,cyc.size());

	ans = n;
	//for (int i=1;i<=n;++i) cerr<<dd[i]<<' '; cerr<<'\n';
	//for (int i=1;i<=n;++i) cerr<<my[i]<<' '; cerr<<'\n';
	//for (int i=1;i<=n;++i) cerr<<pl[i]<<' '; cerr<<'\n';
	for (int q=1,x,y;q<=m;++q)
	{
		F>>x>>y;
		//cerr<<x<<' '<<y<<'\n';
		
		vector<int> ax,ay; 
		for (int i=x;;i=dd[i])
		{
			i = chains[my[i]].back();
			ax.push_back(my[i]);
			if ( !dd[i] ) break;
		}
		for (int i=y;;i=dd[i])
		{
			i = chains[my[i]].back();
			//cerr<<i<<'\n';
			ay.push_back(my[i]);
			if ( !dd[i] ) break;
		}
		
		reverse(ax.begin(),ax.end());
		reverse(ay.begin(),ay.end());
		
		ax.resize(ax.size()+1);
		ax[ax.size()-1] = -1;
		ay.resize(ay.size()+1);
		ay[ay.size()-1] = -2;
		
		int mx = max(ax.size(),ay.size());
		ax.resize(mx);
		ay.resize(mx);
		
		//for (int i=0;i<int(ax.size());++i) cerr<<ax[i]<<' '; cerr<<'\n';
		//for (int i=0;i<int(ay.size());++i) cerr<<ay[i]<<' '; cerr<<'\n';
		
		if ( tree[x] == tree[y] )
		{
			//cerr<<x<<' '<<y<<'\n';
			int idx = 0;
			while ( ax[idx+1] == ay[idx+1] ) 
				++idx;
			int chain = ax[idx]; 
			
			//cerr<<chain<<'\n';
			x = go(x,chain);
			y = go(y,chain);
			
			int p1 = pl[x];
			int p2 = pl[y];
			if ( p1 > p2 ) swap(p1,p2);
			if ( p1 != p2 ) solve(my[x],p1,p2-1);
		}
		else
		{
			x = go(x,ax[0]);
			y = go(y,ay[0]);
			
			int nx = chains[my[x]].back();
			int ny = chains[my[y]].back();
			
			if ( x != nx ) solve(my[x],pl[x],pl[nx]-1);
			if ( y != ny ) solve(my[y],pl[y],pl[ny]-1);
			
			x = nx;
			y = ny;
			
			// on cycle edges are defined like : x -> x+1
			
			int flag = 0;
			if ( ord[x] > ord[y] ) 
			{
				swap(x,y);
				flag = 1;
			}
			
			//cerr<<ord[x]<<' '<<ord[y]<<'\n';
			
			if ( cmp(x,y,flag) )
			{
				solve2(ord[x],ord[y]-1);
			}   
			else
			{
				//cerr<<"here\n";
				if ( ord[x] > 1 ) solve2(1,ord[x]-1);
				solve2(ord[y],cyc.size());
			}
		}
		
		G<<ans+(c.a[1]==0)<<'\n';
		//for (int i=1;i<=n;++i) cerr<<s[my[i]].ask(1,1,chains[my[i]].size(),pl[i],pl[i])<<' ';cerr<<'\n';
		//for (int i=0;i<int(chains.size());++i,cerr<<'\n') 
		//{ 
		//	cerr<<i<<':'; 
		//	for (int j=0;j<int(chains[i].size());++j) 
		//		cerr<<'('<<chains[i][j]<<','<<s[i].ask(1,1,chains[i].size(),j+1,j+1)<<") "; 
		//}
	}
}
/** Get the ordering of the segments after the cur_x value
  * choose the extra x needed for arrangement accordingly.
  */
bool operator< (const seg & a, const seg & b) {
    return a.get_y(cur_x+1e-8) < b.get_y(cur_x+1e-8);
}