Bzoj 3211 Flower Gods travel all over the world tree-like array + and search collection

Source: Internet
Author: User
Tags square root

The main idea: the flower God has a degree of affection to each country, sometimes he will visit a succession of countries, ask him to love the degree of and, sometimes he will have a succession of a state of disgust, the degree of affection becomes sqrt (x) to take the whole.

Thought: At first glance seems to be a rmq problem, with the line tree can be water over, but the open root of the mark how to pass? This is a serious problem, so we have to change one way of thinking.
Note that the open square root has an interesting property: sqrt (1) = 1,sqrt (0) = 0, and all numbers are converted to 1 by a finite number of open-root operations. This is a very good nature. We open the square root of every point of violence, and then when the point of this store becomes 1, it's a marker, and the next time it doesn't matter. Maintained with a segment tree.
There are, of course, smaller methods of constants. Maintaining the tree array for the entire sequence, using and checking the set to maintain the first digit not 1 on the right of each number, and then violently open the root of the root, when a number becomes 1, the point in the and the father of the concentration to the right of the number of the father. When you modify a continuous interval, you can skip the continuous 1.

CODE:

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define MAX 200010using namespace Std;int cnt,asks;int src[max];long long fenwick[max];int Father[max];  void pretreatment (), inline void fix (int x,int c), inline void fix (int x), inline long long getsum (int x); int Find (int x); int Main () {cin >> cnt;for (int i = 1;i <= cnt; ++i) {scanf ("%d", &src[i]); Fix (I,src[i]); if (Src[i] <= 1) father[i] = i + 1;} CIN >> asks;for (int flag,x,y,i = 1;i <= asks; ++i) {scanf ("%d%d%d", &flag,&x,&y), if (flag = = 1) printf (  "%lld\n", Getsum (Y)-getsum (x-1)), elsefor (x = Find (x), x <= y;x = find (x + 1)) {Fix (x,-src[x]); src[x] = sqrt (src[x]) + 1e-7; Fix (x,src[x]); if (src[x] = = 1) father[x] = Find (x + 1);}} return 0;} inline void Fix (int x,int c) {for (int i = x;i <= cnt;i + = i&-i) Fenwick[i] + = C;} Inline long long getsum (int x) {Long Long re = 0;for (int i = X;i;i = i&-i) Re + = Fenwick[i];return Re;} int Find (int x) {if (!father[x] | | father[x] = = x) return father[x] = X;return Father[x] = Find (Father[x]);} 


Bzoj 3211 Flower Gods travel all over the world tree-like array + and search collection

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.