poj(3468)A Simple Problem with Integers

2024-06-10 21:38
文章标签 poj problem simple integers 3468

本文主要是介绍poj(3468)A Simple Problem with Integers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 POJ 3468其实算是一道比较基础的线段树的题了,但若用普通的毫不优
化的线段树去做的话,绝对是会TLE的。这道题优化的思路就是,
每次插入一个数字时,没必要把数字一直插入到叶子节点,只要有适合
的范围就插入到这个范围中,用一个增量记录它,当下一次若有询问时
,这个范围若刚好适合询问的范围,就直接把原来这个节点的值加上
增量乘以范围,再加到SUM中,就可以了,若这个节点的范围不适合查
询的范围的话,就要查询它的子节点了,呢么这时候再把这个增量传递
给它的子节点,这样在时间上效率就会比较高了。

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct point
{
 __int64 x,y,sum,add;//add记录增加值。
}a[600000];
__int64 B[600006];
void tree(__int64 t,__int64 x,__int64 y))//首先建树用递归思想建sum树,先考虑根节点然后是左右孩子节点
{
 a[t].x=x;
 a[t].y=y;
 a[t].add=0;
 if(x==y)
 {
  a[t].sum=B[x];];//建叶子节点时将其赋值 返回。
  return ;
 }
 __int64 mid=(x+y)/2;
 __int64 temp=2*t;
 tree(temp,x,mid);
 tree(temp+1,mid+1,y);
 a[t].sum=a[temp].sum+a[temp+1].sum;
}
void update(__int64 t,__int64 x,__int64 y,__int64 val)
{
 if(a[t].x==x&&a[t].y==y)
 {
  a[t].add+=val;;//将其增值存入add,返回
  return ;
 }
 a[t].sum=a[t].sum+val*(y-x+1););//修改当前结点的sum值,因为当前结点只增加了[a,b]区间内数的个数的val倍值,所以用tree[k].sum=tree[k].sum+val*(b-a+1);
 __int64 mid=(a[t].x+a[t].y)/2;
 __int64 temp=2*t;
 if(y<=mid)
  update(temp,x,y,val);
 else if(x>mid)
  update(temp+1,x,y,val);
 else
 {
  update(temp,x,mid,val);
  update(temp+1,mid+1,y,val);
 }
}

__int64 query(__int64 t,__int64 x,__int64 y)
{
 if(x==a[t].x&&y==a[t].y)//找到结点区间是查询区间,直接返回其sum+(b-a+1)*tree[k].add
  return a[t].sum+(y-x+1)*a[t].add;
 a[t].sum+=(a[t].y-a[t].x+1)*a[t].add;  //如果不匹配修改当前结点的sum值,继续搜索。
 __int64 temp=2*t;
 __int64 mid=(a[t].x+a[t].y)/2;
 update(temp,a[t].x,mid,a[t].add);//这时才需更新左右子树的sum和add值知道找到匹配 [a,b]区间位置
 update(temp+1,mid+1,a[t].y,a[t].add);
 a[t].add=0;//这时将当前结点add增值归0 因为已往下更新左右子树和当前结点sum值
 if(mid>=y));//查询分三种情况:1.[a,b]在左子树 2.[a,b]在右子树3.[a,b]跨左右子树。
  return query(temp,x,y);
 else if(x>mid)
  return query(temp+1,x,y);
 else
 return query(temp,x,mid)+query(temp+1,mid+1,y);
}
int main()
{
 __int64 m,n,i,j,a,b,val;
 while(scanf("%I64d%I64d",&n,&m)!=EOF)
 {
  for(i=1;i<=n;i++)
  {
   scanf("%I64d",&B[i]);
  }
  tree(1,1,n);
  while(m--)
  {
   char c[100];
   scanf("%s",c);
   if(c[0]=='C')
   {
    scanf("%I64d%I64d%I64d",&a,&b,&val);
    update(1,a,b,val);
   }
   else
   {
    scanf("%I64d%I64d",&a,&b);
    printf("%I64d\n",query(1,a,b));
   }
  }
 }
 return 0;
}

 

 

 关键思路在于 查询函数中。加入了更新 只有当须查询的区间不为当前值时 需要更新

