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. c#学习之路---壁咚漏洞搜索

    每次出漏洞都会用JAVA去写,不过JAVA你懂得,写GUI每次画图很吃力. 于是左右学习了下c#,期间也得到表哥storm7kb的帮助,要不然这个表格与数据绑定不知道c#怎么弄. 上一下图吧: --- ...

  2. java反射专题一

    一丶Class的理解 /* * Class类是反射的源头 * 创建一个类,通过编译(javac.exe),生成对应的.class文件,之后使用java.exe加载(JVM的类加载器完成的)此.clas ...

  3. leetcode643

    double findMaxAverage(vector<int>& nums, int k) { double max = INT_MIN; int len = nums.siz ...

  4. .net 4 安装未成功,无意中的解决办法!

    公司 电脑是chost的系统,由于使用时间过长,重装纯净版系统的话,代价太大,故网上寻求各种解决办法! 安装.net 4 总是失败,查看百度,各种: WIN7系统哈哈跟我的问题一样,我的刚才解决了:1 ...

  5. python去掉括号之间的字符

    在字符串中识别括号并删除括号及其中的内容括号包括 大中小 3种括号 输入为 1个字符串 s="我是一个人(中国人)[真的]{确定}"; 输出为 result = "我是一 ...

  6. LinkedHashMap和HashMap的区别

    一.问题描述: 前几天写webservices接口,需要同步人力资源,涉及到添加顺序:主账号需要添加在次账号之前,直接上级需要添加在下级之前.解析xml之后直接封装在HashMap中,导致取对象时顺序 ...

  7. sklearn.svm.SVC参数说明

    摘自:https://blog.csdn.net/szlcw1/article/details/52336824 本身这个函数也是基于libsvm实现的,所以在参数设置上有很多相似的地方.(PS: l ...

  8. 关于c#里的集合的,结构体,枚举的定义,解释与应用

    那么先写一下 集合 . 集合和数组很相似,数组里的类型是必须同一类型,固定长度.然而集合里的可以是不同类型,不固定长度的.所以集合运用的灵活度要更高一些. 要使用集合,必须先引用命名空间:using ...

  9. javascript使用setTimeout、setInterval时找不到变量的问题

    我们在某个作用域内或者在自己定义的一个类里调用setTimeout.setInterval会经常会遇到找不到某个变量的错误. 比如下面这个例子: window.onload = function(){ ...

  10. Windows中lib和DLL区别和使用

    本文转载自:http://www.cppblog.com/amazon/archive/2009/09/04/95318.html 共有两种库:一种是LIB包含了函数所在的DLL文件和文件中函数位置的 ...