HDU 4107 gangster Segment tree line

Source: Internet
Author: User

This problem is also a bit new, is the need to record the minimum and maximum segments, and then update the paragraph into a paragraph, without the need to update, to speed up the purpose of lifting.

The subject is very few people, because most of them have timed out, I strictly follow the method of line segment tree to write. It also timed out at the beginning.

Then repair two places on the past, detailed changes in the place please see the program.

Know the maximum and minimum segments, and then fix it to get past it. Not a particularly difficult topic.

#include <stdio.h> #include <string> #include <algorithm>using namespace std;const int SIZE = 200002; const int TREESIZE = SIZE + (size<<1); int N, M, P;int segtree[treesize];int smallest[treesize];int largest[treesize] ; inline int lchild (int rt) {return rt<<1;} inline int rchild (int rt) {return rt<<1|1;} void build (int l, int r, int rt) {Segtree[rt] = 0;smallest[rt] = 0;largest[rt] = 0;if (L = = r) return; int m = L + ((r-l) &G T;&GT;1); Build (L, M, Lchild (RT)); Build (M+1, R, Rchild (RT));} inline void pushup (int rt) {Smallest[rt] = min (Smallest[lchild (RT)], Smallest[rchild (RT)]); Largest[rt] = max (largest[ Lchild (RT)], Largest[rchild (RT)]);} inline void pushdown (int rt) {if (Segtree[rt]) {int L = lchild (RT), r = rchild (RT), Largest[l] + = segtree[rt];largest[r] + = SE Gtree[rt];smallest[l] + = Segtree[rt];smallest[r] + segtree[rt];segtree[l] + + segtree[rt];segtree[r] + = SegTree[rt]; SEGTREE[RT] = 0;}} void Update (int l, int r, int val, int l, int r, int rt) {if (L <= l&& R <= R) {if (Largest[rt] < P) {Smallest[rt] + Val;largest[rt] + val;segtree[rt] + = Val;return;} if (Smallest[rt] >= P) {Smallest[rt] + Val<<1;largest[rt] + val<<1;segtree[rt] + = Val<<1;return;}} if (R < L | | | R < L) return;//is inferred here will also time out pushdown (RT), int m = L + ((r-l) >>1), if (L <= m) update (L, R, Val, L, M, Lchild (RT)); F (M < R) Update (L, R, Val, M+1, R, Rchild (RT));p Ushup (RT);} inline void pushdown_2 (int rt) {if (Segtree[rt]) {segtree[lchild (RT)] + = Segtree[rt];segtree[rchild (RT)] + = Segtree[rt];}} void Printtree (int l, int r, int rt) {if (L = = r) {if (l! = N) printf ("%d", Segtree[rt]), Else printf ("%d\n", Segtree[rt]); r Eturn;//Remember to return. }pushdown_2 (RT); int m = L + ((r-l) >>1);p Rinttree (L, M, Lchild (RT));p Rinttree (m+1, R, Rchild (RT));} int main () {int A, B, C;while (scanf ("%d%d", &n, &m, &p)! = EOF) {build (1, N, 1);//memset (largest, 0, sizeof (largest)); This writes instead timeout//memset (smallest, 0, sizeof (smallest));//memset (segtree, 0, sizeOf (Segtree)), while (m--) {scanf ("%d%d%d", &a, &b, &c); Update (A, B, C, 1, N, 1);} Printtree (1, N, 1);} return 0;}


HDU 4107 gangster Segment tree line

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.