Exemplo n.º 1
0
int main(){
    int N,x;
    scanf("%d",&N);
    
    SegTree* ST = new SegTree(N);

    for(int i=1;i<=N;i++){
        scanf("%d",&x);
        ST->A[i] = x;
    }

    ST->initialize(1, 1, N);
    int Q;
    scanf("%d",&Q);
    int type, y;

    while(Q--) {
        scanf("%d %d %d", &type, &x, &y);
        if(type==0)
            ST->update(1, 1, N, x, y);
        else {
            Node* ans = ST->query(1, 1, N, x, y);
            printf("%d\n",ans->maxs);
        }
    }
}
Exemplo n.º 2
0
int main(){
    int N,x;
    
    while( (cin >> N) && N){
        
        SegTree* ST = new SegTree(N);

        for(int i=1;i<=N;i++){
            scanf("%d",&x);
            ST->A[i] = x;
        }

        ST->initialize(1, 1, N);
        cout << getArea(ST, 1, N) << endl;
    }
}
int bfs(int x1, int y1, int x2, int y2) {
    if (x1 == x2 && y1 == y2) return 0;

    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++) {
            dist[i][j] = INF;
            P[i][j] = 1;
        }

    priority_queue < pair < int, pii > > fila;
    dist[x1][y1] = V[x1][y1];
    fila.push(make_pair(-dist[x1][y1], make_pair(x1, y1)));

    visited.init();
    visited.build_x(1, 1, r);
    visited.update_x(1, 1, r, x1, y1, 0);

    int a1, b1, a2, b2;
    Node aux;
    while (!fila.empty()) {
        pii u = fila.top().second;
        fila.pop();
        if (abs(u.first - x2) <= R[u.first][u.second] && abs(u.second - y2) <= C[u.first][u.second]) return dist[u.first][u.second];

        a1 = max(1, u.first - R[u.first][u.second]);
        b1 = max(1, u.second - C[u.first][u.second]);
        a2 = min(r, u.first + R[u.first][u.second]);
        b2 = min(c, u.second + C[u.first][u.second]);

        while (1) {
            aux = visited.query_x(1, 1, r, a1, b1, a2, b2);
            if (aux.val < 1) break;

            dist[aux.x][aux.y] = dist[u.first][u.second] + V[aux.x][aux.y];
            fila.push(make_pair(-dist[aux.x][aux.y], make_pair(aux.x, aux.y)));
            visited.update_x(1, 1, r, aux.x, aux.y, 0);
        }
    }
    return -1;
}
Exemplo n.º 4
0
void Solve::work_segTree(FILE *fin, FILE *fout)
{
	SegTree tree;
	for(int i = 1; i <= n; i ++)
	{
		int t = tree.ask(i);
		f[i] = MIN(INFINITY, f[t] + w(t, i));
		int l = i + 1, r = n, mid = (l + r) / 2;
		while(l < r)
		{
			Val_t tmp = MIN(INFINITY, f[i] + w(i, mid));
			int s = tree.ask(mid);
			if(s == 0 || f[s] + w(s, mid) > tmp)
				r = mid;
			else
				l = mid + 1;
			mid = (l + r) / 2;
		}
		tree.change(mid, n, i);
	}
	fprintf(fout, VAL_T_FMT "\n", f[n]);
}
Exemplo n.º 5
0
bool isYes(int s,int e){
  s--;
  e--;
  if((s+e+1)%2) return false;
  int l = segl.node[s+segl.n-1];
  int r = segr.node[e+segr.n-1];
  int o,c,q;
  if(s!=0){
    o = no[e]-no[s-1];
    c = nc[e]-nc[s-1];
    q = nq[e]-nq[s-1];
  }else{
    o = no[e]-no[s];
    c = nc[e]-nc[s];
    q = nq[e]-nq[s];
  }
  if(abs(o-c) > q) return false;
  cout << l << " " << segl.query(s,s+(e-s)/2+1) << endl;
  if(segl.query(s,s+(e-s)/2+1) < l) return false;
  cout << r << " " << segr.query(e-(e-s)/2-1,e) << endl;
  if(segr.query(e-(e-s)/2-1,e) < r) return false;
  return true;
}
Exemplo n.º 6
0
int solve(SegTree &t, int i, int j)
{
    if (i > j) return 0;
    if (i == j) return t.A[i];

    int midx = t.query_idx(i, j);
    int a = (j - i + 1) * t.A[midx];

    int lt = solve(t, i, midx - 1);
    int rt = solve(t, midx + 1, j);

    if (lt > a) a = lt;
    if (rt > a) a = rt;
    return a;
}
Exemplo n.º 7
0
int main()
{
    Reader rr;

    N = rr.next_u32();
    tree.init(ARRAYSZ);

    char op;
    int i, j, x;
    while (N--) {
        op = rr.next_char();
        i = rr.next_u32();
        j = rr.next_u32();

        switch (op) {
        case 'A':
            tree.inc_left(i, j);
            break;

        case 'B':
            tree.inc_right(i, j);
            break;

        case 'C':
            x = rr.next_int();
            tree.set(i, j, x);
            break;

        case 'S':
            printf("%lld\n", tree.query(i, j));
            break;
        }
    }

    return 0;
}
Exemplo n.º 8
0
int main()
{


  int p,c,i,j,k,q,l,t,tt=1,sz;
  string str;
  char ch;
  SegTree tree;
  cin>>t;
  while(t--)
  {
  sz=0;
  cin>>p;
  
  while(p--)
  {
  	cin>>c>>str;
  	l=str.length();
    for(i=0;i<c;i++)
  	for(j=0;j<l;j++)
  	tree.A[sz++]=str[j]-'0';
  }
  tree.n=sz;
  tree.init();
  cin>>q;
  int qnum=0;
  printf("Case %d:\n",tt++);
  while(q--)
  {
   cin>>ch>>i>>j;
   if(ch=='F')tree.set(i,j);
   else if(ch=='E')tree.clear(i,j);
   else if(ch=='I')tree.flip(i,j);
   else printf("Q%d: %d\n",++qnum,tree.query(i,j));
  }
  
  tree.reset();
  }
return  0;	
}
Exemplo n.º 9
0
void initSeg(int n){
  int l=0;
  int r=0;
  segr.n = n;
  segl.n = n;
  for(int i=0;i<SIZE*2-1;i++){
    segl.node[i]=20000;
    segr.node[i]=20000;
  }
  for(int i=0;i<n;i++){
    if(S[i] == '('){
      l++;
      r++;
      segl.update(i,l);
      segr.update(i,r);
      no[i]++;
    }
    if(S[i] == ')'){
      l--;
      r--;
      segl.update(i,l);
      segr.update(i,r);
      nc[i]++;
    }
    if(S[i] == '?'){
      l++;
      r--;
      segl.update(i,l);
      segr.update(i,r);
      nq[i]++;
    }
    no[i+1]=no[i];
    nc[i+1]=nc[i];
    nq[i+1]=nq[i];
  }
}
Exemplo n.º 10
0
int main()
{
	for(int i = 0; i < MAX; i++)
	{
		used[i] = false;
		tree.is_root[i] = true;
		cycle[i] = -1;
	}
	cin >> M;
	for(int i = 1; i <= M; i++)
	{
		int a, b;
		cin >> a >> b;
		used[a] = used[b] = true;
		if(maxtree.Find(a) == maxtree.Find(b))
		{
			for(int v = a; v != b; v = tree.G2[v]) cycle[v] = cycle_next;
			cycle[b] = cycle_next;
			cycle_first[cycle_next] = b;
			cycle_cut[cycle_next] = i;
			cycle_next++;
		}
		else
		{
			tree.is_root[b] = false;
			tree.add_edge(a, b);
			maxtree.add_edge(a, b, i);
		}
	}
	tree.get_dfs_times();
	cin >> Q;
	for(int q = 0; q < Q; q++)
	{
		int first = INF;

		int nt, nf;
		cin >> nt;
		for(int i = 0; i < nt; i++)
		{
			int v;
			cin >> v;
			if(!used[v]) continue;
			True.push_back(v);
			//cout << cycle[v] << ' ' << maxtree.Find(v)<<endl;
			if(cycle[v] != -1) cycle_components[maxtree.Find(v)] = cycle[v];
		}
		cin >> nf;
		for(int i = 0; i < nf; i++)
		{
			int v;
			cin >> v;
			if(!used[v]) continue;
			False.push_back(v);
		}

		sort(True.begin(), True.end(), compare_by_dfs_in);

		segtree.insert(-1, 0, MAX); // reset segtree
		for(int i = 0; i < True.size(); i++)
			segtree.insert(True[i], tree.dfs_times[True[i]][0], tree.dfs_times[True[i]][1]);
		for(int i = 0; i < False.size(); i++)
		{
			int v = False[i];
			int closest_true = segtree.lookup(tree.dfs_times[v][0]);
			//cout << "closest_true="<<closest_true<<endl;
			if(closest_true == -1)
			{
				int Rv = maxtree.Find(v);
				//cout<<maxtree.R[v] << ' '<<Rv << endl;;
				//cout << cycle_cut[cycle_components[Rv]] << ' ';
				if(cycle_components.find(Rv) != cycle_components.end())
					first = min(first, max(cycle_cut[cycle_components[Rv]], maxtree.max_edge(cycle_first[cycle_components[Rv]], v)));
			}
			//cout << "closest of " << False[i] << " is " << closest_true << endl;
			//if(closest_true != -1) cout << "mindist is " << maxtree.max_edge(False[i], closest_true) << endl;
			if(closest_true != -1) first = min(first, maxtree.max_edge(closest_true, v));
		}

		True.clear();
		False.clear();
		cycle_components.clear();

		cout << (first == INF ? -1 : first) << '\n';
	}
	return 0;
}
Exemplo n.º 11
0
 int Query(int l, int r)
 {
     if (r < ll || l > rr) return 0;
     else if (l <= ll && rr <= r) return val;
     else return lft->Query(l, r) + rgt->Query(l, r);
 }