本文整理汇总了C++中SegmentTree类的典型用法代码示例。如果您正苦于以下问题:C++ SegmentTree类的具体用法?C++ SegmentTree怎么用?C++ SegmentTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SegmentTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: main
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;
}
}
示例3: main
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("");
}
示例4: 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;
}
示例5: main
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;
}
示例6: 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;
}
示例7: main
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]);
}
示例8: 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;
}
示例9: 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;
}
示例10: main
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";
}
示例11: 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;
}
示例12: 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;
}
示例13: main
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;
}
示例14: expend
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);
}
示例15: change
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);
}