社区讨论
关于这道题的二分部分的疑惑
学术版参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lolg9dlo
- 此快照首次捕获于
- 2023/11/05 20:28 2 年前
- 此快照最后确认于
- 2023/11/05 22:24 2 年前
题目
两种二分,分数不同
只有二分部分不同!!!
百思不得其解,求助!
C
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0,f = 1;char c = getchar();
for(;!isdigit(c);c = getchar())if( c == '-' )f = -1;
for(;isdigit(c);c = getchar())x = ( x << 1 ) + ( x << 3 ) + c - '0';
return x * f;
}
const int N = 1e6 + 5;
const int M = 1e3 + 5;
int n,m;
int a[N],belong[N],l[M],r[M],lazy[M],t[N],tot,len;
inline void build(){
len = sqrt( n );
tot = n / len;
if(n % len)tot ++;
for(int i = 1;i <= n;i ++){
t[i] = a[i] = read();
belong[i] = (i - 1) / len + 1;
}
for(int i = 1;i <= tot;i ++){
l[i] = (i - 1) * len + 1;
r[i] = i * len;
}
r[tot] = n;
for(int i = 1;i <= tot;i ++)
sort( t + l[i] , t + r[i] + 1 );
}
inline void change(int x,int y,int k){
if( belong[x] == belong[y] ){
for(int i = x;i <= y;i ++)a[i] += k;
for(int i = l[belong[x]];i <= r[belong[x]];i ++)t[i] = a[i];
sort( t + l[belong[x]] , t + r[belong[x]] + 1 );
}
else{
for(int i = x;i <= r[belong[x]];i ++)a[i] += k;
for(int i = l[belong[x]];i <= r[belong[x]];i ++)t[i] = a[i];
sort( t + l[belong[x]] , t + r[belong[x]] + 1 );
for(int i = l[belong[y]];i <= y;i ++)a[i] += k;
for(int i = l[belong[y]];i <= r[belong[y]];i ++)t[i] = a[i];
sort( t + l[belong[y]] , t + r[belong[y]] + 1 );
for(int i = belong[x] + 1;i <= belong[y] - 1;i ++)lazy[i] += k;
}
}
inline int query(int x,int y,int c){
int ret = 0;
if( belong[x] == belong[y] ){
for(int i = x;i <= y;i ++)
if( a[i] + lazy[belong[x]] >= c )ret ++;
return ret;
}
else{
for(int i = x;i <= r[belong[x]];i ++){
if( a[i] + lazy[belong[x]] >= c ){
ret ++;
}
}
for(int i = l[belong[y]];i <= y;i ++){
if( a[i] + lazy[belong[y]] >= c ){
ret ++;
}
}
for(int i = belong[x] + 1;i <= belong[y] - 1;i ++){
int L = l[i],R = r[i],mid;
while( L < R ){
mid = (L + R) >> 1;
if( t[mid] + lazy[i] >= c )R = mid;
else L = mid + 1;
}
ret += r[i] - L + 1;
}
return ret;
}
}
signed main(){
n = read();
m = read();
build();
for(int i = 1;i <= m;i ++){
char opt;
cin >> opt;
int x = read(),y = read(),w = read();
if( opt == 'M' ){
change( x , y , w );
}
else{
int ans = query( x , y , w );
printf("%d\n",ans);
}
}
return 0;
}
C
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0,f = 1;char c = getchar();
for(;!isdigit(c);c = getchar())if( c == '-' )f = -1;
for(;isdigit(c);c = getchar())x = ( x << 1 ) + ( x << 3 ) + c - '0';
return x * f;
}
const int N = 1e6 + 5;
const int M = 1e3 + 5;
int n,m;
int a[N],belong[N],l[M],r[M],lazy[M],t[N],tot,len;
inline void build(){
len = sqrt( n );
tot = n / len;
if(n % len)tot ++;
for(int i = 1;i <= n;i ++){
t[i] = a[i] = read();
belong[i] = (i - 1) / len + 1;
}
for(int i = 1;i <= tot;i ++){
l[i] = (i - 1) * len + 1;
r[i] = i * len;
}
r[tot] = n;
for(int i = 1;i <= tot;i ++)
sort( t + l[i] , t + r[i] + 1 );
}
inline void change(int x,int y,int k){
if( belong[x] == belong[y] ){
for(int i = x;i <= y;i ++)a[i] += k;
for(int i = l[belong[x]];i <= r[belong[x]];i ++)t[i] = a[i];
sort( t + l[belong[x]] , t + r[belong[x]] + 1 );
}
else{
for(int i = x;i <= r[belong[x]];i ++)a[i] += k;
for(int i = l[belong[x]];i <= r[belong[x]];i ++)t[i] = a[i];
sort( t + l[belong[x]] , t + r[belong[x]] + 1 );
for(int i = l[belong[y]];i <= y;i ++)a[i] += k;
for(int i = l[belong[y]];i <= r[belong[y]];i ++)t[i] = a[i];
sort( t + l[belong[y]] , t + r[belong[y]] + 1 );
for(int i = belong[x] + 1;i <= belong[y] - 1;i ++)lazy[i] += k;
}
}
inline int query(int x,int y,int c){
int ret = 0;
if( belong[x] == belong[y] ){
for(int i = x;i <= y;i ++)
if( a[i] + lazy[belong[x]] >= c )ret ++;
return ret;
}
else{
for(int i = x;i <= r[belong[x]];i ++){
if( a[i] + lazy[belong[x]] >= c ){
ret ++;
}
}
for(int i = l[belong[y]];i <= y;i ++){
if( a[i] + lazy[belong[y]] >= c ){
ret ++;
}
}
for(int i = belong[x] + 1;i <= belong[y] - 1;i ++){
int L = l[i],R = r[i],mid,res = 0;
while( L <= R ){
mid = (L + R) >> 1;
if( t[mid] + lazy[i] >= c )R = mid - 1,res = r[i] - mid + 1;
else L = mid + 1;
}
ret += res;
}
return ret;
}
}
signed main(){
n = read();
m = read();
build();
for(int i = 1;i <= m;i ++){
char opt;
cin >> opt;
int x = read(),y = read(),w = read();
if( opt == 'M' ){
change( x , y , w );
}
else{
int ans = query( x , y , w );
printf("%d\n",ans);
}
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...