CF #52 C Circular RMQ (segment tree Interval Update ),

Source: Internet
Author: User

CF #52 C Circular RMQ (segment tree Interval Update ),

Description

You are given circular arrayA0, average,A1, middle..., middle ,...,ANLimits-limits 1. There are two types of operations with it:

  • Inc(Lf, Bytes,Rg, Bytes,V)-This operation increases each element on the segment [Lf, Bytes,Rg] (Inclusively)V;
  • Rmq(Lf, Bytes,Rg)-This operation returns minimal value on the segment [Lf, Bytes,Rg] (Inclusively ).

Assume segments to be circular, so ifNBetween = between 5 andLfLatency = latency 3, latency,RgSequence = sequence 1, it means the index sequence: 3, sequence 4, sequence 0, sequence 1.

Write program to process given sequence of operations.

Input

The first line contains integerN(1 digit ≤ DigitNLimit ≤ limit 200000). The next line contains initial state of the array:A0, average,A1, middle..., middle ,...,ANExtract-extract 1 (extract-extract 10)6 bytes ≤ bytesAILimit ≤ limit 106 ),AIAre integer. The third line contains integerM(0 bytes ≤ bytesMLimit ≤ limit 200000 ),M-The number of operartons. NextMLines contain one operation each. If line contains two integerLf, Bytes,Rg(0 bytes ≤ bytesLf, Bytes,RgLimit ≤ limitNCommit-commit 1) it meansRmqOperation, it contains three integersLf, Bytes,Rg, Bytes,V(0 bytes ≤ bytesLf, Bytes,RgLimit ≤ limitNExecutor-cores 1; executor-cores 106 bytes ≤ bytesVLimit ≤ limit 106 )-IncOperation.

Output

For eachRmqOperation write result for it. please, do not use % lld specificator to read or write 64-bit integers in C ++. it is preffered to usecout (also you may use % I64d ).

Sample Input

Input
41 2 3 443 03 0 -10 12 1
Output
100

The question is easy to understand.

If a> B, query 0 ~ B and ~ N-1. The rest is the line segment tree Interval Update template, which determines whether c exists in m queries. For details, refer to the Code for better understanding.


#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#include <ctype.h>#include <iostream>#define lson o << 1, l, m#define rson o << 1|1, m+1, rusing namespace std;typedef __int64 LL;const __int64 MAX =  9223372036854775807;const int maxn = 200010;int n, a, q, c, b;char str[1200];LL mi[maxn<<2], add[maxn<<2];void up(int o) {    mi[o] = min(mi[o<<1], mi[o<<1|1]);}void down(int o) {    if(add[o]) {        add[o<<1] += add[o];        add[o<<1|1] += add[o];        mi[o<<1] += add[o];        mi[o<<1|1] += add[o];        add[o] = 0;    }}void build(int o, int l, int r) {    if(l == r) {        scanf("%I64d", &mi[o]);        return;    }    int m = (l+r) >> 1;    build(lson);    build(rson);    up(o);}void update(int o, int l, int r) {    if(a <= l && r <= b) {        add[o] += c;        mi[o] += c;        return ;    }    down(o);    int m = (l+r) >> 1;    if(a <= m) update(lson);    if(m < b ) update(rson);    up(o);}LL query(int o, int l, int r) {    if(a <= l && r <= b) return mi[o];    down(o);    int m = (l+r) >> 1;    LL res = MAX;    if(a <= m) res = query(lson);    if(m < b ) res = min(res, query(rson));    return res;}int main(){    scanf("%d", &n);    build(1, 0, n-1);    scanf("%d", &q); getchar(); while(q--) {        gets(str);        if(sscanf(str,"%d %d %d", &a, &b, &c) == 3)  {            if(a <= b) update(1, 0, n-1);            else {                int tmp = b;                b = n-1; update(1, 0, n-1);                a = 0, b = tmp; update(1, 0, n-1);            }        } else {            LL ans ;            if(a <= b) ans = query(1, 0, n-1);            else {                int tmp = b;                b = n-1; ans = query(1, 0, n-1);                a = 0, b = tmp; ans = min(ans, query(1, 0, n-1));            }            printf("%I64d\n", ans);        }    }    return 0;}


Zookeeper
How does CF show out of memory?

Windows 7 often has this problem, and Windows XP systems also have this problem. In this case, I have also encountered a Windows XP system. Simply re-register the game. The English meaning is caused by insufficient memory, but the real problem I think you have understood is that the system cannot be used for the excess memory space of the game, so where is the 4 GB memory? In addition to the memory occupied by the system, CF is swallowed up. If there is a 3250m of air, a gas pump is always aspirated without exhaust, do you say this air is enough? A game only eats memory and does not release memory. Can you say there be extra memory for the game? The problem that the game does not release memory has already been updated in several versions.

