Codeforces 85 D. Sum of Medians
2017-03-24 20:18
295 查看
题目链接:http://codeforces.com/contest/85/problem/D
做法果然男默女泪啊.....
大概就是直接开了一个$vector$每次插入删除都用自带的$insert$和$erase$,然后查询也是暴力搞。
那么为啥么过得很有理有据呢?
1.首先考虑如果没有修改我就能继承上一次的答案...
2.修改我们假设(就是)${O(logn)}$的。
3.每次暴力查询是$5$个数字一步。
4.显然一开始并不是上来就有${100000}$个数字
所以大概复杂度会是${O(n^{2}/40)}$的....
当然正解也是可以想出来的,就是一个线段树每个点维护所有数字%5=x的数字和,然后记录右移多少。
做法果然男默女泪啊.....
大概就是直接开了一个$vector$每次插入删除都用自带的$insert$和$erase$,然后查询也是暴力搞。
那么为啥么过得很有理有据呢?
1.首先考虑如果没有修改我就能继承上一次的答案...
2.修改我们假设(就是)${O(logn)}$的。
3.每次暴力查询是$5$个数字一步。
4.显然一开始并不是上来就有${100000}$个数字
所以大概复杂度会是${O(n^{2}/40)}$的....
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<cstdlib> #include<cmath> #include<cstring> using namespace std; #define maxn 10010 #define llg int #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); llg n,m,w,x; vector<llg>a; long long ans; bool pd; char s[6]; inline int getint() { int w=0,q=0; char c=getchar(); while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w; } int main() { yyj("sum"); cin>>n; while (n--) { scanf("%s",s); if (s[0]=='s') { if (!pd) {printf("%lld\n",ans); continue;} ans=0; w=a.size(); for (llg i=2;i<w;i+=5) ans+=a[i]; pd=false; printf("%lld\n",ans); } else { x=getint(); if (s[0]=='a') a.insert(lower_bound(a.begin(),a.end(),x),x); else a.erase(lower_bound(a.begin(), a.end(), x)); pd=true; } } return 0; }
当然正解也是可以想出来的,就是一个线段树每个点维护所有数字%5=x的数字和,然后记录右移多少。
相关文章推荐
- CodeForces 85 D.Sum of Medians(线段树)
- codeforces 85D D. Sum of Medians Vector的妙用
- Coderforces 85 D. Sum of Medians(线段树单点修改)
- Codeforces 616E - Sum of Remainders
- Codeforces 396B:On Sum of Fractions
- hdu 4288 线段树 Sum of Medians 像lazysales girl Coder
- Codeforces 847 C. Sum of Nestings (技巧)
- hdu 4288 Coder & CF85-D Sum of Medians (单点更新)
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- codeforces 616E - Sum of Remainders
- codeforces—— 770B —— Maximize Sum of Digits
- CodeForces 489C (贪心) Given Length and Sum of Digits...
- Codeforces 489C - Given Length and Sum of Digits...(贪心)
- Codeforces #277.5 (Div. 2) C. Given Length and Sum of Digits...(简单贪心)
- codeforces#277.5 C. Given Length and Sum of Digits
- Yandex-R1 Sum of Medians [线段树]
- CF-85D-Sum of Medians(线段树)
- Codeforces-489C-Given Length and Sum of Digits...
- CF85D:Sum of Medians(STL)
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树