FZU-1921 + line segment tree

Source: Internet
Author: User

Simple line segment tree. Record MinVal and the corresponding ID.

/* Line Segment tree */# include <stdio. h> # include <string. h> # include <stdlib. h> # include <algorithm> # include <iostream> # include <queue> # include <map> # include <stack> # include <set> # include <math. h> using namespace std; typedef long int64; // typedef _ int64 int64; typedef pair <int64, int64> PII; # define MP (a, B) make_pair (a), (B) const int maxn = 10005; const int inf = 0x7fffffff; const double pi = acos (-1.0); const double eps = 1e-8; # define L (x) (x <1) # define R (x) (x <1 | 1) struct Tree {int l, r, id, val ;} tree [maxn <2]; int a [maxn]; void build (int L, int R, int n) {tree [n]. l = L; tree [n]. r = R; if (L = R) {tree [n]. id = L; tree [n]. val = a [L]; return;} // tree [n]. val = 0; int mid = (L + R)/2; build (L, mid, L (n); build (mid + 1, R, R (n); if (tree [L (n)]. val <tree [R (n)]. val) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else if (tree [L (n)]. val = tree [R (n)]. val) {if (tree [L (n)]. id <tree [R (n)]. id) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} void update1 (int val, int L, int R, int n) {if (L = R) {tree [n]. val + = val; return;} int mid = (L + R)/2; if (tree [n]. id <= mid) update1 (val, L, mid, L (n); else update1 (val, mid + 1, R, R (n )); if (tree [L (n)]. val <tree [R (n)]. val) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else if (tree [L (n)]. val = tree [R (n)]. val) {if (tree [L (n)]. id <tree [R (n)]. id) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} void update2 (int id, int val, int L, int R, int n) {if (L = R) {tree [n]. val + = val; return;} int mid = (L + R)/2; if (id <= mid) update2 (id, val, L, mid, L (n); else update2 (id, val, mid + 1, R, R (n); if (tree [L (n)]. val <tree [R (n)]. val) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else if (tree [L (n)]. val = tree [R (n)]. val) {if (tree [L (n)]. id <tree [R (n)]. id) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} void query (int L, int R, int n) {if (L = R) return; int mid = (L + R)/2; query (L, mid, L (n); query (mid + 1, R, R (n); if (tree [L (n)]. val <tree [R (n)]. val) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else if (tree [L (n)]. val = tree [R (n)]. val) {if (tree [L (n)]. id <tree [R (n)]. id) {tree [n]. val = tree [L (n)]. val; tree [n]. id = tree [L (n)]. id;} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} else {tree [n]. val = tree [R (n)]. val; tree [n]. id = tree [R (n)]. id ;}} int main () {int T; int Case = 1; scanf ("% d", & T); while (T --) {int n; scanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d ", & a [I]); build (1, n, 1); int m; scanf ("% d", & m); int x, y; while (m --) {scanf ("% d", & x, & y); if (x = 0) update1 (y, 1, n, 1); else update2 (x, y, 1, n, 1);} query (1, n, 1); printf ("Case % d: % d \ n", Case ++, tree [1]. id, tree [1]. val);} return 0 ;}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.