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 main(){
  //Treap::Node* test = Treap::nil;
  //Treap::insert(test, 1);
  //cout << Treap::getRank(test, 2) << endl;
  scanf("%d", &TestNum);
  while (TestNum--){
    scanf("%d", &n);
    int m; scanf("%d", &m);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    SegmentTree::Node root(0, n - 1); 
    tree.init(root);
    while (m--){
      char buf[32]; scanf("%s", buf);
      if (buf[0] == 'Q'){
        int i, j, k; scanf("%d %d %d", &i, &j, &k);
        printf("%d\n", getKth(i - 1, j - 1, k - 1));
      }else{
        int i, t; scanf("%d %d", &i, &t);
        tree.gao(i -1 , i - 1).update(root, a[i - 1], t);
        a[i - 1] = t;
      }
      //printf("new_cnt: %d\n", new_cnt);
    }
    tree.clear(root);
    //printf("new_cnt: %d\n", new_cnt);
    assert(new_cnt == 0);
    //len = 1;
  }
}
int main()
{
	#ifdef LOCAL
//	freopen("1008.in", "r", stdin);
//	freopen("out.txt", "w", stdout);
	#endif
	
	int T;
	scanf("%d", &T);
	for(int ck=1; ck<=T; ck++)
	{
		scanf("%d%d", &N, &M);
		for(int i=1; i<=N; i++) scanf("%d", &A[i]);
		tree.len=N;
		tree.build();
		for(int i=1,opt,l,r,x; i<=M; i++)
		{
			scanf("%d%d%d", &opt, &l, &r);
			if(opt==1) {scanf("%d", &x); tree.add(l,r,x);}
			if(opt==2) {tree.rsqrt(l,r);}
			if(opt==3) {printf("%lld\n", tree.sum(l,r));}
//			PCUT;
		}
	}
	return 0;
}
Beispiel #4
0
int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  const double pi = 2 * acos(0.0);

  while (scanf("%d%d", &N, &C) != EOF) {
    REP(i, N) scanf("%d", L+i);
    REP(i, C) scanf("%d%d", S+i, A+i);

    double prv[10000];

    SegmentTree T;
    T.init(0,0,N);
    for (int i = 1; i < N; ++i) prv[i] = pi;

    REP(i, C) {
      int s = S[i];
      double a = (A[i] / 360.0) * 2 * pi;
      T.change(s, a-prv[s], 0, 0, N);
      prv[s] = a;
      printf("%.2f %.2f\n", T.vx[0], T.vy[0]);
    }
    puts("");
  }
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;
}
Beispiel #6
0
int main(int argc, char* argv[])
{

	RandomGraph();		

	// These are 9 edges and 8 vertices

	// Mapping:
	// x = 0
	// z = 1
	// u = 2
	// w = 3
	// y = 4
	// v = 5
	// s = 6
	// t = 7	

	/*g.addEdge(0, 1);
	g.addEdge(1, 2);
	g.addEdge(2, 3);
	g.addEdge(2, 4);
	g.addEdge(1, 5);
	g.addEdge(0, 6);
	g.addEdge(6, 7);
	g.addEdge(0, 3);
	g.addEdge(1, 4);*/
	
	g.DFS();
	cout << "List Printing in DFS order" << endl;
	g.vectorListPrint();
	cout << "Edge List Printing in DFS order" << endl;
	g.edgeList();
	
	cout << "elapsed_secs starts for SegmentTree" << endl;
	clock_t begin = clock();
	SegmentTree segTree;
	clock_t end = clock();
  	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  	cout << "elapsed_secs: " << elapsed_secs << endl;


  	SegmentTreeNode *nodes = segTree.getNodes();
  	BinarySearchTree *ptrBST =  nodes[1].getBinarySearchTree();
  	BSTNode* rootNode = ptrBST -> root;
  	vector<BSTNode*> BSTNodeVector = ptrBST -> inorderforList();

   	int w = rand() % VERTEX;
  	int y;
  	while(1)
  	{
  		y = rand() % VERTEX;
  		if(y != w)
  			break;
  	}
  	pair<int,int> final_edge = Query(BSTNodeVector,w,0,y);
  	cout << "Edge: " << final_edge.first  << " " << final_edge.second << endl;
  	cout << "Total edges: " << EDGES << " and Total vertices: " << VERTEX << endl;
	return 0;
}
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;
}
void expend(int np)
{
	if(str[np] != chr[3]) return;
	int lm = startE(np);
	if(!lm) return;
	printf("ex-lm: %d %d\n", lm, np);
	tree.clear(1,lm,np);
	tree.update(1,np);
}
Beispiel #9
0
int main() {
	int x[] = {4, 2, 19, 24, 11, -1, 100, 32};
	int size = sizeof(x) / sizeof(x[0]);

	SegmentTree seg;
	seg.init(size);

	rep(i, size) {
		seg.update(i, x[i]);
	}
Beispiel #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;
 }
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;
}
Beispiel #12
0
int main()
{
  int arr[] = {1, 3, 8, 5, -1, 7, 9, 11};
  int n = sizeof(arr) / sizeof(int); 
  SegmentTree * tree = new SegmentTree(arr, n);

  // Find sum after the value is updated
  cout << "Updated sum of values in given range = "
           << tree->rmq(6, 7) << "\n";
  cout << "Updated sum of values in given range = "
           << tree->rmq(1, 6) << "\n";
  cout << "Updated sum of values in given range = "
           << tree->rmq(0, 3) << "\n";
}
void HLD::change(int pos, int val)
{
	pos=(pos-1)*2;
	int u=G->u[pos], v=G->v[pos];
	if(dept[u]>dept[v]) swap(u,v);
	tree.change(1,dfsn[v],val);
}
void HLD::nega(int x, int y)
{
	if(x==y) return;
	while(ntop[x] != ntop[y])
	{
		if(dept[ ntop[x] ] > dept[ ntop[y] ]) swap(x,y);
//		printf("%d %d %d %d %d\n", x, y, ntop[y], dfsn[ntop[y]], dfsn[y]);
		tree.nega(1, dfsn[ntop[y]], dfsn[y]);
		y = fath[ ntop[y] ];
//		printf("res: %d\n", tree.query(1, dfsn[2], dfsn[2]));
	}
	if(x==y) return;
	if( dept[x] > dept[y] ) swap(x,y);
	x=bson[x];
	tree.nega(1, dfsn[x], dfsn[y]);
}
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;
}
Beispiel #16
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;
 }
