int main()
{
    ios::sync_with_stdio(false);
    
    SegmentTree T;
    int n = SN;
    srand(time(0));
    int Q = 1000000;

    vector<int> v(n);

    char c;
    int a,b,x;
    // while(cin >> c >> a >> b){
    //     if(c == 'u'){
    //         cin >> x;
    //         T.update(x,a,b);
    //         FORA(i,a,b) v[i] += x;
    //     }
    //     else {
    //         int sum = 0;
    //         FORA(i,a,b) sum += v[i];

    //         cout << T.query(a,b) << ' ' << sum << endl;
    //     }
    //     FOR(i,n) cout << v[i] << ' '; cout << endl;
    // }
    // return 0;

    while(Q--){
        a = (rand() % n) + 1, b = (rand() % n) + 1;
        if(a > b) swap(a,b);

        x = rand() % (n*n);
        printf("update : [%d,%d) +%d\n", a,b,x);
        T.update(x,a,b);
        FORA(i,a,b) v[i] += x;

        a = (rand() % n) + 1, b = (rand() % n) + 1;
        if(a > b) swap(a,b);
        printf("query : [%d,%d) \n",a,b);

        int sum = 0;
        FORA(i,a,b) sum += v[i];

        FOR1(i,n) cout << v[i] << ' '; cout << endl;
        cout << T.query(a,b) << ' ' << sum << endl;

        assert(T.query(a,b) == sum);
    }
    
    return 0;
}
Exemple #2
0
int main()
{
	#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	#endif
	
	int T;
	scanf("%d", &T);
	for(int ck=1; ck<=T; ck++)
	{
		scanf("%d", &N);
		
		scanf("%*s%*s");
		scanf("%d", &Q);
		tree.build(1,1,N);
		for(int i=1; i<=Q; i++)
		{
			int opt,a,b;
			scanf("%d%d%d", &opt, &a, &b);
			if(opt)
			{
				printf("%d\n", tree.query(1,a,b));
			}
			else
			{
				tree.update(1,a,a,b);
			}
		}
		
		scanf("%*s%*s");
		scanf("%d", &Q);
		tree.build(1,1,N);
		for(int i=1; i<=Q; i++)
		{
			int opt,a,b,c;
			scanf("%d", &opt);
			if(opt)
			{
				scanf("%d", &a);
				printf("%d\n", tree.query(1,a,a));
			}
			else
			{
				scanf("%d%d%d", &a, &b, &c);
				tree.update(1,a,b,c);
			}
		}
	}
	return 0;
}
int main() {

    int b, p, l, n, n_input;

    while(scanf("%d %d %d %d", &b, &p, &l, &n) == 4) {
        if(b == 0 and p == 0 and l == 0 and n == 0) break;
        B = b;
        mod = p;
        
        n_input = n;
        
        SegmentTree tree;
        tree.initialize((int)l + 1);
        
        char cm;
        int i, v; 
        while(n_input--) {
            assert(scanf(" %c %d %d", &cm, &i, &v) == 3);
            
            //D(cm); D(i); D(v);
            // responder al comando cm, i, v 
            if (cm == 'E') {
                tree.update(i, v);
            } else if (cm == 'H'){
                int ans = tree.query(i, v) % mod;
                printf("%d\n", ans);
            } 
        }
        puts("-");
    }
    return 0;
}
int main(void) {
	T = in();

	int kind;
	SegmentTree tree;
	
	for ( ; T--; ) {
		N = in();
		C = in();

		tree.clear();
		
		for ( ; C--; ) {
			kind = in();

			if (kind == 0) {
				P = in();
				Q = in();
				V = in();

				tree.update(1, 1, N, P, Q, V);
			} else {
				P = in();
				Q = in();

				printf("%lld\n", tree.query(1, 1, N, P, Q));
			}
		}
	}

	return 0;
}
int HLD::query(int x, int y)
{
	if(x==y) return 0;
	int res=-INF;
	while(ntop[x] != ntop[y])
	{
		if(dept[ ntop[x] ] > dept[ ntop[y] ]) swap(x,y);
		res = max(res, tree.query(1, dfsn[ntop[y]], dfsn[y]));
		y = fath[ ntop[y] ];
	}
	if(x==y) return res;
	if( dept[x] > dept[y] ) swap(x,y);
	x=bson[x];
	res = max(res, tree.query(1, dfsn[x], dfsn[y]));
	return res;
}
Exemple #6
0
 int queryNode(int u, int v) {
     int ret = queryEdge(u, v);
     int p = lca(u, v);
     int r = stIdx[p];
     ret += st.query(r, r);
     return ret;
 }
