zhber
有好多做过的题没写下来,如果我还能记得就补吧

In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.

A sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1 < a2 < a3 < ... < ak, will be understood by as

The operator stands for taking the remainder, that is stands for the remainder of dividing x by y.

To organize exercise testing quickly calculating the sum of medians for a changing set was needed.

Input

The first line contains number n (1 ≤ n ≤ 105), the number of operations performed.

Then each of n lines contains the description of one of the three operations:

  • add x — add the element x to the set;
  • del x — delete the element x from the set;
  • sum — find the sum of medians of the set.

For any add x operation it is true that the element x is not included in the set directly before the operation.

For any del x operation it is true that the element x is included in the set directly before the operation.

All the numbers in the input are positive integers, not exceeding 109.

Output

For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).

Examples
Input
6
add 4
add 5
add 1
add 2
add 3
sum
Output
3
Input
14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3
add 4
add 10
sum
del 1
sum
Output
5
11
13

 

要有一个支持自动排序的数据结构,然后支持询问所有rnk=k*5+3的数字的和

离散完实际上只有10w个不同的数

按照rnk建个线段树,每个节点存5个点,sum[i]是当前rnk%5==i的数字之和

每次加一个数,只要在rnk[a[i]]+1到n上面所有sum[k]往右移一位

 

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<deque>
  9 #include<set>
 10 #include<map>
 11 #include<ctime>
 12 #define LL long long
 13 #define inf 0x7ffffff
 14 #define pa pair<int,int>
 15 #define mkp(a,b) make_pair(a,b)
 16 #define pi 3.1415926535897932384626433832795028841971
 17 using namespace std;
 18 inline LL read()
 19 {
 20     LL 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 char s[10];
 26 int n,m,x;
 27 int d[100010];
 28 map<int,int>mp;
 29 struct opr{int op,x;}q[100010];
 30 struct segtree{
 31     LL sum[5];
 32     int l,r,tag,tot;
 33 }t[100010*8];
 34 LL swp[5];
 35 inline void gao(int k,int tt)
 36 {
 37     int t2=0;t[k].tot+=tt;
 38     for (int i=0;i<5;i++)
 39     {
 40         t2=i+tt;if (t2>=5)t2-=5;if (t2<0)t2+=5;
 41         swp[t2]=t[k].sum[i];
 42     }
 43     for (int i=0;i<5;i++)t[k].sum[i]=swp[i];
 44 }
 45 inline void pushdown(int k)
 46 {
 47     int tt=t[k].tag;t[k].tag=0;
 48     t[k<<1].tag+=tt;t[k<<1|1].tag+=tt;
 49     if (t[k<<1].tag>=5)t[k<<1].tag-=5;if (t[k<<1].tag<0)t[k<<1].tag+=5;
 50     if (t[k<<1|1].tag>=5)t[k<<1|1].tag-=5;if (t[k<<1|1].tag<0)t[k<<1|1].tag+=5;
 51     gao(k<<1,tt);gao(k<<1|1,tt);
 52 }
 53 inline void update(int k)
 54 {
 55     for (int i=0;i<5;i++)t[k].sum[i]=t[k<<1].sum[i]+t[k<<1|1].sum[i];
 56 }
 57 inline void buildtree(int now,int l,int r)
 58 {
 59     t[now].l=l;t[now].r=r;
 60     if (l==r)return;
 61     int mid=(l+r)>>1;
 62     buildtree(now<<1,l,mid);
 63     buildtree(now<<1|1,mid+1,r);
 64 }
 65 inline void rotate(int now,int x,int y,int d)
 66 {
 67     pushdown(now);
 68     int l=t[now].l,r=t[now].r;
 69     if(l==x&&r==y)
 70     {
 71         t[now].tag+=d;
 72         gao(now,d);
 73         return;
 74     }
 75     int mid=(l+r)>>1;
 76     if (y<=mid)rotate(now<<1,x,y,d);
 77     else if (x>mid)rotate(now<<1|1,x,y,d);
 78     else rotate(now<<1,x,mid,d),rotate(now<<1|1,mid+1,y,d);
 79     update(now);
 80 }
 81 inline void insert(int now,int x,int z)
 82 {
 83     pushdown(now);
 84     int l=t[now].l,r=t[now].r;
 85     if (l==x&&r==x)
 86     {
 87         if (z==1)t[now].sum[t[now].tot%5]+=d[x];
 88         else if (z==-1)t[now].sum[t[now].tot%5]-=d[x];
 89         return;
 90     }
 91     int mid=(l+r)>>1;
 92     if (x<=mid)insert(now<<1,x,z);
 93     else insert(now<<1|1,x,z);
 94     update(now);
 95 }
 96 int main()
 97 {
 98     m=read();
 99     for (int i=1;i<=m;i++)
100     {
101         scanf("%s",s+1);
102         if (s[1]=='a')
103         {
104             x=read();
105             q[i].op=1;q[i].x=x;
106             d[++n]=x;
107         }else if (s[1]=='d')
108         {
109             x=read();
110             q[i].op=2;q[i].x=x;
111         }else q[i].op=3;
112     }
113     if (!n)
114     {
115         for (int i=1;i<=m;i++)puts("0");
116         return 0;
117     }
118     sort(d+1,d+n+1);
119     for (int i=1;i<=n;i++)mp[d[i]]=i;
120     for (int i=1;i<=m;i++)if (q[i].x)q[i].x=mp[q[i].x];
121     buildtree(1,1,n);
122     for (int i=1;i<=m;i++)
123     {
124         if (q[i].op==1)
125         {
126             rotate(1,q[i].x,n,1);
127             insert(1,q[i].x,1);
128         }else if (q[i].op==2)
129         {
130             rotate(1,q[i].x,n,-1);
131             insert(1,q[i].x,-1);
132         }else if (q[i].op==3)
133         {
134             printf("%lld\n",t[1].sum[3]);
135         }
136     }
137 }
cf85D

 

posted on 2017-08-04 14:01  zhber  阅读(276)  评论(0编辑  收藏  举报