摩斯
— —
— —
— —
— —
大事忌
23.9.25 AC 150
好玩的
桌游
1
暴力
图床1
图床2
同学
神贴
搜东西的
GPT
音乐
随机
55
小说
闫强大大

int
int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x*f;
}
void write(int x){
if(x==0){
putchar('0');return;
}
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}long long
long long read1(){
long long x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x*f;
}
void write1(long long x){
if(x==0){
putchar('0');return;
}
if(x<0) putchar('-'),x=-x;
if(x>9) write1(x/10);
putchar(x%10+'0');
}string
inline string read(){
char ch=getchar();
string st1="";
while(!((ch>='a')&&(ch<='z')))
ch=getchar();
while((ch>='a')&&(ch<='z'))
st1+=ch,ch=getchar();
return st1;
}
inline void write(string s){
for(int i=0;i<s.size();i++) putchar(s[i]);
}double
inline double read(){
long long s=0,w=1,k=0,n=0,m=0;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') w=-1;
ch=getchar();
}
while((ch>='0'&&ch<='9')||ch=='.'){
if(ch=='.') n=1;
else if(n==0) s=s*10+ch-'0';
else k=k*10+ch-'0',m++;
ch=getchar();
}
return (pow(0.1,m)*k+s)*w;
}
可以传多参数的
template <typename T> inline void read(T& x){x = 0; bool f = false; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-') f = !f; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} x = (f ? -x : x); return;}
template <typename T, typename... Args> inline void read(T& x, Args&...x_){read(x), read(x_...);}
template <typename T> inline void put(T x){if(x < 0) putchar('-'), x = -x; if(x > 9) put(x / 10); putchar(x % 10 + '0'); return ;}
template <typename T> inline void write(T x){put(x);}
template <typename T, typename... Args> inline void write(T x, Args...x_){write(x), write(x_...);}teacher’s 线段树
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid (h+t)/2
const int N=1e6+10;
int a[N],n,m;
struct sgt{
int v[N*4],lazy[N*4];
void down(int x,int h,int t){
v[x*2]+=lazy[x]*(mid-h+1); v[x*2+1]+=lazy[x]*(t-mid);
lazy[x*2]+=lazy[x]; lazy[x*2+1]+=lazy[x];
lazy[x]=0;
}
void g(int x,int h,int t,int h1,int t1,int vv){
if (h1<=h&&t<=t1){
v[x]+=(t-h+1)*vv;
lazy[x]+=vv;
return;
}
down(x,h,t);
if (h1<=mid) g(x*2,h,mid,h1,t1,vv);
if (mid<t1) g(x*2+1,mid+1,t,h1,t1,vv);
v[x]=v[x*2]+v[x*2+1];
}
int q(int x,int h,int t,int h1,int t1){
if (h1<=h&&t<=t1){
return v[x];
}
down(x,h,t);
int ans=0;
if (h1<=mid) ans+=q(x*2,h,mid,h1,t1);
if (mid<t1) ans+=q(x*2+1,mid+1,t,h1,t1);
return ans;
}
}S;
signed main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for (int i=1;i<=n;i++) {
cin>>a[i];
S.g(1,1,n,i,i,a[i]);
}
for (int i=1;i<=m;i++) {
int k,x,y,z;
cin>>k;
if (k==1){
cin>>x>>y>>z;
S.g(1,1,n,x,y,z);
} else {
cin>>x>>y;
cout<<S.q(1,1,n,x,y)<<endl;
}
}
return 0;
}二分答案
#define mid (h+t)/2;
bool check(int x)
{
for(int i=1;i<=n;i++) if(a[i]>x) return 0;
int sum=0,cnt=1;
for(int i=1;i<=n;i++)
{
if(sum+a[i]>x) sum=a[i],cnt++;
else sum+=a[i];
}
return cnt<=m;
}
//二分
int h=0,t=1e9;
while(h+10<t)
{
if(check(mid)) t=mid;
else h=mid;
}
for(int i=h;i<=t;i++)
{
if(check(i))
{
cout<<i<<endl;
return 0;
}
}Trie字典树
int in(char s[]){//建树
int p=0;
for(int i=0;s[i];i++){
int a=s[i]-'a';
if(!ch[p][a]) ch[p][a]=++idx;
p=ch[p][a];
}
cnt[p]++;//串尾
}
int sf(char *s){//查询
int p=0;
for(int i=0;s[i];i++){
int a=s[i]-'a';
if(!ch[p][a]) return 0;
p=ch[p][a];
}
return cnt[p];
}
