UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#198217#2789. 极差snow_trace1007927ms146156kbC++114.3kb2023-11-21 11:02:152023-11-21 14:08:37

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lowbit(x) x&(-x)
int n;
int a[500005];
const int mod = 998244353;
const int inv = mod+1>>1;
const int N = 500005;
int inv2[1000005];
int Tree1[500010],Tree2[500010];
void Upd1(int pos,int add){
	for(int i = pos;i<N;i+=lowbit(i))Tree1[i]+=add;
}void Upd2(int pos,int add){
	for(int i = pos;i<N;i+=lowbit(i))Tree2[i]+=add;
}int Query1(int pos){
	int res = 0;for(int i = pos;i>0;i-=lowbit(i))res+=Tree1[i];return res;
}int Query2(int pos){
	int res = 0;for(int i= pos;i>0;i-=lowbit(i))res+=Tree2[i];return res;
}
struct tree{
	int l,r;
	int pre,suf;
	int lazy1 = 1,lazy2 = 1,tot1 = 1,tot2 = 1;
	void add1(int x){
		lazy1*=x,pre*=x;tot1*=x;
		lazy1%=mod,pre%=mod;tot1%=mod;
	}void add2(int x){
		lazy2*=x,suf*=x;tot2*=x;
		lazy2%=mod,suf%=mod;tot2%=mod;
	}
}tree[2000005];
void push_up(int k){
	tree[k].pre = tree[k<<1].pre+tree[k<<1|1].pre;
	if(tree[k].pre>mod)tree[k].pre-=mod;
	tree[k].suf = tree[k<<1].suf+tree[k<<1|1].suf;
	if(tree[k].suf>mod)tree[k].suf-=mod;
}void push_down(int k){
	if(tree[k].lazy1!=1)tree[k<<1].add1(tree[k].lazy1),tree[k<<1|1].add1(tree[k].lazy1),tree[k].lazy1 = 1;
	if(tree[k].lazy2!=1)tree[k<<1].add2(tree[k].lazy2),tree[k<<1|1].add2(tree[k].lazy2),tree[k].lazy2 = 1;
}
void build(int l,int r,int k){
	//cout << l << " " << r << " " << k << endl;
	tree[k].l = l,tree[k].r = r;tree[k].tot1 = tree[k].tot2 = 1;
	tree[k].pre = tree[k].suf =0 ;tree[k].lazy1 = tree[k].lazy2 = 1;
	if(l+1 == r){return;}
	build(l,l+r>>1,k<<1),build(l+r>>1,r,k<<1|1);
}void upd1(int l,int r,int add,int k){
	int ll = tree[k].l,rr = tree[k].r;
	if(l>=rr or ll>=r)return;
	if(l<=ll and rr<=r){
		tree[k].add1(add);return;
	}push_down(k),upd1(l,r,add,k<<1),upd1(l,r,add,k<<1|1);push_up(k);
}void upd2(int l,int r,int add,int k){
	int ll = tree[k].l,rr = tree[k].r;
	if(l>=rr or ll>=r)return;
	if(l<=ll and rr<=r){
		tree[k].add2(add);return;
	}push_down(k),upd2(l,r,add,k<<1),upd2(l,r,add,k<<1|1);push_up(k);
}void nrd(int pos,int k){
	int l = tree[k].l,r = tree[k].r,mid = l+r>>1;
	if(l+1 == r){
		tree[k].pre = pos*tree[k].tot1%mod,tree[k].suf = (n-pos+1)*tree[k].tot2%mod;return;
	}push_down(k);
	if(pos<mid)nrd(pos,k<<1);
	else nrd(pos,k<<1|1);
	push_up(k);
}int query1(int l,int r,int k){
	int ll = tree[k].l,rr = tree[k].r;
	if(ll>=r or l>=rr)return 0;
	if(l<=ll and rr<=r)return tree[k].pre;
	push_down(k);return (query1(l,r,k<<1)+query1(l,r,k<<1|1))%mod;
}int query2(int l,int r,int k){
	int ll = tree[k].l,rr = tree[k].r;
	if(ll>=r or l>=rr)return 0;
	if(l<=ll and rr<=r)return tree[k].suf;
	push_down(k);return (query2(l,r,k<<1)+query2(l,r,k<<1|1))%mod;
}
vector<pair<int,int> >p;
bool cmp(pair<int,int>a,pair<int,int>b){
	return a.first<b.first;
}
signed main(){
	cin >> n;inv2[0] = 1;for(int i = 1;i<=2*n;i++)inv2[i] = inv2[i-1]*inv%mod;
	for(int i= 1;i<=n;i++)cin >> a[i];
	for(int i = 1;i<=n;i++){
		p.push_back({a[i],i});
	}sort(p.begin(),p.end(),cmp);build(1,n+1,1);
	int ans = 0;
	for(int i =0;i<p.size();i++){
		int pos = p[i].second,val = p[i].first;
		nrd(pos,1);
	//	cout << pos << endl;
		//cout << query1(1,pos+1,1) << " " << query2(pos,n+1,1) << endl;
		//cout << " "<< query1(1,pos+1,1)*query2(pos,n+1,1)%mod*val << " " << query1(1,pos+1,1)*query2(pos,n+1,1)%mod*val%mod*inv2[i]%mod << endl;
		ans+=query1(1,pos+1,1)*query2(pos,n+1,1)%mod*val%mod*inv2[i]%mod;ans%=mod;
		//cout << query2(1,n+1,1) << endl;
		//for(int j = 1;j<=n;j++)cout << query2(j,j+1,1) << " ";cout << endl;
		upd1(1,pos,2,1),upd2(pos+1,n+1,2,1);
		//;ans+=pos*(n-pos+1)*a[i]%mod;
	}//cout << ans << endl;
	reverse(p.begin(),p.end());build(1,n+1,1);
	for(int i =0;i<p.size();i++){
		int pos = p[i].second,val = p[i].first;
		nrd(pos,1);
	//	cout << pos << endl;
		//cout << query1(1,pos+1,1) << " " << query2(pos,n+1,1) << endl;
		//cout << " "<< query1(1,pos+1,1)*query2(pos,n+1,1)%mod*val << " " << query1(1,pos+1,1)*query2(pos,n+1,1)%mod*val%mod*inv2[i]%mod << endl;
		ans-=query1(1,pos+1,1)*query2(pos,n+1,1)%mod*val%mod*inv2[i]%mod;ans%=mod;ans +=mod;ans%=mod;
		//cout << query2(1,n+1,1) << endl;
		//for(int j = 1;j<=n;j++)cout << query2(j,j+1,1) << " ";cout << endl;
		upd1(1,pos,2,1),upd2(pos+1,n+1,2,1);
		//;ans+=pos*(n-pos+1)*a[i]%mod;
	}cout << ans << endl;
	
	return 0;
}/*
3
2 3 1 
*/

