。 Due to the time is sufficient, you need to understand the functions inside the STL.
。 It's just that it's very inefficient. finally ran out more than 6,000 Ms.
Another approach is the line tree approach, which maintains 5 segment trees. The number of each node that is stored in the structure. First, because the segment tree does not support the insertion, deletion, to maintain a number of CNT, when inserting a number, you see the original%3 number, now take surplus is certainly equal to 2, then how to do?? Then this CNT has a wonderful role to play. Each time you insert a delete, the corresponding number of nodes is changed to maintain the 5 tree segment.
。
Finally, because there is no data to tell the scope, so to take discretization, and then off-line processing, and finally get all the total number of operations, and then build on this. The first time with discretization, think good tall on ... code: (from Cxlove)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <vector> #include <cmath> #include <string> #include <queue> #define EPS 1e-9# Define ll long Long#define INF 0x3f3f3f3fusing namespace Std;const int maxn=100000+10;int N,a[maxn],b[maxn];char OP[MAXN] [5];struct tree{int cnt; ll sum[5];} Tree[maxn<<2];void buildtree (int l,int r,int dex) {tree[dex].cnt=0; memset (tree[dex].sum,0,sizeof (tree[dex].sum)); if (l==r) return; int mid= (L+R) >>1; Buildtree (l,mid,dex<<1); Buildtree (mid+1,r,dex<<1|1);} void push_up (int dex) {for (int i=0;i<5;i++) TREE[DEX].SUM[I]=TREE[DEX<<1].SUM[I]+TREE[DEX<<1|1].S um[((i-tree[dex<<1].cnt)%5+5)%5];} void update (int l,int r,int dex,int pos,int flag,int val) {tree[dex].cnt+=flag; if (l==r) {if (flag==1) Tree[dex].sum[1]=val; else tree[dex].sum[1]=0; Return} int mid= (L+R) >>1; if (pos<=mid) update (l,mid,dex<<1,pos,flag,val); else update (mid+1,r,dex<<1|1,pos,flag,val); PUSH_UP (DEX);} int main () {int tot,pos,flag; while (~SCANF ("%d", &n)) {tot=0; for (int i=1;i<=n;i++) {scanf ("%s", Op[i]); if (op[i][0]!= ' s ') {scanf ("%d", &b[i]); A[tot++]=b[i]; }} sort (A,a+tot); Tot=unique (A,a+tot)-A; if (tot==0) memset (tree[1].sum,0,sizeof (tree[1].sum)); else Buildtree (1,tot,1); for (int i=1;i<=n;i++) {pos=lower_bound (A,a+tot,b[i])-A; pos++; if (op[i][0]== ' a ') {flag=1; Update (1,tot,1,pos,flag,b[i]); } else if (op[i][0]== ' d ') {flag=-1; Update (1,tot,1,pos,flag,b[i]); } else printf ("%i64d\n", tree[1].sum[3]); }} return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
hdu4288 Coder (segment tree + detach)