This is written by others. You can try.
An out of memory solution:
Out of memory
The memory is insufficient.
As a programmer with years of WIN32 C ++ development experience, I do not agree with WIN7
Because I do not think that the system's pre-read will occupy the user address space. At least, the pre-read function is a separate system service and an independent process. If the system memory is insufficient, windows will certainly prompt that you have never encountered it. A small triangle in the lower right corner will prompt that the virtual memory is too small.
This problem has been caused by the memory leakage of the DN program or the amount of memory requested exceeds 2 GB because the memory space is not taken into account, however, every time this BUG occurs, I observe the memory usage, but I find that it is generally 1.5 GB. Therefore, this possibility was ruled out at the beginning, but today I suddenly think of a memory ing file, and the maximum resource package of DN is exactly 0.5 GB.
Therefore, it is extremely likely that this is the cause. (Although opening a file through memory ing does not load the entire file to the memory, it occupies the address space equal to the file size, while the maximum address space of win32 system processes is 4 GB.
The Default User address space is 2 GB, that is to say, as a programmer, the maximum address space of a process is 2 GB, and when the memory is insufficient, the memory usage of the DN process is GB and the resource package address space of GB is approximately 2 GB. Therefore, when the application for memory is made will fail .)
It is difficult to understand these estimates, so let's talk about the solution. (I have no source code for DN, so I can only find a solution from other places.) The following is the focus.
Modify the WINDOWS startup configuration so that the user address space of the process reaches 3 GB.
Windows
7 system:
1. Enter the command line: Click the Start Menu-> enter "cmd" in the search box, and press ctrl + shift +
Press enter (* member mode of the command line)
2. Enter the command: bcdedit/set increaseuserva 3072
Enter
3. restart the system.
XP system:
1. Remove the read-only attributes of the boot file: Start-> RUN input "attrib
C: \ boot. ini-h-r-s "Press ENTER
2. Edit the boot file: Start-> run the input "c: \ boot. ini"
Press enter, in a format similar to "multi (0) disk (0) rdisk (0) partition (1) \ WINDOWS =" Microsoft Windows XP
"/Noexecute = optin/fastdetect" is added at the end of the line "/3 GB"
Save.
3. Restore the boot file read-only and other attributes: Start-> run the input "attrib c: \ boot. ini + h + r + s"
Enter
4. restart the system.
(Remind the XP system to edit the boot file directly on my computer-> properties-> advanced
-> Startup and fault recovery settings-> do not forget to save after editing)... the remaining full text>

How does CF show out of memory?

Windows 7 often has this problem, and Windows XP systems also have this problem. In this case, I have also encountered a Windows XP system. Simply re-register the game. The English meaning is caused by insufficient memory, but the real problem I think you have understood is that the system cannot be used for the excess memory space of the game, so where is the 4 GB memory? In addition to the memory occupied by the system, CF is swallowed up. If there is a 3250m of air, a gas pump is always aspirated without exhaust, do you say this air is enough? A game only eats memory and does not release memory. Can you say there be extra memory for the game? The problem that the game does not release memory has already been updated in several versions.

This is written by others. You can try.
An out of memory solution:
Out of memory
The memory is insufficient.
As a programmer with years of WIN32 C ++ development experience, I do not agree with WIN7
Because I do not think that the system's pre-read will occupy the user address space. At least, the pre-read function is a separate system service and an independent process. If the system memory is insufficient, windows will certainly prompt that you have never encountered it. A small triangle in the lower right corner will prompt that the virtual memory is too small.
This problem has been caused by the memory leakage of the DN program or the amount of memory requested exceeds 2 GB because the memory space is not taken into account, however, every time this BUG occurs, I observe the memory usage, but I find that it is generally 1.5 GB. Therefore, this possibility was ruled out at the beginning, but today I suddenly think of a memory ing file, and the maximum resource package of DN is exactly 0.5 GB.
Therefore, it is extremely likely that this is the cause. (Although opening a file through memory ing does not load the entire file to the memory, it occupies the address space equal to the file size, while the maximum address space of win32 system processes is 4 GB.
The Default User address space is 2 GB, that is to say, as a programmer, the maximum address space of a process is 2 GB, and when the memory is insufficient, the memory usage of the DN process is GB and the resource package address space of GB is approximately 2 GB. Therefore, when the application for memory is made will fail .)
It is difficult to understand these estimates, so let's talk about the solution. (I have no source code for DN, so I can only find a solution from other places.) The following is the focus.
Modify the WINDOWS startup configuration so that the user address space of the process reaches 3 GB.
Windows
7 system:
1. Enter the command line: Click the Start Menu-> enter "cmd" in the search box, and press ctrl + shift +
Press enter (* member mode of the command line)
2. Enter the command: bcdedit/set increaseuserva 3072
Enter
3. restart the system.
XP system:
1. Remove the read-only attributes of the boot file: Start-> RUN input "attrib
C: \ boot. ini-h-r-s "Press ENTER
2. Edit the boot file: Start-> run the input "c: \ boot. ini"
Press enter, in a format similar to "multi (0) disk (0) rdisk (0) partition (1) \ WINDOWS =" Microsoft Windows XP
"/Noexecute = optin/fastdetect" is added at the end of the line "/3 GB"
Save.
3. Restore the boot file read-only and other attributes: Start-> run the input "attrib c: \ boot. ini + h + r + s"
Enter
4. restart the system.
(Remind the XP system to edit the boot file directly on my computer-> properties-> advanced
-> Startup and fault recovery settings-> do not forget to save after editing)... the remaining full text>

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.