HDU6315 Naive Operations(多校第二场1007)(线段树)
Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 3636 Accepted Submission(s): 1612
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
1
2
4
4
6
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define FO freopen("in.txt", "r", stdin);
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int N=;
int b[N];
int sum[N<<],sub[N<<],lazy[N<<];//区间和 区间最小值 lazy标记 void Pushup(int rt) {//上传
sum[rt]=sum[rt<<]+sum[rt<<|];//左右孩子之和
sub[rt]=min(sub[rt<<],sub[rt<<|]);//左右孩子的最小值
} void Pushdown(int rt) {//下压
lazy[rt<<]+=lazy[rt];//传标记
lazy[rt<<|]+=lazy[rt];
sub[rt<<]-=lazy[rt];//更新 因为是每次减一 所以就直接减lazy
sub[rt<<|]-=lazy[rt];
lazy[rt]=;//清除父节点标记
} void build(int rt,int L,int R) {
sum[rt]=;//rt的初始状态
lazy[rt]=;
if(L==R) {
scanf("%d",&b[L]);//建树的一种方式
sub[rt]=b[L];
return;
}
int mid=(L+R)>>;//递归建树
build(rt<<,L,mid);
build(rt<<|,mid+,R);
Pushup(rt);//因为不涉及修改,不需要下压,只需上传
} void dfs(int rt,int L,int R) {
if(L==R) {
sum[rt]++;
sub[rt]=b[L];
return;
}
int mid=(L+R)>>;
Pushdown(rt);
if(!sub[rt<<]) dfs(rt<<,L,mid);
if(!sub[rt<<|]) dfs(rt<<|,mid+,R);
Pushup(rt);
} void Updata(int rt,int L,int R,int l,int r) {//L,R为时时变动的区间
if(L>=l&&R<=r) {//如果当前节点在待查询节点内
lazy[rt]++;
sub[rt]--;
if(!sub[rt]) dfs(rt,L,R);//如果区间最小值为0,递归搜索位置
return;
}
int mid=(L+R)>>;//递归找l,r的涉及区间
Pushdown(rt);//需要走左右孩子,先下压
if(l<=mid) Updata(rt<<,L,mid,l,r);
if(r>mid) Updata(rt<<|,mid+,R,l,r);
Pushup(rt);//走完之后,状态上传给父亲节点
} int Query(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) //待查询区间内
return sum[rt];
int mid=(L+R)>>;
Pushdown(rt);//走左右孩子
int ans=;
if(l<=mid) ans+=Query(rt<<,L,mid,l,r);
if(r>mid) ans+=Query(rt<<|,mid+,R,l,r);
Pushup(rt);//走完,状态上传给父亲节点
return ans;
} int main() {
int n,q;
while(~scanf("%d%d",&n,&q)) {
build(,,n);
char s[];
int a,b;
while(q--) {
scanf("%s%d%d",s,&a,&b);
if(s[]=='a') Updata(,,n,a,b);
else printf("%d\n",Query(,,n,a,b));
}
}
}
上面用了dfs搜索最小值为0的位置,也可以一直去找。
void Update(int L,int R,int l,int r,int rt)
{
if(a[rt]>&&L<=l&&r<=R)
{
lazy[rt]++;
a[rt]--;
return;
}
if(a[rt]==&&l==r)
{
sum[rt]++;
lazy[rt]=;
a[rt]=b[l];
return;
}
int mid=(l+r)>>;
PushDown(rt);
if(L<=mid)Update(L,R,l,mid,rt<<);
if(R>mid)Update(L,R,mid+,r,rt<<|);
PushUp(rt);
}
!!!!这里的变量含义和第一个代码不一样。自行理解。
HDU6315 Naive Operations(多校第二场1007)(线段树)的更多相关文章
- HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)
原题地址 Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/ ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 2018 Multi-University Training Contest 2 杭电多校第二场
开始逐渐习惯被多校虐orz 菜是原罪 1004 Game (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...
- 2019年牛客多校第二场 H题Second Large Rectangle
题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...
- 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)
题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...
- HDU 4612 (13年多校第二场1002)无向图缩点,有重边
这道题是多校的题,比赛的时候是一道纷纷水过的板刷题. 题意:给你一些无向边,只加一条边,使该图的桥最少,然后输出最少的桥. 思路:当时大致想到思路了,就是缩点之后找出最长的链,然后用总的桥数减去链上的 ...
- 2019牛客多校第二场H-Second Large Rectangle
Second Large Rectangle 题目传送门 解题思路 先求出每个点上的高,再利用单调栈分别求出每个点左右两边第一个高小于自己的位置,从而而得出最后一个大于等于自己的位置,进而求出自己的位 ...
- 第二大矩阵面积--(stack)牛客多校第二场-- Second Large Rectangle
题意: 给你一幅图,问你第二大矩形面积是多少. 思路: 直接一行行跑stack求最大矩阵面积的经典算法,不断更新第二大矩形面积,注意第二大矩形可能在第一大矩形里面. #define IOS ios_b ...
随机推荐
- 2015.1.15 利用航线id取所有点的函数创建视图
1.根据航路id取所有航路点的函数 create or replace function alinepnts(alid in number) return tab_airline_pnt is --返 ...
- mongodb用mongoose得到的对象不能增加属性解决
一,先定义了一个goods(商品)的models var mongoose = require('mongoose'); var Schema = mongoose.Schema; var produ ...
- object类型对象 ref参数如何理解?
class Program { static void Main(string[] args) { Student stu = new Student { Name = "老王" ...
- Javascript 面向对象(一):封装
Javascript 面向对象编程(一):封装 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言, ...
- spring整合mybatis的事物管理配置
一.基本配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- android opencv
最近工作需求:用opencv来先做一个demo.扫描照片进行边缘检测和透视矫正. 之后会加入照片降噪等处理. 请教了一下搞图像的同事.他的提议: 1.绿盟的“黄色照片检测” 用的是动态的opencv库 ...
- for xml path 按分类合并行数据
) as itemnum FROM ( SELECT Sonum, (SELECT ItemNum+',' FROM testtb WHERE Sonum=A.Sonum FOR XML ...
- 关于web.xml中的<welcome-file-list>中的默认首页文件
先看我的配置文件: <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome ...
- day70 12-存储过程和存储函数
什么是相关子查询? 这是一个子查询,子查询本身又是一个多表查询.where不能用组函数,但是可以用字符函数instr().除了order by排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...
- [Elasticsearch2.x] 多字段搜索 (一) - 多个及单个查询字符串 <译>
多字段搜索(Multifield Search) 本文翻译自官方指南的Multifield Search一章. 查询很少是只拥有一个match查询子句的查询.我们经常需要对一个或者多个字段使用相同或者 ...