poj2352~树状数组~线段树尝试失败~
来源:互联网 发布:云南白药面膜 知乎 编辑:程序博客网 时间:2024/05/16 09:41
啊啊啊,刚学线段树,用线段树写这题不断的TLE!我自己弄了15000个数据,秒算的,结果还是说我TLE,搞了2个小时没看出哪里能超时— —。
然后用之前学的树状。十分钟就搞定了,两种代码都先放这里,线段树那个以后再拿出来看看哪里TLE
/********树状数组解法*************/
#include<iostream>#include<string>using namespace std;int c[35000],Sum[15010];int Getsum(int i) //查询0到i区间的数值状况{int s=0;while(i>0){s+=c[i];i-=i&(-i);}return s;}void update(int i,int value)//更新0到i区间的数值状况{while(i<=35500){c[i]+=value;i+=i&(-i);}}int main(){int n,i,j,x,y,s;scanf("%d",&n);memset(Sum,0,sizeof(Sum));for(i=0;i<n;i++){scanf("%d%d",&x,&y);s=Getsum(x+1); //为了防止0的情况,这里的X统一都加个1Sum[s]++;update(x+1,1);}for(i=0;i<n;i++)printf("%d\n",Sum[i]);}
下面是错误的线段树解法,因为是第一次写代码很长,如果有兴趣帮我看看哪错我会很感激的~
#include<iostream>#include<string>#include<fstream>using namespace std;int Sum[16000];typedef struct STAR{ int left; int right; int cover; STAR *leftchild; STAR *rightchild; };STAR * build(int l , int r ) { STAR *root=new STAR;root->cover=0; root->left = l; root->right = r; root->leftchild = NULL; root->rightchild = NULL; if ( l +1<= r ) { int mid = (r+l) /2; root->leftchild = build ( l , mid ) ; root->rightchild = build ( mid+1 , r) ; } return root; }void Insert(int c, int d , STAR *root ){ if(root==NULL) return ;int mid=(root->left+root->right)/2;if(c==root->left&&d==root->right) root->cover++; else{root->cover++;if(d<=mid)Insert(c,d,root->leftchild);else if(c>mid)Insert(c,d,root->rightchild);}}int Count(STAR *root,int c,int d){ int mid;if(root==NULL) return 0;mid=(root->left+root->right)/2;if(root->left==c&&root->right==d) return root->cover;else{ if(d<=mid)return Count(root->leftchild,c,d); else {return Count(root->leftchild,c,mid)+Count(root->rightchild,mid+1,d); }}}int main(){int n,i,j,x,y,s;STAR *root;scanf("%d",&n);root=build(0,32100);for(i=0;i<n;i++)Sum[i]=0;for(i=0;i<n;i++){scanf("%d%d",&x,&y);s=Count(root,0,x);Sum[s]++;Insert(x,x,root);}for(i=0;i<n;i++)printf("%d\n",Sum[i]);return 0;}
0 0
- poj2352~树状数组~线段树尝试失败~
- 【树状数组】尝试 POJ2352-Stars
- poj2352(线段树或树状数组)
- poj2352--Stars--线段树&树状数组
- POJ2352 Stars 【树状数组】or【线段树】
- POJ2352 Stars(线段树 & 树状数组)
- Poj2352-树状数组或线段树
- 见微知著----POJ2352(树状数组 或 线段树)
- poj2352树状数组入门
- 【树状数组】 poj2352 Stars
- POJ2352 stars(树状数组)
- 简单树状数组-poj2352
- POJ2352(树状数组)
- poj2352 - Stars(树状数组)
- POJ2352树状数组
- poj2352之树状数组
- poj2352 树状数组
- POJ2352树状数组
- POJ 题目分类
- PCA降维
- 优先队列
- 实用网址大全
- Tesseract-OCR 字符识别---样本训练
- poj2352~树状数组~线段树尝试失败~
- 转:Android网络编程之http通信
- HDU 3081 Marriage Match II —并查集,最大流
- CoreJava 8th V-1 P33 Unicode编码
- 转 undefined reference to `ts_read_raw'
- tesseract-ocr识别中文扫描图片实例讲解
- 如何访问Google
- 后台线程使用 SynchronizationContext 更新主界面
- 《单元测试的艺术(第2版)》