这程序好像有点Bug,我给组数据试试?

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 15ms
memory: 126256kb

input:

10
822569775 140960465 675448100 378356373 881781963 632511858 517856306 355237516 318232566 701815470

output:

783495679

result:

ok "783495679"

Test #2:

score: 10
Accepted
time: 20ms
memory: 126256kb

input:

10
449281704 767368983 682106748 506365083 122199784 498808182 538569145 55437432 155268974 94445018

output:

68844302

result:

ok "68844302"

Test #3:

score: 10
Accepted
time: 8ms
memory: 126256kb

input:

10
591970549 421988862 413830573 240490034 164237038 902977274 59135052 95107365 250425253 324531999

output:

147766221

result:

ok "147766221"

Test #4:

score: 10
Accepted
time: 27ms
memory: 126496kb

input:

5000
646617726 824464102 323759947 246753612 585842690 504415490 14828815 693037963 801159103 946215...

output:

67443460

result:

ok "67443460"

Test #5:

score: 10
Accepted
time: 19ms
memory: 126500kb

input:

5000
654241726 72924489 266088677 295706937 963146543 634317920 182210355 549745450 893149778 174946...

output:

389625531

result:

ok "389625531"

Test #6:

score: 10
Accepted
time: 17ms
memory: 126496kb

input:

5000
38701861 203013652 438779672 247355223 112434120 403380796 731125167 338765345 868447685 883741...

output:

770908379

result:

ok "770908379"

Test #7:

score: 10
Accepted
time: 781ms
memory: 134936kb

input:

200000
695831669 804173945 82349222 653935161 167064728 232585930 988716260 695383039 585257840 3008...

output:

745841684

result:

ok "745841684"

Test #8:

score: 10
Accepted
time: 2354ms
memory: 146156kb

input:

500000
5 8 7 10 7 9 7 4 4 4 5 0 0 10 6 0 0 7 10 6 5 3 9 7 3 8 6 10 3 1 9 6 4 10 1 8 5 3 1 9 2 5 5 9 ...

output:

225099176

result:

ok "225099176"

Test #9:

score: 10
Accepted
time: 2424ms
memory: 146156kb

input:

500000
494353572 796134262 130578837 890308560 320968753 455636669 324898154 477464175 320403450 543...

output:

396578562

result:

ok "396578562"

Test #10:

score: 10
Accepted
time: 2262ms
memory: 146152kb

input:

500000
303781858 81904833 166578999 116580840 916556701 186538262 945202043 778425019 138280242 9981...

output:

394870719

result:

ok "394870719"

Extra Test:

score: 0
Extra Test Passed