Bzoj 3211 Flora to the National tree Array + and check the collection

Source: Internet
Author: User

Title effect: Flora looks at every country, and sometimes he will be a continuous country visit, begging for his appetite and sometimes, he will produce an aversion to the country's continuous cycle and like to become sqrt (x) by rounding.



Thinking: At first glance, this seems to be the RMQ problem, the line segment tree will be able to make water too, mark how to pass it? This is a serious problem. So we're going to change a new idea.
Note that the open square root has an interesting property: sqrt (1) = 1,sqrt (0) = 0, and all the 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 the store becomes 1, it makes a mark, and the next time it's no point.

Maintained with a segment tree.
There are, of course, smaller methods of constants. Maintains a tree-like array for the entire sequence. Use and check to maintain a number on the right of each number that is not 1, and then violently open the root. When a number becomes 1, the father of the point is attached to the number on the right. You can skip a continuous 1 when you change the continuous interval.

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 Flora to the National tree Array + and check the 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.