int HLD::Get(int x,int y)
{
	int res=0;
	SegmentTree::node r;
	while(ntop[x] != ntop[y])
	{
		if( dept[ ntop[x] ] > dept[ ntop[y] ] ) swap(x,y);
		r = tree.query(1, dfsn[ ntop[y] ], dfsn[y]);
		res += r.sum - r.dp;
		y=fath[ ntop[y] ];
	}
	if( dept[x] > dept[y] ) swap(x,y);
	r = tree.query(1, dfsn[x], dfsn[y]);
	res += r.sum - r.dp;
	r = tree.query(1, dfsn[x], dfsn[x]);
	res += r.dp;
	return res;
}
int main() {
    int nums[] = {-2, 0, 3, -5, 2, -1};
    SegmentTree<int> *segmentTree = new SegmentTree<int>(nums, sizeof(nums) / sizeof(int), [](int a, int b) -> int {
        return a + b;
    });
    std::cout << segmentTree->query(2,5) << std::endl;
    segmentTree->print();
    return 0;
}
Exemple #9
0
 int queryEdgeChain(int anc, int u) {
     int ret = 0;
     while (u != anc) {
         int fe = rev[u];
         if (top[u] != u) {
             int p = top[u];
             if (dep[p] < dep[anc]) p = anc;
             int l = stIdx[heavyChild(p)];
             int r = stIdx[u];
             ret += st.query(l, r);
             u = p;
         } else {
             int r = stIdx[u];
             ret += st.query(r, r);
             u = g.E[fe].to;
         }
     }
     return ret;
 }
Exemple #10
0
 /**
  *@param A, queries: Given an integer array and an query list
  *@return: The result list
  */
 vector<int> intervalMinNumber(vector<int> &A, vector<Interval> &queries) {
     // write your code here
     vector<int> r;
     SegmentTree st;
     Node* root = st.build(A, 0, A.size()-1);
     for(int i=0;i<queries.size();i++) {
         Interval tmp = queries[i];
         r.push_back(st.query(root, tmp.start, tmp.end));
     }
     return r;
 }