当前区间的sum值及其 之下的 左右两个区间。 因为接下来的查找时要 进入更小的区间
即 当前区间的左右区间中。则需要把在此区间所加入的值分别放入其左右子树区间,
确保加入的值被一直记录。
这样从大到小的更新可以 节省程序的时间效率。若从小的区间一次更新到最后需要遍历所有的区间
而 用这个方法只需要找到 满足条件的区间即可。

 

 

 

 
延迟标记是如何延迟的呢:
就是在这个区间段增加的值,就在这个区间段上的a[t].add+=这个增值(val),同时此区间段的a[t].sum+=val*(a[t].y-a[t].x+1);
然后就不在往下进行了,当所求区间段小于上个区间段时,开始往下传a[t].add,并改变a[temp].sum,a[temp+1].sum,a[temp].add,a[temp+1].add的值;
更新,求到哪就更新的那,不求的就不管他,这样就很容易过了。。

 

 

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define N 200000
__int64 ans;
struct point
{
 int x,y;
 __int64 sum,add;
}a[N*3];
int b[100001];
void tree(int t,int x,int y)
{
 a[t].x=x;a[t].y=y;
 a[t].add=0;
 if(x==y)
 {
  a[t].sum=b[x];
  return ;
 }
 int temp=2*t;
 int mid=(x+y)/2;
 tree(temp,x,mid);
 tree(temp+1,mid+1,y); 
 a[t].sum=a[temp].sum+a[temp+1].sum;
}
void updata(int t,int x,int y,int val)
{
 if(a[t].x>y||a[t].y<x)
  return;
 if(a[t].x>=x&&a[t].y<=y)
 {
  a[t].sum+=val*(a[t].y-a[t].x+1);
  a[t].add+=val;
  return ;
 } 
 int temp=2*t;
 int mid=(a[t].x+a[t].y)/2;
 if(a[t].add)
 {
  a[temp].sum+=(a[temp].y-a[temp].x+1)*a[t].add; 
  a[temp+1].sum+=(a[temp+1].y-a[temp+1].x+1)*a[t].add; 
  a[temp].add+=a[t].add;
  a[temp+1].add+=a[t].add;
  a[t].add=0;
 }
 updata(temp,x,y,val);
 updata(temp+1,x,y,val);
 a[t].sum=a[temp].sum+a[temp+1].sum;
}
void find(int t,int x,int y)
{
 if(a[t].x>y||a[t].y<x)
  return ;
 if(a[t].x>=x&&a[t].y<=y)
 {
  ans+=a[t].sum;
  return ;
 }
 int temp=2*t;
 int mid=(a[t].x+a[t].y)/2;
 if(a[t].add)
 {
  a[temp].sum+=(a[temp].y-a[temp].x+1)*a[t].add;
  a[temp+1].sum+=(a[temp+1].y-a[temp+1].x+1)*a[t].add;
  a[temp].add+=a[t].add;
  a[temp+1].add+=a[t].add;
  a[t].add=0;
 }
 find(temp,x,y);//之所以可以这样写是因为此函数中的第一个条件在限制着不符合的范围;
 find(temp+1,x,y);
 a[t].sum=a[temp].sum+a[temp+1].sum;
}
int main()
{
 int i,h,m,n,p,t;
 char str[10];
 while(scanf("%d%d",&n,&m)!=EOF)
 {
 
  for(i=1;i<=n;i++)
  {
   scanf("%d",&b[i]);
  } tree(1,1,n);
  while(m--)
  {
   
   ans=0;
   scanf("%s",str);
   if(str[0]=='Q')
   {
    scanf("%d%d",&h,&p);
    find(1,h,p);
    printf("%I64d\n",ans);
   }
   else
   {
    scanf("%d%d%d",&h,&p,&t);
    updata(1,h,p,t);
   }
  }
 }
 return 0;
}

 

 

 

 

 

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define N 200000
__int64 ans;
int B[100001];
struct point
{
 int x,y;
 __int64 sum,add;
}a[N*3];
void tree(int t,int x,int y)
{
 a[t].x=x;a[t].y=y;
 a[t].add=0;
 if(x==y)
 {
  a[t].sum=B[x];
  return ;
 }
 int temp=2*t;
 int mid=(x+y)/2;
 tree(temp,x,mid);
 tree(temp+1,mid+1,y);
 a[t].sum=a[temp].sum+a[temp+1].sum;
}
void updata(int t,int x,int y,int val)
{
 if(a[t].x>=x&&a[t].y<=y)
 {
  a[t].sum+=val*(a[t].y-a[t].x+1);
  a[t].add+=val;
  return ;
 }
 int temp=2*t;
 int mid=(a[t].x+a[t].y)/2;
 if(a[t].add)
 {
  a[temp].sum+=(a[temp].y-a[temp].x+1)*a[t].add;
  a[temp+1].sum+=(a[temp+1].y-a[temp+1].x+1)*a[t].add;
  a[temp].add+=a[t].add;
  a[temp+1].add+=a[t].add;
  a[t].add=0;
 }
 if(y<=mid)
 updata(temp,x,y,val);
 else if(x>mid)
 updata(temp+1,x,y,val);
 else
 {
  updata(temp,x,mid,val);
  updata(temp+1,mid+1,y,val);
 }
 a[t].sum=a[temp+1].sum+a[temp].sum;
}
void find(int t,int x,int y)
{
 if(a[t].x>=x&&a[t].y<=y)
 {
  ans+=a[t].sum;
  return ;
 }
 int temp=2*t;
 int mid=(a[t].x+a[t].y)/2;
 if(a[t].add)
 {
  a[temp].sum+=(a[temp].y-a[temp].x+1)*a[t].add;
  a[temp+1].sum+=(a[temp+1].y-a[temp+1].x+1)*a[t].add;
  a[temp].add+=a[t].add;
  a[temp+1].add+=a[t].add;
  a[t].add=0;
 }
 if(y<=mid)
 find(temp,x,y);
 else if(x>mid)
 find(temp+1,x,y);
 else
 {
  find(temp,x,mid);
  find(temp+1,mid+1,y);
 }
 a[t].sum=a[temp+1].sum+a[temp].sum;
}
int main()
{
 int i,m,n,a,b,c;
 char str[10];
 while(scanf("%d%d",&n,&m)!=EOF)
 {
  for(i=1;i<=n;i++)
  {
   scanf("%d",&B[i]);
  }
  tree(1,1,n);
  while(m--)
  {
   scanf("%s",str);
   if(str[0]=='Q')
   {
    ans=0;
    scanf("%d%d",&a,&b); 
    find(1,a,b);
    printf("%I64d\n",ans);
   }
   else
   {
    scanf("%d%d%d",&a,&b,&c);
    updata(1,a,b,c);
   }
  }
 }
 return 0;
}

 

 

