Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 3636    Accepted Submission(s): 1612

Problem Description
In a galaxy far, far away, there are two integer sequence a and b of length n.
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⌋
 
Input
There are multiple test cases, please read till the end of input file.
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.
 
Output
Output the answer for each 'query', each one line.
 
Sample Input
5 12
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
 
Sample Output
1
1
2
4
4
6
 
 
 
题目大意:b[1-n] 是 1-n  ,a[1-n] 是 0,add区间每一数加1,query区间a[i]/b[i]的和
 
 
思路:维护区间最小值,add操作为b[l]~b[r] 减一,最小值为0说明某个位置a[i]/b[i]=1, 答案贡献1,最后求区间和即可
 
 
 #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)(线段树)的更多相关文章

  1. 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/ ...

  2. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  3. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  4. 2018 Multi-University Training Contest 2 杭电多校第二场

    开始逐渐习惯被多校虐orz  菜是原罪 1004  Game    (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...

  5. 2019年牛客多校第二场 H题Second Large Rectangle

    题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...

  6. 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)

    题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...

  7. HDU 4612 (13年多校第二场1002)无向图缩点,有重边

    这道题是多校的题,比赛的时候是一道纷纷水过的板刷题. 题意:给你一些无向边,只加一条边,使该图的桥最少,然后输出最少的桥. 思路:当时大致想到思路了,就是缩点之后找出最长的链,然后用总的桥数减去链上的 ...

  8. 2019牛客多校第二场H-Second Large Rectangle

    Second Large Rectangle 题目传送门 解题思路 先求出每个点上的高,再利用单调栈分别求出每个点左右两边第一个高小于自己的位置,从而而得出最后一个大于等于自己的位置,进而求出自己的位 ...

  9. 第二大矩阵面积--(stack)牛客多校第二场-- Second Large Rectangle

    题意: 给你一幅图,问你第二大矩形面积是多少. 思路: 直接一行行跑stack求最大矩阵面积的经典算法,不断更新第二大矩形面积,注意第二大矩形可能在第一大矩形里面. #define IOS ios_b ...

随机推荐

  1. Python No module named pkg_resources

    好记性不如烂笔头. I encountered the same ImportError today while trying to use pip. Somehow the setuptools p ...

  2. 【新手向】Centos系统文件权限的系统阐述与演示

    在linux服务器日常管理中,我们会经常管理查看文件或者文件夹的权限内容以保证服务的正常运行.今天就和大家聊聊文件权限的那些事. 查看文件的权限情况可以用 ll 命令例: ll -d /kid #查看 ...

  3. Go并发原理

    Go语言是为并发而生的语言,Go语言是为数不多的在语言层面实现并发的语言:也正是Go语言的并发特性,吸引了全球无数的开发者. 并发(concurrency)和并行(parallellism) 并发(c ...

  4. eclipse格式化代码模板

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...

  5. c类库,委托,var ,运算符 is 和 as 。

    类库(Class Library)  格式   .dll  文件 类库   就是类的仓库 c#代码被编译过以后的文件,不可阅读,不可修改,只能调用. 类库是一个综合性的面向对象的可重用类型集合,这些类 ...

  6. QGraphicsScene绘制网格背景

    博客转载自:https://blog.csdn.net/u010177010/article/details/51496038 //两条轴线QPolygonF myPolygon1; myPolygo ...

  7. com.fasterxml.jackson.databind.JavaType.isReferenceType

    <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-map ...

  8. [译]Javascript中的do-while循环

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  9. 罗技K380连接Win10(MacBookPro双系统)系统失败

    问题描述: MacBook Pro 双系统,先连接MacOS使用没问题,切换至Win10系统,连接失败. 解决方案: 进入MacOS,打开蓝牙设置,将已经连接的键盘删除,重新进入Win10系统,再连接 ...

  10. HTML完全使用详解 PDF扫描版​

    <HTML完全使用详解>根据网页制作的实际特点和目前市场需要,全面系统地介绍了最新的HTML4.01.丰富的实例贯穿全书,能帮助您全面掌握HTML,而且本书所有实例均可直接修改使用,可以提 ...