魔兽世界---屠夫(Just a Hook)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23450 Accepted Submission(s): 11742
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.
- #include<stdio.h>
- const int MAXN=;
- struct Node{
- int l,r;
- int sum,lazy,val;//val不能全局变量。。。
- };
- Node tree[MAXN<<];
- #define NOW tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum
- #define lson root<<1,tree[root].l,mid
- #define rson root<<1|1,mid+1,tree[root].r
- void build(int root,int l,int r){
- tree[root].l=l;
- tree[root].r=r;
- tree[root].lazy=;
- tree[root].val=;
- if(l==r)tree[root].sum=;
- else{
- int mid=(l+r)>>;
- build(lson);
- build(rson);
- NOW;
- }
- }
- void update(int root,int l,int r,int v){
- if(l==tree[root].l&&r==tree[root].r){
- tree[root].lazy=;
- tree[root].val=v;
- tree[root].sum=(r-l+)*v;
- }
- else{
- int mid=(tree[root].l+tree[root].r)>>;
- if(tree[root].lazy==){
- tree[root].lazy=;
- update(lson,tree[root].val);
- update(rson,tree[root].val);
- tree[root].val=;
- }
- if(r<=mid)update(root<<,l,r,v);
- else if(l>mid)update(root<<|,l,r,v);
- else{//这个不能少了,代表l,r在tree的两个孩子节点内,也就是找到了l,r的区间;
- update(root<<,l,mid,v);
- update(root<<|,mid+,r,v);
- }
- NOW;
- }
- }
- int main(){
- int T,N,Q,flot=;
- scanf("%d",&T);
- while(T--){
- scanf("%d",&N);
- scanf("%d",&Q);
- build(,,N);
- while(Q--){
- int x,y,z;
- scanf("%d%d%d",&x,&y,&z);
- update(,x,y,z);
- }
- printf("Case %d: The total value of the hook is %d.\n",++flot,tree[].sum);
- }
- return ;
- }
过了一段时间又写了一遍,增强了自己的理解:
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<vector>
- #include<algorithm>
- #include<set>
- using namespace std;
- #define mem(x,y) memset(x,y,sizeof(x))
- const int MAXN=100010;
- struct Node{
- int lazy,v;
- };
- Node tree[MAXN<<2];
- #define ll root<<1
- #define rr root<<1|1
- #define lson ll,l,mid
- #define rson rr,mid+1,r
- #define LAZY(x) tree[x].lazy
- #define V(x) tree[x].v
- int ans;
- void pushdown(int root,int x){
- if(LAZY(root)){
- LAZY(ll)=LAZY(root);
- LAZY(rr)=LAZY(root);
- V(ll)=LAZY(root)*(x-(x>>1));//这里错了半天。。。。。右节点有时候要多一。。。
- V(rr)=LAZY(root)*(x>>1);
- LAZY(root)=0;
- }
- }
- void pushup(int root){
- V(root)=V(ll)+V(rr);
- }
- void build(int root,int l,int r){
- LAZY(root)=0;
- int mid=(l+r)>>1;
- if(l==r){
- V(root)=1;
- return;
- }
- build(lson);
- build(rson);
- pushup(root);
- }
- void update(int root,int l,int r,int L,int R,int v){
- if(l>=L&&r<=R){
- LAZY(root)=v;
- V(root)=(r-l+1)*v;
- return;
- }
- pushdown(root,r-l+1);
- int mid=(l+r)>>1;
- /*if(mid>=R)update(lson,L,R,v);
- else if(mid<L)update(rson,L,R,v);
- else{
- update(lson,L,mid,v);
- update(rson,mid+1,R,v);
- }*///这样写也可以。。。
- if(mid>=L)update(lson,L,R,v);
- if(mid<R)update(rson,L,R,v);
- pushup(root);
- }
- void query(int root,int l,int r,int L,int R){
- int mid=(l+r)>>1;
- if(l>=L&&r<=R){
- ans+=V(root);
- return;
- }
- pushdown(root,r-l+1);
- if(mid>=L)query(lson,L,R);
- if(mid<R)query(rson,L,R);
- }
- int main(){
- int T,N,kase=0;
- scanf("%d",&T);
- while(T--){
- scanf("%d",&N);
- build(1,1,N);
- int q,a,b,c;
- scanf("%d",&q);
- while(q--){
- scanf("%d%d%d",&a,&b,&c);
- update(1,1,N,a,b,c);
- }
- ans=0;
- query(1,1,N,1,N);//也可以不询问直接输出tree[1].v
- printf("Case %d: The total value of the hook is %d.\n",++kase,ans);
- }
- return 0;
- }
map超时;
代码:
- #include<iostream>
- #include<cstdio>
- #include<map>
- #include<algorithm>
- using namespace std;
- map<int,int>mp;
- map<int,int>::iterator it1;
- const int MAXN=100010;
- struct Node{
- int s,e,v;
- };
- Node dt[MAXN];
- int main(){
- int T,N,q,kase=0;
- scanf("%d",&T);
- while(T--){
- scanf("%d",&N);
- for(int i=1;i<=N;i++)mp[i]=1;
- scanf("%d",&q);
- for(int i=0;i<q;i++)scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].v);
- for(int i=0;i<q;i++){
- for(it1=mp.lower_bound(dt[i].s);it1!=mp.end();){
- if(it1->first<=dt[i].e)it1->second=dt[i].v,it1++;
- else break;
- }
- }
- int ans=0;
- for(int i=1;i<=N;i++)ans+=mp[i];
- printf("Case %d: The total value of the hook is %d.\n",++kase,ans);
- }
- return 0;
- }
过了一段时间又写了遍,本想着一遍a的,但是错了几个小时,实在找不出,问了群里面的大神,原来我是x-(x>>1)没有加括号,这就是代码风格的问题了,我的代码风格存在很大的问题,以至于错误了很难找出来,大神指出了几点问题:
1:关于define 的括号问题;
2:关于字母与运算符的缩进问题;
3:关于宏定义的名称问题;都存在一定问题;以后要注意了,参照谷歌的吧
另一种线段树写法:
- extern "C++"{
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- typedef long long LL;
- typedef unsigned u;
- typedef unsigned long long ull;
- #define mem(x,y) memset(x,y,sizeof(x))
- void SI(double &x){scanf("%lf",&x);}
- void SI(int &x){scanf("%d",&x);}
- void SI(LL &x){scanf("%lld",&x);}
- void SI(u &x){scanf("%u",&x);}
- void SI(ull &x){scanf("%llu",&x);}
- void SI(char *s){scanf("%s",s);}
- void PI(int x){printf("%d",x);}
- void PI(double x){printf("%lf",x);}
- void PI(LL x){printf("%lld",x);}
- void PI(u x){printf("%u",x);}
- void PI(ull x){printf("%llu",x);}
- void PI(char *s){printf("%s",s);}
- #define NL puts("");
- #define ll root<<1
- #define rr root<<1|1
- #define lson ll,l,mid
- #define rson rr,mid+1,r
- const int INF=0x3f3f3f3f;
- const int MAXN=;
- int tree[MAXN<<];
- int lazy[MAXN<<];
- }
- /*
- void pushup(int root){
- tree[root]=tree[ll]+tree[rr];
- }
- void pushdown(int root,int x){
- if(lazy[root]){
- lazy[ll]=lazy[root];
- lazy[rr]=lazy[root];
- // tree[ll]=lazy[root]*(mid-l+1);
- // tree[rr]=lazy[root]*(r-mid);
- tree[ll]=lazy[root]*(x-(x>>1));
- tree[rr]=lazy[root]*(x>>1);
- lazy[root]=0;
- }
- }
- void build(int root,int l,int r){
- int mid=(l+r)>>1;
- lazy[root]=0;
- if(l==r){
- tree[root]=1;return ;
- }
- build(lson);
- build(rson);
- pushup(root);
- }
- void update(int root,int l,int r,int L,int R,int C){
- if(l>=L&&r<=R){
- tree[root]=(r-l+1)*C;
- lazy[root]=C;
- return ;
- }
- int mid=(l+r)>>1;
- pushdown(root,r-l+1);
- if(mid>=L)update(lson,L,R,C);
- if(mid<R)update(rson,L,R,C);
- pushup(root);
- }
- */
- void pushdown(int root){
- lazy[ll] = lazy[rr] = lazy[root];
- lazy[root] = ;
- }
- void update(int root,int l,int r,int L,int R,int C){
- if(l >= L && r <= R){
- lazy[root] = C;
- return;
- }
- if(lazy[root])
- pushdown(root);
- int mid = (l + r) >> ;
- if(mid >= L)
- update(lson,L,R,C);
- if(mid < R)
- update(rson,L,R,C);
- }
- int sum(int root,int l,int r){
- int mid = (l + r) >> ;
- if(lazy[root])
- return (r - l + ) * lazy[root];
- return sum(lson) + sum(rson);
- }
- int main(){
- //assert(false);
- int T,kase=;
- int N,M;
- SI(T);
- while(T--){
- SI(N);
- lazy[]=;
- SI(M);
- int a,b,c;
- while(M--){
- scanf("%d%d%d",&a,&b,&c);
- update(,,N,a,b,c);
- }
- printf("Case %d: The total value of the hook is %d.\n",++kase,sum(,,N));
- }
- return ;
- }
java超时了。。。
注意:左右查找都是mid判断的。。。注意lazy要把左右支lazy
代码:
- import java.util.Scanner;
- public class hdoj1698{
- public static void main(String[] argv){
- Scanner cin = new Scanner(System.in);
- SegmentTree atree = new SegmentTree(100010 << 2);
- int T, N, q;
- T = cin.nextInt();
- int kase = 0;
- while(T-- > 0){
- N = cin.nextInt();
- atree.build(1, 1, N);
- q = cin.nextInt();
- while(q-- > 0){
- int a, b, c;
- a = cin.nextInt();
- b = cin.nextInt();
- c = cin.nextInt();
- atree.update(1, 1, N, a, b, c);
- }
- // System.out.println(atree.tree[1]);
- System.out.println("Case " + (++kase) +
- ": The total value of the hook is " +
- atree.query(1, 1, N, 1, N) + ".");
- }
- }
- }
- class SegmentTree{
- private int[] tree;
- private int[] lazy;
- public SegmentTree(int size) {
- tree = new int[size];
- lazy = new int[size];
- }
- private void pushup(int root){
- tree[root] = tree[root << 1] + tree[root<<1 | 1];
- }
- private void pushdown(int root, int x){
- if(lazy[root] != 0){
- lazy[root << 1] = lazy[root << 1|1] = lazy[root];
- tree[root << 1] = (x - (x >> 1))*lazy[root];
- tree[root << 1|1] = (x >> 1)*lazy[root];
- lazy[root] = 0;
- }
- }
- public void build(int root, int l, int r){
- lazy[root] = 0;
- if(l == r){
- tree[root] = 1;
- return ;
- }
- int mid = (l + r) >> 1;
- build(root << 1, l, mid);
- build(root << 1|1, mid + 1, r);
- pushup(root);
- }
- public void update(int root, int l, int r, int L, int R, int v){
- if(l >= L && r <= R){
- tree[root] = (r - l + 1)*v;
- lazy[root] = v;
- return;
- }
- pushdown(root, r - l + 1);
- int mid = (r + l) >> 1;
- if(mid >= L){
- update(root << 1, l, mid, L, R, v);
- }
- if(mid < R){
- update(root << 1|1, mid + 1, r, L, R, v);
- //日了狗了。。。。。。。。。。
- }
- pushup(root);
- }
- public int query(int root, int l, int r, int L, int R){
- if(l >= L && r <= R){
- return tree[root];
- }
- pushdown(root, r - l + 1);
- int sum = 0;
- int mid = (l + r) >> 1;
- if(mid >= L){
- sum += query(root << 1, l, mid, L, R);
- }
- if(mid < R){
- sum += query(root << 1|1, mid +1, r, L, R);
- }
- return sum;
- }
- }
魔兽世界---屠夫(Just a Hook)的更多相关文章
- HDU-1698 JUST A HOOK 线段树
最近刚学线段树,做了些经典题目来练手 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 1698 Just a Hook(线段树
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- svnserver hook python
在使用中可能会遇到的错误排除 :1.Error: svn: 解析"D:\www\test"出错,或svn: E020024: Error resolving case of 'D: ...
- Android Hook技术
原文:http://blog.csdn.net/u011068702/article/details/53208825 附:Android Hook 全面入侵监听器 第一步.先爆项目demo照片,代码 ...
- Frida HOOK微信实现骰子作弊
由于微信摇骰子的功能在本地进行随机后在发送,所以存在可以hook掉判断骰子数的方法进行修改作弊. 1.frida实现hook java层函数1)写个用来测试的demo,当我们点击按钮的时候会弹出窗口显 ...
- java的关闭钩子(Shutdown Hook)
Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jv ...
- IDT HOOK思路整理
IDT(中断描述符表)分为IRQ(真正的硬件中断)和软件中断(又叫异常). HOOK的思路为,替换键盘中断处理的函数地址为自己的函数地址.这样在键盘驱动和过滤驱动之前就可以截获键盘输入. 思路确定之后 ...
- Android Hook 借助Xposed
主要就是使用到了Xposed中的两个比较重要的方法,handleLoadPackage获取包加载时候的回调并拿到其对应的classLoader:findAndHookMethod对指定类的方法进行Ho ...
- iOS App 无代码入侵的方法hook
继续Objective-C runtime的研究 最近公司项目在做用户行为分析 于是App端在某些页面切换,交互操作的时候需要给统计系统发送一条消息 在几十个Controller 的项目里,一个一个地 ...
随机推荐
- linux网卡掉包或挂掉解决办法
最近自己公司网站老出现掉包问题之前以为是网络问题或机房问题,经过N久的排查发现是linux网卡掉包了,下面我来分享我的解决办法. 之前公司的系统由于网卡问题,经常出现掉包(掉包排除攻击的 因素)或 ...
- Activitys, Threads, & Memory Leaks
Activitys, Threads, & Memory Leaks 在Android编程中,一个公认的难题是在Activity的生命周期如何协调长期运行的任务和避免有可能出现的内存泄漏问题. ...
- Android stagefright与opencore对比
[转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...
- 把给定的字符串解析为Date对象
把给定的字符串解析为Date对象: /** * <pre> * 把给定的字符串解析为Date对象 * </pre> * * @param str 要进行解析的字符串 * @pa ...
- Hibernate、乐观锁和悲观锁
悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的关系型数据 ...
- SQLServer,仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表xx中的标识列指定显式值
情景: 如果此表的主键或者其中有一个列使用了 IDENTITY(1,1) 自增长时,但又想手动为此列指定值时,当用如下解决方案: set identity_insert 表名 ON 使用此命令把表的 ...
- #include <strstream>
std::ostrstream MYOUT(str, sizeof(str)); 拼接字符串 #include <iostream> #include <strstream> ...
- 如何在ASP.NET中用C#将XML转换成JSON 【转】
本文旨在介绍如果通过C#将获取到的XML文档转换成对应的JSON格式字符串,然后将其输出到页面前端,以供JavaScript代码解析使用.或许你可以直接利用JavaScript代码通过Ajax的方 ...
- Robotium双client測试框架
互联网的本质就是信息交换.移动互联网更是如此, 所以很多移动互联网的服务类应用中有着身份地位不同的两种用户(比如:交易中的买家和卖家, 教学中的老师和学生, 打车中的车主和乘客).近期的工作是给公司的 ...
- Exception Handling in ASP.NET Web API
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...