hdu 4288 线段树 Sum of Medians 像lazysales girl Coder
2012-09-16 20:21
423 查看
题意:
就是不停地插数,让数按从小到大排。求第3,8,13...个数的和。
解:
线段树。线段树的坐标表示在所有给出的数当中第i‘大的数(0就是还没有,>0就是第i大的数已经插入了)。然后ans[][i]表示当前区间(以第i (i为下标)个数% 5 )相加的和(可能表述有点不清楚哈。。)意会,意会。 最后是O(1)查询。
做题过程:
终于有一道网赛有思路的题了。。。当时的思路有一点偏颇。比赛时我的线段树的节点含义是对的,就是我居然表示的是所有整个区间的。。。导致我后面的update我就不知道怎么update了。。。。而且,这题是我即将要做的题。我得加快做题速度了,如果现场赛这样子,那还真是有点后悔。
比赛的代码。。。
就是不停地插数,让数按从小到大排。求第3,8,13...个数的和。
解:
线段树。线段树的坐标表示在所有给出的数当中第i‘大的数(0就是还没有,>0就是第i大的数已经插入了)。然后ans[][i]表示当前区间(以第i (i为下标)个数% 5 )相加的和(可能表述有点不清楚哈。。)意会,意会。 最后是O(1)查询。
做题过程:
终于有一道网赛有思路的题了。。。当时的思路有一点偏颇。比赛时我的线段树的节点含义是对的,就是我居然表示的是所有整个区间的。。。导致我后面的update我就不知道怎么update了。。。。而且,这题是我即将要做的题。我得加快做题速度了,如果现场赛这样子,那还真是有点后悔。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define maxn 111111 #define lson l, m , rt << 1 #define rson m + 1, r, rt <<1 |1 #define havem int m = (l + r) >> 1 using namespace std; int sum[maxn << 2],n,x[maxn],flag, add[maxn],xsub; __int64 ans[maxn << 2][5]; char op[maxn][5]; void build(int l ,int r , int rt){ for(int i = 0; i < 5; i ++) ans[rt][i] = 0; sum[rt] = 0; if(l == r) return ; havem; build(lson); build(rson); } void push_up(int rt){ for(int i = 0; i < 5; i ++) ans[rt][i] = ans[rt << 1][i] + ans[rt << 1 | 1][ ( (i - sum[rt << 1]) % 5 + 5) % 5];//一定是取余加5再取余 } void update(int pos,int l, int r, int rt){ sum[rt] += 2 * flag - 1; if(l == r){ ans[rt][0] = x[pos] * flag; return; } int m = (l + r) >> 1; if(pos <= m) update(pos,lson); else update(pos,rson); push_up(rt); } int main(){ while(~scanf("%d",&n)){ xsub = 0; for(int i = 0; i < n; i ++){ scanf("%s",op[i]); if(op[i][0] == 'a' || op[i][0] == 'd') scanf("%d",&add[i]), x[xsub ++] = add[i]; } sort(x,x + xsub); xsub = unique(x, x + xsub) - x; build(0,xsub,1); //从1开始 for(int i = 0; i < n; i ++){ if(op[i][0] != 's') { int low = lower_bound(x,x + xsub,add[i]) - x; if(op[i][0] == 'a') flag = 1; else flag = 0; update(low,0,xsub,1); }else printf("%I64d\n",ans[1][2]); } } return 0; }
比赛的代码。。。
/* Pro: 0 Sol: date: */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> #define lson l , m , rt << 1 #define rson m + 1, r , rt << 1 | 1 #define maxn 111111 // using namespace std; int X[maxn],n,add[maxn],xsub; __int64 p[maxn << 2][6]; char op[maxn][10]; short pp[maxn]; void build(int l, int r, int rt){ for(int i = 0; i < 5; i ++) p[rt][i] = 0; if(l == r) return ; int m = (l + r) >> 1; build(lson); build(rson); } void push_up(int rt){ for(int i = 0; i < 5; i ++) p[rt][i] = p[rt << 1][i] + p[rt << 1 | 1][i]; } void update(int pos, short ii, int c, int l,int r, int rt){// if(l == r) { p[rt][ii] += c * X[pos]; return ; } int m = (l + r) >> 1; if(pos <= m) update(pos,ii,c,lson); else update(pos,ii,c,rson); push_up(rt); } int main(){ while(~scanf("%d",&n)){ xsub = 0; for(int i = 0; i < n; i ++){ scanf("%s",&op[i]); if(op[i][0] != 's') scanf("%d",&add[i]), X[xsub ++] = add[i]; } sort(X, X + xsub); int m = 1; for(int i = 1; i <xsub; i ++){ if(X[i] != X[i - 1]) X[m ++] = X[i]; } build(0, m, 1); int x = 0; for(int i = 0; i < n; i ++){ if(op[i][0] == 'a'){ int ll = lower_bound(X,X + m,add[i]) - X; x ++, update(ll, x % 5, 1, 0 , m , 1);//??? pp[ll] = x % 5; } else if(op[i][0] == 'd'){ int ll = lower_bound(X,X + m,add[i]) - X; x -- , update(ll, pp[ll], -1, 0,m, 1); } else{ if(3 > x) printf("0\n"); else{ printf("%I64d !!! \n",p[1][3]); } } } } return 0; }
相关文章推荐
- hdu 4288 Coder & CF85-D Sum of Medians (单点更新)
- Coderforces 85 D. Sum of Medians(线段树单点修改)
- HDU 4288 Coder || CodeForces - 85D Sum of Medians (线段树)
- hdu 4288 Coder (线段树)
- HDU 4288 Coder 【线段树+离线处理+离散化】
- HDU-4288-Coder(线段树)
- Yandex-R1 Sum of Medians [线段树]
- HDU 4288 Coder(线段树或者暴力vector)#by zh
- HDU 4288 Coder 线段树
- HDU 4288 Coder (技巧性暴力模拟+二分||线段树+离线操作)
- HDU-4288 Coder 线段树
- ZOJ 3606 Lazy Salesgirl ( 线段树 + 思路 )
- ZOJ 3606 Lazy Salesgirl 线段树
- HDU 4288 Coder(12年成都 线段树)
- CF-85D-Sum of Medians(线段树)
- hdu 4288 Coder(树形结构-线段树)
- hdu 4288 Coder(线段树)
- hdu 4288 Coder (线段树)
- HDU 4288 Coder(线段树)
- HDU 4288 Coder 线段树维护区间%5的和