当前位置: 首页>>代码示例>>C++>>正文


C++ SegmentTree::query方法代码示例

本文整理汇总了C++中SegmentTree::query方法的典型用法代码示例。如果您正苦于以下问题:C++ SegmentTree::query方法的具体用法?C++ SegmentTree::query怎么用?C++ SegmentTree::query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SegmentTree的用法示例。


在下文中一共展示了SegmentTree::query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

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;
}
开发者ID:bloops,项目名称:Algorithms-and-Data-Structures,代码行数:53,代码来源:segmenttree.cpp

示例2: main

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;
}
开发者ID:XingMo,项目名称:ACM_Code,代码行数:51,代码来源:发糖游戏1+1.cpp

示例3: main

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;
}
开发者ID:Ochobytes,项目名称:competitive_programming,代码行数:32,代码来源:j.6.cpp

示例4: main

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;
}
开发者ID:aajjbb,项目名称:contest-files,代码行数:32,代码来源:HorribleQueries.cpp

示例5: query

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;
}
开发者ID:XingMo,项目名称:ACM_Code,代码行数:16,代码来源:这是一颗普通的树.cpp

示例6: queryNode

 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;
 }
开发者ID:atubo,项目名称:codeforces,代码行数:7,代码来源:P3258.cpp

示例7: Get

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;
}
开发者ID:XingMo,项目名称:ACM_Code,代码行数:18,代码来源:Tree_chain_problem.cpp

示例8: main

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;
}
开发者ID:LarryKameZhao,项目名称:Play-with-Data-Structures,代码行数:9,代码来源:main.cpp

示例9: queryEdgeChain

 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;
 }
开发者ID:atubo,项目名称:codeforces,代码行数:19,代码来源:P3258.cpp

示例10: intervalMinNumber

 /**
  *@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;
 }
开发者ID:hzy001,项目名称:lintcode-oj,代码行数:15,代码来源:main.cpp

示例11: main

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;
}
开发者ID:Ra1nWarden,项目名称:Online-Judges,代码行数:20,代码来源:e.cpp

示例12: check_query_correctness

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");
}
开发者ID:AssaultKoder95,项目名称:codejam,代码行数:11,代码来源:segment_tree_tester.cpp

示例13: main

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;
}
开发者ID:hphp,项目名称:Algorithm,代码行数:40,代码来源:H.cpp

示例14: main

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;
}
开发者ID:XingMo,项目名称:ACM_Code,代码行数:37,代码来源:As_Easy_As_Possible.cpp

示例15: main

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;
}
开发者ID:Rashik004,项目名称:Coded-algorithms,代码行数:18,代码来源:segmentTree.cpp


注:本文中的SegmentTree::query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。