Beispiel #17
0
 void updateEdgeChain(int u, int anc, int val) {
     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];
             st.update(val, l, r);
             u = p;
         } else {
             int r = stIdx[u];
             st.update(val, r, r);
             u = g.E[fe].to;
         }
     }
 }
void check_update_correctness(int where, int what){
  vector<int> old = t.arr;
  t.update(where, what);

  old[where] = what;
  printf("Update [%d] = %d\n", where, what);
  assert(t.arr == old);
  printf("      SUCCESS\n");
}
Beispiel #19
0
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 ()
{
    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;
}
Beispiel #21
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;
 }
	void init(Graph *g)
	{
		G=g;
		dfst=0;
		CLR(dfsn); CLR(dept); CLR(size); CLR(bson); CLR(fath); CLR(ntop);
		CLR(pval); CLR(sque);
		dfs1(1);
		dfs2(1,1);
		tree.build(1,1,G->ndn,sque);
	}
Beispiel #23
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;
}
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;
}
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");
}
Beispiel #26
0
int main()
{
	node identity;
	identity.val = -1;
	SegmentTree<node> s;
	int n , k;
	scanf( "%d" , &n );
	vector<node> v( n );
	for ( int i = 0; i < n; ++i )
	{
		int x;
		scanf( "%d" , &x );
		v[ i ].val = x;
	}
	scanf( "%d" , &k );
	s.init( v , identity );
	for ( int i = 0; i <= n - k; ++i )
		printf( "%d " , s.rangeQuery( i , i + k-1 ).val );
	return 0;
}
int getKth(int from, int to, int k){
  SegmentTree::Node root(0, n - 1); 
  int lo = 0, hi = 1000*1024*1024; 
  while (lo < hi){
    int m = (lo + hi + 1) / 2;
    int less_than_m = tree.gao(from, to).getRank(root, m);
    //printf("%d: %d %d, %d\n", m, lo, hi, less_than_m); 
    if (less_than_m > k) hi = m - 1;
    else lo = m;
  }
  return lo;
};
Beispiel #28
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;
}
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;
}
// 'rin' is the input range set
//
// 'rout' is the output range set. It is guaranteed that 'rout'
// contains the mapping needed to re-contruct the least number of
// ranges needed to represent the complete range. Start with rout[0]
// and keep following the indexes till you reach some 'k' for which
// rout[k] == n.
//
// rout[i] contains the next index within rout (in sorted string
// order) where you need to jump to find the next range that completes
// the smallest number of ranges that cover the entire range.
// 
// you will need to index into 'slcp' to locate the index of this
// range in the original string
//
void
compress_ranges(vi_t &rin, vi_t &rout) {
    SegmentTree st;
    rout.clear();
    if (rin.empty()) {
        return;
    }

    const int n = rin.size();
    st.initialize(n + 1, n + 1);
    rout.resize(n);
    st.insert(n, 0);

    for (int i = rin.size() - 1; i >= 0; --i) {
        assert(rin[i] > 0);
        int l = i + 1, r = std::min(n, i+rin[i]);
        pii_t m = st.query_min(l, r);
        rout[i] = m.INDEX;
        st.insert(i, m.LENGTH + 1);
    }
}