Just a Hook(区间set)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29106 Accepted Submission(s): 14396
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<bits/stdc++.h>
#define N 100005
#define lson i*2,l,m
#define rson i*2+1,m+1,r
#define ll long long
using namespace std;
long long sum[N*];
long long setv[N*];
void pushup(int i)
{
sum[i]=sum[i*]+sum[i*+];
} void pushdown(int i,int num)
{
if(setv[i])
{
sum[i*]=setv[i]*(num-num/);
sum[i*+]=setv[i]*(num/);
setv[i*]=setv[i];
setv[i*+]=setv[i];
setv[i]=;
}
} void build(int i,int l,int r)
{
setv[i]=;
sum[i]=;
if(l==r)
return ;
int m=(l+r)/;
build(lson);
build(rson);
pushup(i);
} void update(int ql,int qr,int val,int i,int l,int r)
{
if(ql<=l&&r<=qr)
{
setv[i]=val;
sum[i]=val*(r-l+);
return ;
}
pushdown(i,r-l+);
int m=(l+r)/;
if(m>=ql) update(ql,qr,val,lson);
if(m<qr) update(ql,qr,val,rson);
pushup(i);
} ll query(int ql,int qr,int i,int l,int r)
{
//cout<<"l="<<l<<" r="<<r<<endl;
if(ql<=l&&r<=qr)
{
return sum[i];
}
pushdown(i,r-l+);
int m=(l+r)/;
ll cur=;
if(m>=ql) cur+=query(ql,qr,lson);
if(m<qr) cur+=query(ql,qr,rson);
//cout<<cur<<endl;
return cur;
} int t,n,q;
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
scanf("%d",&t);
for(int Case=;Case<=t;Case++)
{
scanf("%d",&n);
build(,,n);
//for(int i=1;i<18;i++)
// cout<<sum[i]<<" ";
//cout<<endl;
int a,b,c;
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
//cout<<a<<" "<<b<<" "<<c<<endl;
update(a,b,c,,,n);
}
printf("Case %d: The total value of the hook is %lld.\n",Case,query(,n,,,n));
}
return ;
}
Just a Hook(区间set)的更多相关文章
- HDU1698 Just a Hook (区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook 区间更新 lazy标记
lazy标记 #include <iostream> #include <cstdio> #include <cstring> #include <sstre ...
- 线段树入门&lazy思想
线段树将区间分成若干个子区间,子区间又继续分,直到区间为一个点(区间左值等于右值) 对于父区间[a,b],其子区间为[a,(a+b)/2]和[(a+b)/2+1,b] 用于求区间的值,如区间最值.区间 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1698 Just a Hook(线段树/区间更新)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description In the g ...
- HDU1698Just a Hook(线段树 + 区间修改 + 求和)
题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...
- hdu 1698:Just a Hook(线段树,区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1698 Just a Hook (线段树区间更新)
题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...
- HDU 1698 (线段树 区间更新) Just a Hook
有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...
随机推荐
- sqoop使用的问题
找不到表 17/05/02 18:15:47 ERROR tool.ImportTool: Imported Failed: There is no column found in the targe ...
- poj2528 线段树+离散化 (倒序)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- apache一个ip多个端口虚拟主机
1.打开httpd.conf,查找Listen:80,在下面一行加入Listen:8080:2.查找#Include conf/extra/httpd-vhosts.conf,将此行前面的#去掉:3. ...
- Erlang游戏服设计总结
这主要是一年多来,个人从事Erlang游戏服开发中对一些事情的思考. 想到哪说到哪,没有条理可言. 欢迎讨论. 通常Erlang游戏服务的设计涉及到的东东包括如下: 任务系统 活动系统 公会系统 玩法 ...
- 一个强迫症的Git 选择
选择 1,经常性的commit or 干净的历史 在本地(私有)的开发分支中,选择经常性的commit,以便于实时记录修改,回退等操作.eg.develop,feature... 实现方式: comm ...
- zoj 1938 Binomial Showdown 组合数裸基础
Binomial Showdown Time Limit: 2 Seconds Memory Limit: 65536 KB In how many ways can you choose ...
- 数据库中删除语句Drop、Delete、Truncate的相同点和不同点的比较
数据库删除语句的分别介绍: Delete:用于删除表中的行(注:可以删除某一行:也可以在不删除表的情况下(即意味着表的结构.属性.索引完整)删除所有行) 语法:删除某一行:Delete From 表名 ...
- Java面向对象 网络编程 上
Java面向对象 网络编程 上 知识概要: (1)网络模型 (2)网络通讯要素 (3)UDP TCP 概念 (4)Socket (5)UDP TCP 传输 ...
- Window2008 R2(64位)使用codesmith连接Sqlite
①打开C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config目录,找到machine.config文件新增 <add name=" ...
- MySQL5.7以上Zip版官方安装文档(选译)
前言 在windows上安装Zip版MySQL(选译) 学习mysql的朋友们会发现5.7+版本的mysql变得比以前难安装了许多(当然我们可以选择installer版本,但是这样总感觉对学习mysq ...