这篇关于poj(3468)A Simple Problem with Integers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1049338

相关文章

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

poj 3294(Life Forms) 2分+ 后缀数组

我曾用字符串hash写,但是超时了。只能用后最数组了。大致思路:用不同的符号吧字符串连接起来,构建后缀数组,然后2分答案,依次扫描后缀数组,看是否瞒住条件。 VIEW CODE #include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>#include<cassert>#

poj 2391 Ombrophobic Bovines (网络流)

这是一道很经典的网络流的题目。首先我们考虑假如我们的时间为无穷大。我们吧每个点拆成2个点 i和i' .。虚拟源点s和汇点t。对于每个点建边(s,i, a[i])  (i‘,t,ib[i]) 。 其中a[i]为给点有多少牛,b[i]为容量。i和j连通 建边 (i,j',inf);如果最大流==所有牛的个数,就可能装下所有的牛。那么现在我们考虑时间。假设最大时间为T.那么如果i到j的的最短时间>T

poj 1330 LCA 最近公共祖先

水题目。直接上代码了。 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点。实际上集合内的每个点是相同的,这样的集合就是一个强连通分量。 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图。在dag的图上面进行dp。可以先用拓扑排序后dp。或者建反响边记忆化搜索 。 VIEW

Simple-STNDT使用Transformer进行Spike信号的表征学习(一)数据处理篇

文章目录 1.数据处理部分1.1 下载数据集1.2 数据集预处理1.3 划分train-val并创建Dataset对象1.4 掩码mask操作 数据、评估标准见NLB2021 https://neurallatents.github.io/ 以下代码依据 https://github.com/trungle93/STNDT 原代码使用了 Ray+Config文件进行了参数搜

poj 1564 Sum It Up -- DFS 递归

题意:给一个数 t ,以及 n 个数,求 n 个数中的几个数加起来的和为 t 的情况有多少种。 注意:题目要求相同的组合方式不能出现2次,即 “3 4 1 1 1 1 ” 的结果为:“1+1+1”。 思路:一个  for  循环遍历一遍,每个 i 表示以当前数为起点开始一次DFS递归,当所遍历的和为 t 时,输出该组合方式,如果大于 t 则返回,小于则往下递归。以二维数组保存已经输出过的数据,

道路 Road(百练,POJ)

题目链接: 1724 -- ROADS (poj.org) 题目描述: 思路: 这题目乍一看,是一个含有2个标尺的单源最短路问题,挺难处理的。 既然没法直接用最短路处理,那我们就“记录信息”,将花费的时间也记录进dp数组,然后跑“状态最短路”。 用f[i][j] 表示到达点i 且 总花费时间为j的最短距离,然后跑堆优化的dijkstra算法就好。由于不含有负边权,因此可以搞一个vis数

约瑟夫问题(Josephus Problem)4:第k个出列的人是谁

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/16813419 本文是论述约瑟夫问题的第四部分,约瑟夫问题的描述在第一部分,本文用到了第三部分的算法。请先阅读第一部分和第三部分。 现在要求输出第k个出列的人的编号。 由第三部分的算法分析可知,规模为i-1的队列的任意一人的编号为p,规

约瑟夫问题(Josephus Problem)3:谁最后一个出列

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/16813349 本文是论述约瑟夫问题的第三部分,约瑟夫问题的描述在第一部分。请先阅读第一部分。 现在要求输出最后一个出列的人的编号。 第一次见到这个问题是在我高一的时候,那时候搞NOIP,培训的时候碰到了这个题目,当时没想到好的方法,