Beispiel #1
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));
        }
    }
}
Beispiel #2
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);
        }
    }
}
Beispiel #3
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");
}
Beispiel #4
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);
        }
    }
}
Beispiel #5
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)<<") "; 
		//}
	}
}