HDU 1698 区间更新
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20532 Accepted Submission(s): 10284
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 <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 100005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n; struct node{
int l, r, val;
bool f;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].val=;
if(l==r){
return;
}
build(l,mid,ll);
build(mid+,r,rr);
} void update(int l,int r,int val,int root){
if(a[root].val==val) return;
if(a[root].l==l&&a[root].r==r){
a[root].val=val;
return;
}
if(a[root].val){
if(a[root].l!=a[root].r) {
a[ll].val=a[rr].val=a[root].val;
}
}
if(l>=a[rr].l) update(l,r,val,rr);
else if(r<=a[ll].r) update(l,r,val,ll);
else {
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
if(a[ll].val==a[rr].val) a[root].val=a[ll].val;
else a[root].val=;
} int get_sum(int root){
if(a[root].val) return a[root].val*(a[root].r-a[root].l+);
return get_sum(ll)+get_sum(rr);
} main()
{
int q, i, j, k, x, y, z;
int t;
cin>>t;
for(k=;k<=t;k++){
scanf("%d %d",&n,&q);
build(,n,);
while(q--){
scanf("%d %d %d",&x,&y,&z);
update(x,y,z,);
}
printf("Case %d: The total value of the hook is %d.\n",k,get_sum()); }
}
最新文章
- printf 的场宽
- crawler4j源码学习(2):Ziroom租房网房源信息采集爬虫
- javascript笔记---貌似大叔
- 一个基本jquery的评论留言模块
- Android njava.net.UnknownHostException: Unable to resolve host
- 深入了解JavaScript中的for循环
- [每日一题] 11gOCP 1z0-052 :2013-09-3 Because of frequent checkpoints...........................A30
- wordpress函数技巧
- 搭建solr单机版
- Struts2基础学习(八)&mdash;Struts2防止表单重复提交
- 一周中的后两天 笔记(网路基础 Python基础)
- 个人总结-Alpha阶段
- 翻译连载 | 第 10 章:异步的函数式(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
- 点击table中的某一个td,获得这个tr的所有数据
- Flink中的Time
- mybatis 一对一关联 association 返回空值
- 如何在Anaconda中把python环境更新更高版本
- 012 HDFS API 文件读写代码演示
- awesomes前端资源库网站
- Echarts 修改字体样色 X、Y轴