【set】【multiset】bzoj1058 [ZJOI2007]报表统计

对n个位置,每个位置维护一个vector。

每次插入,可能对MIN_SORT_GAP产生的影响,只可能是 插入元素 和 它的 前驱 后继 造成的,用一个set维护(存储所有序列中的元素)。

我们还得维护一个multiset(存储相邻元素的差值),可能对MIN_GAP产生影响,只可能是它在序列中的前一个元素和后一个元素造成的,但同时要删除它两侧的元素的差值(有可能有重复的,我们不应多删)。

 1 #include<cstdio>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<set>
 5 using namespace std;
 6 vector<int>a[500001];
 7 set<int>s1;
 8 multiset<int>s2;
 9 typedef set<int>::iterator ITER;
10 typedef pair<ITER,bool> Point;
11 int n,m,t,x,y,MIN_SORT_GAP=2147483647;
12 char op[12];
13 int Abs(const int &x){return x<0 ? -x : x;}
14 int main()
15 {
16     scanf("%d%d",&n,&m);
17     for(int i=1;i<=n;i++)
18       {
19           scanf("%d",&t);
20           a[i].push_back(t);
21           s1.insert(t);
22           if(i!=1) s2.insert(Abs(t-(*a[i-1].begin())));
23       }
24     for(ITER it=s1.begin();it!=s1.end();it++)
25       {
26           int t=(*it); if((++it)==s1.end()) break;
27           MIN_SORT_GAP=min(MIN_SORT_GAP,(*it)-t);
28         --it;
29       }
30     for(int i=1;i<=m;i++)
31       {
32           scanf("%s",op);
33           if(op[0]=='I')
34             {
35                 scanf("%d%d",&x,&y);
36                 s2.insert(Abs(y-a[x][a[x].size()-1]));
37                 if(x!=n)
38               {
39                     s2.erase(s2.find(Abs(a[x][a[x].size()-1]-(*a[x+1].begin()))));
40                   s2.insert(Abs(y-(*a[x+1].begin())));
41               }
42             a[x].push_back(y);
43                 Point t=s1.insert(y);
44                 if(!MIN_SORT_GAP) continue;
45                 if(t.second)
46                   {
47                       if(t.first!=s1.begin())
48                       {
49                           --t.first;
50                           MIN_SORT_GAP=min(MIN_SORT_GAP,y-(*t.first));
51                       }
52                     ++t.first; if((++t.first)!=s1.end())
53                       MIN_SORT_GAP=min((*t.first)-y,MIN_SORT_GAP);
54                   }
55                 else MIN_SORT_GAP=0;
56             }
57           else if(op[4]=='S') printf("%d\n",MIN_SORT_GAP);
58           else printf("%d\n",*s2.begin());
59       }
60     return 0;
61 }
posted @ 2014-11-21 21:57  AutSky_JadeK  阅读(179)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト