狂K 线段树
想写好树剖~~线段树very important
HDU 1166 敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 83545 Accepted Submission(s): 35301
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
6
33
59
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 50010
struct Tree{
int l,r,sum;
}tre[maxn * +];
int T,n;
void Built(int l,int r,int pos){
tre[pos].l=l;tre[pos].r=r;
if(l==r){ scanf("%d",&tre[pos].sum);return; }
int mid=(l+r)/;
Built(l,mid,pos*);
Built(mid+,r,pos*+);
tre[pos].sum=tre[pos<<].sum+tre[pos<<|].sum;
}
int Query(int l,int r,int pos){
int ans=;
if(l<=tre[pos].l&&tre[pos].r<=r)return tre[pos].sum;
int mid=(tre[pos].l+tre[pos].r)/;
if(mid>=l)ans+=Query(l,r,pos<<);
if(mid<r)ans+=Query(l,r,pos<<|);
tre[pos].sum=tre[pos<<].sum+tre[pos<<|].sum;
return ans;
}
void Add(int pos,int num,int now){
if(tre[now].l==tre[now].r){ tre[now].sum+=num;return ;}
int mid=(tre[now].l+tre[now].r)>>;
if(pos<=mid)Add(pos,num,now<<);
if(pos>mid)Add(pos,num,now<<|);
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
}
int main()
{
cin>>T;
int k=,x,y;
char str[];
while(T--){
memset(tre,,sizeof(tre));
scanf("%d",&n);
printf("Case %d:\n",k++);
Built(,n,);
while(){
scanf("%s",str);
if(strcmp(str,"End")==) break;
scanf("%d%d",&x,&y);
if(strcmp(str,"Query")==){
printf("%d\n",Query(x,y,));
}
if(strcmp(str,"Add")==){// zeng jia
Add(x,y,);
}
if(strcmp(str,"Sub")==){//jian shao
Add(x,-y,);
}
}
}
return ;
}
1698 Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30143 Accepted Submission(s): 14884
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
10
2
1 5 2
5 9 3
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100000
struct Node{
int l,r,data,sum;
}tre[N*];
int n,m,x1,x2,x3,t;
void Built(int now,int l,int r){
tre[now].l=l;tre[now].r=r;
if(l==r)tre[now].sum=tre[now].data=;
else{
int mid=(l+r)>>;
Built(now<<,l,mid);Built(now<<|,mid+,r);
tre[now].data=;
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
}
}
void UpData(int now,int l,int r,int val){
if(tre[now].l==l&&tre[now].r==r){
tre[now].data=val;
tre[now].sum=(r-l+)*val;
}
else{
if(tre[now].data>){
tre[now<<].data=tre[now].data;
tre[now<<].sum=(tre[now<<].r-tre[now<<].l+)*tre[now<<].data; tre[now<<|].data=tre[now].data;
tre[now<<|].sum=(tre[now<<|].r-tre[now<<|].l+)*tre[now<<|].data;
}
tre[now].data=;
int mid=(tre[now].l+tre[now].r)>>;
if(r<=mid) UpData(now<<,l,r,val);
else if(l>mid) UpData(now<<|,l,r,val);
else{
UpData(now<<,l,mid,val);
UpData(now<<|,mid+,r,val);
} tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
}
}
int main(){
scanf("%d",&t);
for(int i=;i<=t;i++){
scanf("%d",&n);
Built(,,n);
scanf("%d",&m);
while(m--){
scanf("%d%d%d",&x1,&x2,&x3);
UpData(,x1,x2,x3);
}
printf("Case %d: The total value of the hook is %d.\n",i,tre[].sum);
}
return ;
}
1082 线段树练习 3
时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master
给你N个数,有两种操作:
1:给区间[a,b]的所有数增加X
2:询问区间[a,b]的数的和。
第一行一个正整数n,接下来n行n个整数,
再接下来一个正整数Q,每行表示操作的个数,
如果第一个数是1,后接3个正整数,
表示在区间[a,b]内每个数增加X,如果是2,
表示操作2询问区间[a,b]的和是多少。
pascal选手请不要使用readln读入
对于每个询问输出一行一个答案
3
1
2
3
2
1 2 3 2
2 2 3
9
数据范围
1<=n<=200000
1<=q<=200000
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,type;
struct node{
int l,r;long long sum,flag;
}tre[];
long long read(){
long long x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void build(int now,int l,int r){
tre[now].l=l;tre[now].r=r;
if(l==r){ tre[now].sum=read();return; }
int mid=(l+r)>>;
build(now<<,l,mid);
build(now<<|,mid+,r);
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
}
void tree_flag(int now){
if(tre[now].r==tre[now].l) return;
tre[now<<].flag+=tre[now].flag;
tre[now<<|].flag+=tre[now].flag;
tre[now<<].sum+=tre[now].flag*(tre[now<<].r-tre[now<<].l+);
tre[now<<|].sum+=tre[now].flag*(tre[now<<|].r-tre[now<<|].l+);
tre[now].flag=;
}
void UpData(int now,int l,int r,int k){
if(tre[now].l==l&&tre[now].r==r){
tre[now].sum+=(r-l+)*k;
tre[now].flag+=k;
return;
}
if(tre[now].flag) tree_flag(now);
int mid=(tre[now].l+tre[now].r)>>;
if(l>mid) UpData(now<<|,l,r,k);
else if(r<=mid) UpData(now<<,l,r,k);
else{
UpData(now<<,l,mid,k);
UpData(now<<|,mid+,r,k);
}
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
}
long long int Query(int now,int l,int r){
if(tre[now].l==l&&tre[now].r==r){
return tre[now].sum;
}
if(tre[now].flag)tree_flag(now);
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
int mid=(tre[now].l+tre[now].r)>>;
if(l>mid)return Query(now<<|,l,r);
else if(r<=mid)return Query(now<<,l,r);
else{
return Query(now<<,l,mid)
+Query(now<<|,mid+,r);
}
}
int main(){
n=read();
build(,,(int)n);
m=read();
long long int l,r,x;
for(int i=;i<=m;i++){
type=read();
if(type==){
l=read(),r=read(),x=read();
UpData(,(int)l,(int)r,(int)x);
}
else{
l=read(),r=read();
printf("%lld\n",Query(,(int)l,(int)r));
}
}
return ;
}
最新文章
- AJAX实现跨域的三种种方法(代理,JSONP,XHR2)
- CLR via C#(08)-操作符
- Nginx 配置文件详解
- Swift的一些基础内容
- 华为上机:求2的N次幂的值
- 基于jQuery仿uploadify的HTML5图片上传控件jquery.html5uploader
- RMAN数据库恢复之控制文件和参数文件恢复
- NSURLSessionConfiguration的简单实用
- Hibernate 一对一关联映射
- css3盒模型学习--利用box自适应布局
- 如何将一个二进制的xxx.bin文件轻松转为C语言数组
- Apache SkyWalking 为.NET Core带来开箱即用的分布式追踪和应用性能监控
- 【BZOJ5288】[HNOI2018]游戏(拓扑排序)
- PHP学习路径及练手项目合集
- jqgrid 对编辑行填写的内容做格式验证
- yolo源码解析(一)
- private static final long serialVersionUID = 1L;
- u3d 发布的程序 窗口位置的改变
- css之颜色篇
- yum安装lnmp