HDU-4288 Coder 线段树

题目链接


题目大意:给你一个有序集合,给你n个坐标进行插入,删除,求最终所有下标摸为3的数和;



#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100005;
const int inf = 1<<30;//0x7f;
typedef __int64 LL;
int n,q,tot;
int pos,val;
struct node
{
	int cnt;
	LL sum[5];//sum[i]表示子树中模为i的所以数之和
}tree[maxn<<2];
int data[maxn],tmp[maxn];
char ope[maxn][10];
void buildtree( int root,int ld,int rd )
{
	tree[root].cnt = 0;
	for( int i = 0; i < 5; i ++ )	tree[root].sum[i] = 0;
	if( ld < rd ){
		int mid = (ld+rd)>>1;
		buildtree( root<<1,ld,mid );
		buildtree( root<<1|1,mid+1,rd );
	}
}
void pushup( int root )
{
	for( int i = 0; i < 5; i ++ )
		tree[root].sum[i] = tree[root<<1].sum[i] + tree[root<<1|1].sum[((i-tree[root<<1].cnt)%5+5)%5];    //***
}
void updata( int root,int ld,int rd,int op )
{
	tree[root].cnt += op;
	if( ld == rd ){
		tree[root].sum[0] += val;
		return;
	}
	int mid = (ld+rd)>>1;
	if( pos <= mid )	updata( root<<1,ld,mid,op );
	else	updata( root<<1|1,mid+1,rd,op );
	pushup(root);
}
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("data.txt","r",stdin);
	#endif
	while( scanf("%d",&n) != EOF )
	{
		tot = 0;
		for( int i = 0; i < n; i ++ ){
			scanf("%s",&ope[i]);
			if( ope[i][0] != 's' ){
				scanf("%d",&data[i]);
				tmp[tot++] = data[i];
			}
		}
		sort( tmp,tmp+tot );              //排序
		tot = unique( tmp,tmp+tot ) - tmp;//去重
		buildtree( 1,1,tot );
		for( int i = 0; i < n; i ++ ){
			val = data[i];
			pos = lower_bound( tmp,tmp+tot,data[i] ) - tmp;
			if( ope[i][0] == 'a' )	updata(1,1,tot,1);
			else if( ope[i][0] == 'd' )	val *= -1,updata(1,1,tot,-1);
			else  printf("%I64d\n",tree[1].sum[2]);
		}
	}
    return 0;
}



网上找的暴力解:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
int n;
char str[5];
vector<int> num;
vector<int>::iterator it;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        num.clear();
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            if(str[0]=='a')
            {
                int temp;
                scanf("%d",&temp);
                it=lower_bound(num.begin(),num.end(),temp);
                num.insert(it,temp);
            }
            else if(str[0]=='d')
            {
                int temp;
                scanf("%d",&temp);
                it=lower_bound(num.begin(),num.end(),temp);
                num.erase(it);
            }
            else
            {
                __int64 ans=0;
                for(int i=2;i<num.size();i+=5)
                {
                    ans+=num[i];
                }
                printf("%I64d\n",ans);
            }
        }
    }
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值