Hdoj 4267-line segment tree

Source: Internet
Author: User

2012 Changchun division network competition... Question

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4267

01 there are two operations for Array ai with an initial value
1. Add C to the value of each AI that satisfies (I-A) % K = 0 in the range [a, B]
2. Ask about the AI value.

The idea of forming a line segment tree is obvious, because k <= 10, the value is very small, so the Maintenance Interval Values of nodes can be 1-10, a total of 55 cases
Add [10] [10] directly to sb during the competition... keep MLE all the time, and change it to dynamic allocation. After TLE, Nima knows that adding [55] is just right ....

Don't talk about anything, postCode... Depressed ..

# Include <stdio. h> # include <string. h >#include <algorithm> # include <cmath> # include <iostream> using namespace STD; const int n = 50002; int n, m; struct node {bool flag; int L, R; int val; int add [55]; void reset () {memset (ADD, 0, sizeof (ADD); flag = false ;}} A [n * 4]; int B [N], KK, CC; void build (INT L, int R, int p) {A [p]. L = L; A [p]. R = r; A [p]. reset (); If (L = r) {A [p]. val = B [l]; return;} int mid = (L + r)> 1; build (L, mi D, P * 2); Build (Mid + 1, R, p * 2 + 1);} inline int ijtonum (int I, Int J) {return (I * (I + 1)/2 + J;} inline void down (INT p) {if (a [p]. flag = true) {int I, j; for (I = 0; I <10; I ++) {int II = I + 1; for (j = 0; j <= I; j ++) {if (a [p]. add [ijtonum (I, j)]! = 0) {A [p * 2]. add [ijtonum (I, j)] + = A [p]. add [ijtonum (I, j)]; A [p * 2]. flag = true; A [p * 2 + 1]. add [ijtonum (I, (II * 50001-(A [p * 2]. r-A [p * 2]. L + 1-j) % II)] + = A [p]. add [ijtonum (I, j)]; // note the influence of the Offset generated in the left half on the second half... That is, the use of j... A [p * 2 + 1]. flag = true ;}} A [p]. reset () ;}} void Update (int l, int R, int P, int KI) {if (a [p]. L = L & A [p]. R = r) {// A [p]. add [kk-1] [Ki] + = cc; A [p]. add [ijtonum (kk-1, KI)] + = cc; A [p]. flag = true; return;} // down (p); int mid = (a [p]. L + A [p]. r)> 1; if (r <= mid) Update (L, R, p * 2, KI); else if (L> mid) Update (L, R, p * 2 + 1, KI); else {Update (L, mid, p * 2, KI); Update (Mid + 1, R, p * 2 + 1, (kk * 50001-(mid-L + 1-ki) % KK); // you can create a [p] Here. R-A [p]. L + 1 ....}} Int query (int l, int R, int p) {if (a [p]. L = L & A [p]. R = r) {if (a [p]. flag = true) {for (INT I = 0; I <10; I ++) {// A [p]. val + = A [p]. add [I] [0]; A [p]. val + = A [p]. add [ijtonum (I, 0)]; // A [p]. add [I] [0] = 0; A [p]. add [ijtonum (I, 0)] = 0;} A [p]. flag = false;} return a [p]. val;} Down (p); int mid = (a [p]. L + A [p]. r)> 1; if (r <= mid) return query (L, R, p * 2); else if (L> mid) return query (L, R, p * 2 + 1);} int main () {int op, AA, BB; Wh Ile (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) {scanf ("% d", & B [I]);} build (1, n, 1); scanf ("% d", & M); // printf ("case # % d: \ n", cas1 ++); While (M --) {scanf ("% d", & OP); If (OP = 1) {scanf ("% d", & aa, & BB, & KK, & CC); Update (AA, BB, 1, 0);} else {scanf ("% d", & aa ); printf ("% d \ n", query (AA, AA, 1) ;}} return 0 ;}

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.