Naive Operations

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

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
 

题意:初始时有一段长度为n的数组a为0,长度为n的数组b,给你数组b

  有操作add,把区间[l,r]内每一个ai+1,query,查询操作。 区间 a[i]/b[i]向下取整的和。

题解:

我们每次区间加一,变成把每个值减一,每次减到0的时候ai/bi的值就会+1,用cnt记录,再把值重新更新为bi,查询的时候查询+1 的总和。

用线段树保留最小值,当出现最小值为0的时候把cnt++,值更新为b[r],因为每次只会加+1所以总数不会太大

zzq的做法

考虑维护 的这样的最小的 ,每次 加一的时候 就减
一,一旦 变成 了那么就需要把 加一,这样两个线段树维护一下就行了。
注意到 由于 是排列是 的,那么复杂度就是 。

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9+;
const double eps = 1e-;
int b[*maxn];
int n,q;
int dat[*maxn];
int lazy[*maxn];
int res;
int cnt[maxn*];
void init(int l,int r,int k){
int chl=k<<|,chr=(k+)<<,mid=(l+r)/;
if(r-l==){
lazy[k]=cnt[k]=;
dat[k]=b[r];
return ;
}else{
lazy[k]=cnt[k]=;
init(l,mid,chl);
init(mid,r,chr);
dat[k]=min(dat[chl],dat[chr]);
}
}
int sum(int a,int c,int l,int r,int k){
int chl=k<<|,chr=(k+)<<,mid=(l+r)/;
if(c<=l||a>=r){
return ;
}else if(a<=l&&r<=c){
return cnt[k];
}else {
lazy[chl]+=lazy[k];
lazy[chr]+=lazy[k];
lazy[k]=;
dat[k]=min(dat[chl]+lazy[chl],dat[chr]+lazy[chr]);
return sum(a,c,l,mid,chl)+sum(a,c,mid,r,chr);
}
}
void updata(int a,int c,int l ,int r , int k){
int chl=k<<|,chr=(k+)<<,mid=(l+r)/;
if(c<=l||a>=r){
return;
}else if(a<=l&&r<=c){
if(lazy[k]+dat[k]-<=){
if(r-l==){
cnt[k]++;
dat[k]=b[r];
lazy[k]=;
return;
}
lazy[chl]+=lazy[k];
lazy[chr]+=lazy[k];
lazy[k]=;
updata(a,c,l,mid,chl);
updata(a,c,mid,r,chr);
if(r-l!=){
cnt[k]=cnt[chl]+cnt[chr];
dat[k]=min(dat[chl]+lazy[chl],dat[chr]+lazy[chr]);
}
return;
}
lazy[k]--;
}else{
lazy[chl]+=lazy[k];
lazy[chr]+=lazy[k];
updata(a,c,l,mid,chl);
updata(a,c,mid,r,chr);
lazy[k]=;
dat[k]=min(dat[chl]+lazy[chl],dat[chr]+lazy[chr]);
if(r-l!=) cnt[k]=cnt[chl]+cnt[chr];
}
}
char ch[];
int l,r;
int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
while(scanf("%d%d",&n,&q) !=EOF){
for(int i=;i<=n;i++){
scanf("%d",&b[i]);
}
init(,n,);
while(q--){
scanf("%s %d %d",ch,&l,&r);
if(ch[]=='a'){
updata(l-,r,,n,);
}else{
printf("%d\n",sum(l-,r,,n,));
}
}
}
return ;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define L long long
using namespace std;
const int q=;
int n,m,t,c[][],f[][],x[],a[],b[],p;
int main()
{
int i,j,k,l;
for(i=;i<=;i++)
{
c[i][]=;
for(j=;j<=i;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%q;
}
x[]=;
for(i=;i<=;i++)
x[i]=(x[i-]<<)%q;
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%d%d",&i,&j);
a[i]=;
for(k=i+;k<=n;k++)
{
a[k]=;
for(l=i;l<k;l++)
a[k]=(a[k]-(L)a[l]*c[k][l])%q;
}
b[j]=;
for(k=j+;k<=m;k++)
{
b[k]=;
for(l=j;l<k;l++)
b[k]=(b[k]-(L)b[l]*c[k][l])%q;
}
for(k=i;k<=n;k++)
for(l=j;l<=m;l++)
f[k][l]=(L)c[n][k]*c[m][l]%q*x[(n-k)*(m-l)]%q;
p=;
for(k=i;k<=n;k++)
for(l=j;l<=m;l++)
p=(p+(L)f[k][l]*a[k]%q*b[l])%q;
p=(p+q)%q;
printf("%d\n",p);
}
return ;
}

最新文章

  1. 过段时间逐步使用HTML5新增的web worker等内容
  2. 【转】关于启用 HTTPS 的一些经验分享
  3. 【GOF23设计模式】外观模式
  4. JavaScript Patterns 4.5 Immediate Functions
  5. 无法找到类:java.lang.ClassNotFoundException: com.mysql.jdbc.driver
  6. jQuery常用事件
  7. SQL基础查询实战
  8. “使用多target来构建大量相似App”,唐巧大神理论验证(附工程代码地址)
  9. C# 各种帮助类大全
  10. python基础—迭代器、生成器
  11. hdu 5429(大数模板)
  12. 设置Jexus开机启动
  13. K - Transformation HDU - 4578 线段树经典题(好题)
  14. 20165214 实验二 Java面向对象程序设计
  15. preload 与 prefetch 的区别
  16. Jupyter-1-安装Anaconda3及更改路径
  17. Jenkins自动化打包配置
  18. [转]Intellij idea创建javaWeb以及Servlet简单实现
  19. 使用 urllib 处理 HTTP 异常
  20. IP分类

热门文章

  1. [Cracking the Coding Interview] 4.4 Check Balanced
  2. SQL数据库 面试题
  3. WebSocket 的使用
  4. springmvc 处理put,delete请求
  5. DO NOT BELIEVE HIS LIES 游戏随笔
  6. 利用LD_PRELOAD进行hook
  7. 时屏蔽ios和android下点击元素时出现的阴影
  8. Jmeter学习(三)
  9. 「日常训练」Single-use Stones (CFR476D2D)
  10. 子串查询(二维前缀数组) 2018&quot;百度之星&quot;程序设计大赛 - 资格赛