世风日下的哗啦啦族I

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Problem Description

"世风日下啊,女生不穿裙子的太少了"
"这不是社会的进步吗(逃"
"哎,是否可以建立一种结构统计一下各学院各专业各班级女生穿裙子的数量以及裙子的长度"
"然后查询区间裙子最短值?"
"并输出这个区间 穿这个裙子长度的妹子 有多少个?"
"然后判断下在有风的情况下,多少妹子是安全的."

"maya 哗啦啦族真是太可怕啦!"

Input

第一行 n,m表示n个妹子,m个操作
第二行 a1 a2 a3 …… an,n个整数,表示妹子一开始的裙子长度
下面m行:
总共3种操作
1 a b
修改a妹子的裙子,变成b长度
2 l r
查询[l,r]区间的妹子最短的裙子长度,并输出有多少个妹子穿这个长度裙子的
3 l r t
输出[l,r]区间的妹子身穿裙子长度小于等于t的个数
1<=n,m<=50000
1<=a<=n
1<=b<=50000
1<=ai<=50000
1<=l<=r<=n
0<=t<=50000

Output

根据询问,输出答案

询问为2 则在一行输出两个整数 a b

询问为3 则只输出一个整数c

Sample Input

3 3
1 2 3
1 1 4
2 1 3
3 1 3 2

Sample Output

2 1
1

题目链接:ACdream 1738

题面有点意思…………很容易想到每一个块维护最小值和最小值的出现次数以及块内的有序序列,然后就是分块的最重要思想——两边暴力,中间利用块的信息快速得到答案,显然这题应该二分位置t即可,不过做的时候迷之TLE和WA了好多发

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e6 + 7;
struct Block
{
int l, r, Min, cnt;
};
Block B[50000];
int arr[N], belong[N];
int st[N], unit, bcnt, n, m; int bs(int l, int r, int key)
{
int L = l, R = r;
int ans = l;
while (L <= R)
{
int mid = MID(L, R);
if (st[mid] <= key)
{
ans = mid;
L = mid + 1;
}
else
R = mid - 1;
}
return ans - l + 1;
}
void reset(int bx)
{
int i;
for (i = B[bx].l; i <= B[bx].r; ++i)
st[i] = arr[i];
sort(st + B[bx].l, st + B[bx].r + 1);
B[bx].Min = st[B[bx].l];
B[bx].cnt = 0;
for (i = B[bx].l; i <= B[bx].r; ++i)
{
if (st[i] == B[bx].Min)
++B[bx].cnt;
else
break;
}
}
void init()
{
int i;
unit = sqrt(n);
bcnt = n / unit;
if (n % unit)
++bcnt;
for (i = 1; i <= bcnt; ++i)
{
B[i].l = (i - 1) * unit + 1;
B[i].r = i * unit;
}
B[bcnt].r = n;
for (i = 1; i <= n; ++i)
belong[i] = (i - 1) / unit + 1;
for (i = 1; i <= bcnt; ++i)
reset(i);
}
void update(int x, int v)
{
arr[x] = v;
reset(belong[x]);
}
int getMin(int l, int r)
{
int i;
int bl = belong[l], br = belong[r];
int Min = INF;
if (bl == br)
{
for (i = l; i <= r; ++i)
if (arr[i] < Min)
Min = arr[i];
}
else
{
for (i = l; i <= B[bl].r; ++i)
if (arr[i] < Min)
Min = arr[i];
for (i = B[br].l; i <= r; ++i)
if (arr[i] < Min)
Min = arr[i];
for (i = bl + 1; i < br; ++i)
if (B[i].Min < Min)
Min = B[i].Min;
}
return Min;
}
int getcnt(int l, int r, int Min)
{
int i;
int bl = belong[l], br = belong[r];
int ans = 0;
if (bl == br)
{
for (i = l; i <= r; ++i)
if (arr[i] == Min)
++ans;
}
else
{
for (i = l; i <= B[bl].r; ++i)
if (arr[i] == Min)
++ans;
for (i = B[br].l; i <= r; ++i)
if (arr[i] == Min)
++ans;
for (i = bl + 1; i < br; ++i)
if (B[i].Min == Min)
ans += B[i].cnt;
}
return ans;
}
int getless(int l, int r, int t)
{
int bl = belong[l], br = belong[r];
int ans = 0, i;
if (bl == br)
{
for (i = l; i <= r; ++i)
if (arr[i] <= t)
++ans;
}
else
{
for (i = l; i <= B[bl].r; ++i)
if (arr[i] <= t)
++ans;
for (i = B[br].l; i <= r; ++i)
if (arr[i] <= t)
++ans;
for (i = bl + 1; i < br; ++i)
if (B[i].Min <= t)
ans += bs(B[i].l, B[i].r, t);
}
return ans;
}
int main(void)
{
int i, ops, l, r, a, b, t;
while (~scanf("%d%d", &n, &m))
{
for (i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
init();
while (m--)
{
scanf("%d", &ops);
if (ops == 1)
{
scanf("%d%d", &a, &b);
update(a, b);
}
else if (ops == 2)
{
scanf("%d%d", &l, &r);
int Min = getMin(l, r);
int cnt = getcnt(l, r, Min);
printf("%d %d\n", Min, cnt);
}
else if (ops == 3)
{
scanf("%d%d%d", &l, &r, &t);
printf("%d\n", getless(l, r, t));
}
}
}
return 0;
}
05-17 18:36