Exemple #11
0
int main() {
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    scanf("%d %d %I64d", &rings[i].a, &rings[i].b, &rings[i].h);
  }
  sort(rings, rings + n);
  n = merge();
  tree.build(1, 0, n - 1);
  for(int i = 0; i < n; i++) {
    int update_idx = lower_bound(ranks.begin(), ranks.end(), rings[i].a) - ranks.begin();
    int idx = lower_bound(ranks.begin(), ranks.end(), rings[i].b) - ranks.begin();
    long long val = rings[i].h;
    if(idx) {
      val += tree.query(1, 0, idx - 1);
    }
    tree.update(1, update_idx, val);
  }
  printf("%I64d\n", tree.query(1, 0, n - 1));
  return 0;
}
void check_query_correctness(int u, int v){
  assert(u <= v);
  int index = u;
  for (int k=u; k<=v; ++k) if (t.arr[k] < t.arr[index]) index = k;
  int q = t.query(u, v);
  printf("Range [%d, %d]:\n", u, v);
  printf("     Tree query: index = %d, element = %d\n", q, t.arr[q]);
  printf("   Lineal query: index = %d, element = %d\n", index, t.arr[index]);
  assert(index == q);
  printf("      SUCCESS\n");
}
Exemple #13
0
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&n);
        int cnt = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&h[i]);
            rec[cnt++] = h[i];
        }
        std::sort(rec,rec+cnt);
        rcnt = 1;
        for(int i=0; i<cnt; i++)
            if(i == 0 || rec[i]!=rec[i-1])
            {
                real[rcnt++] = rec[i];
            }
        for(int i=0; i<n; i++)
            conh[i] = find(h[i]);
        SegmentTree sgt = SegmentTree();
        sgt.build(1,0,n);
        for(int i=0; i<n; i++)
        {
            int cur = sgt.query(1,conh[i]+1,n,1);
            if(cur >= 0)
                sgt.insert(1,conh[i],conh[i],0,cur+1);
//			printf("to be low %d ",cur);
            cur = sgt.query(1,0,conh[i]-1,0);
            if(cur >= 0)
                sgt.insert(1,conh[i],conh[i],1,cur+1);
            //		printf("to be high %d\n",cur);
        }
        int ans = Max(sgt.query(1,0,n,0),sgt.query(1,0,n,1));
        printf("%d\n",ans);
    }
    return 0;
}
int main()
{
	#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	#endif
	
	while(~scanf("%s", str+1))
	{
		N = strlen(str+1);
		tree.build(1,1,N);
		scanf("%d", &Q);
		for(int i=0; i<Q; i++) scanf("%d%d", &qry[i].l, &qry[i].r), qry[i].id=i;
		CLR(near[0]);
		for(int i=1; i<=N; i++)
		{
			for(int j=0; j<4; j++) near[i][j] = near[i-1][j];
			for(int j=0; j<4; j++) if(str[i] == chr[j]) {near[i][j] = i; break;}
		}
		sort(qry, qry+Q);
		int np=0;
		for(int q=0; q<Q; q++)
		{
			printf("%d %d\n", qry[q].l, qry[q].r);
			while(np<qry[q].r) expend(++np);
			int res = tree.query(1,qry[q].l,qry[q].r);
			printf("res: %d test:%d\n", res, tree.query(1,1,N));
			int temp;
			int lm = startE(temp=tree.leftmost(1,qry[q].l,qry[q].r)); // bug
			printf("lm: %d lef: %d\n", lm, temp);
			if(lm<qry[q].l && res) res--;
			ans[qry[q].id] = res;
		}
		for(int q=0; q<Q; q++) printf("%d\n", ans[q]);
	}
	return 0;
}
int main ()
{
    SegmentTree obj;
    int n;
    cout<<"Enter the number of elements"<<endl;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>obj.ary[i];
    obj.print(n);

    obj.makeTree(1,n,1);
    int a,b;
    while(cin>>a>>b)
    {
        cout<<"The lowest number between "<<a<<"to "<<b<<"is "<<obj.query(1,n,a,b,1);
    }
    return 0;
}
int main(){
	redirect();
	while(cin>>N>>K){
		for(int i=0;i<N;i++)
			cin>>v[i];
		st.buildTree();
		for(int i=0;i<K;i++){
			cin>>ch>>a>>b;
			if(ch=='C'){
				v[a-1]=b;
				st.update(1,0,N-1,a-1);
			}
			else{
				cout<<st.query(a-1,b-1);
			}
		}
		cout<<endl;
	}
	return 0;
}
int main(){
	redirect();
	while(cin>>N){
		if(!N)
			break;
		cin>>Q;
		ori.clear();
		freq.clear();
		for(int i=0;i<N;i++){
			cin>>t;
			ori.push_back(t);
		}
		translateToFreq();
		st.buildTree();
		for(int j=0;j<Q;j++){
			cin>>a>>b;
			cout<<st.query(a-1,b-1)<<endl;
		}
	}
	return 0;
}
Exemple #18
0
int main() {
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    scanf("%d %d", &v[i].r, &v[i].h);
    ranks[i] = i;
  }
  sort(ranks, ranks+n, cmp);
  for(int i = 0; i < n; i++)
    v[ranks[i]].i = i;
  SegmentTree tree;
  tree.build(1, 0, n-1);
  long long ans = 0;
  for(int i = 0; i < n ; i++) {
    int r = v[i].i;
    long long now = tree.query(1, 0, r) + v[i].r * 1LL * v[i].r * v[i].h;
    ans = max(ans, now);
    tree.update(1, r, now);
  }
  printf("%.9lf\n", pi * ans);
  return 0;
}
Exemple #19
0
void Scan(int flr)
{
	tree.build(1,0,ais[flr].siz);
	for(int i=0; i<isiz[flr]; i++)
	{
//		if(debug)
//		{
//			Object &now=inpt[flr][i];
//			puts((inpt[flr][i].flag==WALL)?"wall":"bird");
//			printf("%d: %d %d %d\n", now.id, now.u, now.v, now.h);
//		}
		if(inpt[flr][i].flag == WALL)
		{
			tree.update(1, ais[flr].id(inpt[flr][i].u), ais[flr].id(inpt[flr][i].v), inpt[flr][i].id, inpt[flr][i].h);
		}
		else
		{
//			if(debug) 
//			{
//				printf("%d\n", ais[flr].siz);
//				for(int i=0; i<ais[flr].siz; i++) printf("%d ", tree.query(1,i).first);puts("");
//				for(int i=0; i<ais[flr].siz; i++) printf("%d ", tree.query(1,i).second);puts("");
//			}
			Pii info=tree.query(1, ais[flr].id(inpt[flr][i].u));
			if(info.first==-1) continue;
//			if(debug) printf("wid=%d\n", info.first);
			int res, id;
			id=inpt[flr][i].id;
			res=abs(inpt[flr][i].h - info.second);
			if(res < bird[0][id])
			{
				bird[0][id] = res;
				bird[1][id] = info.first;
			}
		}
	}
}
 int countRangeSum(vector<int>& nums, int lower, int upper) {
     if(nums.empty()) return 0;
     int n = (int)nums.size();
     vector<long> sums;
     set<long> values;
     long sum = 0L;
     for(int i = 0; i < (int)nums.size(); ++i) {
         sum += nums[i];
         values.insert(sum);
     }
     for(auto it = values.begin(); it != values.end(); ++it) {
         sums.push_back(*it);
     }
     
     int result = 0;
     SegmentTree segmentTree;
     segmentTree.init(sums);
     for(int i = n - 1; i >= 0; --i) {
         segmentTree.update(sum);
         sum -= nums[i];
         result += segmentTree.query(lower + sum, upper + sum);
     }
     return result;
 }
 int query(int lo, int hi) { return st.query(lo, hi); } // Both inclusive