Bzoj1058: [zjoi2007] report statistics

Source: Internet
Author: User
1058: [zjoi2007] report statistics time limit: 15 sec memory limit: 162 MB
Submit: 1751 solved: 614
[Submit] [Status] Description

Q's mother is a cashier who often needs to make some statistical reports. Today is my mother's birthday. Xiao Q hopes to share some work with her mother as one of her birthday gifts. After careful observation, Xiao Q found that counting a report actually maintains a non-negative integer series and performs some query operations. At the beginning, there was an integer sequence whose length was N, and there were three operations: insert I k added a new element K after the I element of the original series; if several elements have been added to the I element of the original sequence, the min_gap query returns the difference (absolute value) between two adjacent elements at the end of these elements (see the example below) the minimum value of min_sort_gap to query the difference (absolute value) between the two elements closest to all elements. For example, if the sequence at the beginning is 5 3 1, execute the insert 2 9 operation, you will get: 5 3 9 1 at this time, min_gap is 2, and min_sort_gap is 2. Then execute insert 2 6 to get the result: 5 3 9 6 1 note that a 9 is added after the first element of the original sequence. At this time, the added 6 should be added after 9. In this case, min_gap is 2 and min_sort_gap is 1. Therefore, Xiao Q wrote a program that enables the program to automatically complete these operations. However, he found that the program runs slowly for some large reports. Can you help him improve the program?

Input

The first row contains two integers, N and M, indicating the length of the original sequence and the number of operations. The second behavior is an integer of N, which is the initial sequence. The next m rows have one operation per row, that is, one of "insert I k", "min_gap", and "min_sort_gap" (no extra space or empty rows ).

Output

For each "min_gap" and "min_sort_gap" command, output a line of answer.

Sample input3 5
5 3 1
Insert 2 9
Min_sort_gap
Insert 2 6
Min_gap
Min_sort_gap
Sample output2
2
1
Hint

 

For 30% of data, n ≤ 1000, m ≤ 5000 for 100% of data, n, m ≤ 500000 for all data, the integer in the sequence cannot exceed 5*108.

 

Source

Question:

STL great! Completely copied hzwer...

1. when inserting data, we can open two arrays, St [I] And Ed [I], to record the first value of this position, what is the last (INSERTED) value?

2. A set is used to query the frontend and successor, to solve the min sort gap, and to combine with the heap

A set is used to store the absolute values of the difference between two adjacent elements, insert a new value, and delete the absolute values of the difference before and after it is used to solve the min gap

Note the following points:

1. Priority is a large root heap by default. If a small root heap is used, greater (INT) is required)

2.> two links that are not stream operations will be judged wrong, so they must be separated.

3. Rand (), ABS () requires a header file # include <cstdlib>

4. int L = * -- B. lower_bound (x), r = * B. lower_bound (x); find the precursor and successor (although I don't know what the first one means ...)

Code:

 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #include<map> 7 #include<queue> 8 #include<vector> 9 #include<cstdlib>10 #define maxn 500000+100011 #define inf 100000000012 #define mod 100000013 using namespace std;14 set<int>a,b;15 map<int,int>mp;16 priority_queue<int,vector<int>,greater<int> >q;17 int n,m,st[maxn],ed[maxn];18 inline int read()19 {20     int x=0,f=1;char ch=getchar();21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}22     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}23     return x*f;24 }25 void insert(int x)26 {27 28     mp[x]++;29     if(mp[x]==1)a.insert(x);30 31 }32 void push(int x)33 {34     int l=*--b.lower_bound(x),r=*b.lower_bound(x);35     q.push(min(x-l,r-x));36     b.insert(x);37 }38 int main()39 {40     freopen("input.txt","r",stdin);41     freopen("output.txt","w",stdout);42     n=read();m=read();43     b.insert(inf);b.insert(-inf);44     for(int i=1;i<=n;i++)45     {46         int x=read();st[i]=ed[i]=x;push(x);47     }48     for(int i=2;i<=n;i++)insert(abs(st[i]-st[i-1]));49     char ch[12];int x,y,z;50     for(int i=1;i<=m;i++)51     {52         scanf("%s",ch);53         if(ch[0]==‘I‘)54         {55             x=read();y=read();56             if(x!=n)57             {58                z=abs(st[x+1]-ed[x]);59                mp[z]--;60                if(!mp[z])a.erase(z);    61             }62             insert(abs(st[x+1]-y));63             insert(abs(y-ed[x]));64             ed[x]=y;push(y);65         }else if(ch[4]==‘S‘)printf("%d\n",q.top());66         else printf("%d\n",*a.begin());67     }68     return 0;69 }
View code

 

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.