QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379537#8572. Passing Gameucup-team3396#WA 1899ms46076kbC++144.7kb2024-04-06 17:48:182024-04-06 17:48:19

Judging History

你现在查看的是最新测评结果

  • [2024-04-06 17:48:19]
  • 评测
  • 测评结果:WA
  • 用时:1899ms
  • 内存:46076kb
  • [2024-04-06 17:48:18]
  • 提交

answer

#pragma GCC optimize(2,3,"Ofast","inline","unroll-loops")
// #pragma GCC target("avx")
#include <bits/stdc++.h>
#define int long long
#define double int
#define MOD1 39989
#define MOD2 1000000000
#define MAXT 40000
using namespace std;
typedef pair<double, int> pdi;

const double eps = 1e-9;

int cmp(double x, double y) {
if(x>y)return -1;
if(x<y)return 1;
  return 0;
}

struct node{
	int x,s,tag;
}a[1000005];

struct line {
  double k, b;
} p[2000005];

int s[2000005];
int cnt;

double calc(int id, int d) { return p[id].b + p[id].k * a[d].x; }

void add(int x0, int y0, int x1, int y1) {
	// cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<"\n";
  cnt++;
  if (x0 == x1)  // 特判直线斜率不存在的情况
    p[cnt].k = 0, p[cnt].b = min(y0, y1);
  else
    p[cnt].k = 1 * (y1 - y0) / (x1 - x0), p[cnt].b = y0 - p[cnt].k * x0;
}

void upd(int root, int cl, int cr, int u) {  // 对线段完全覆盖到的区间进行修改
  int &v = s[root], mid = (cl + cr) >> 1;
  int bmid = cmp(calc(u, mid), calc(v, mid));
  if (bmid == 1 || (!bmid && u < v)) swap(u, v);
  int bl = cmp(calc(u, cl), calc(v, cl)), br = cmp(calc(u, cr), calc(v, cr));
  if (bl == 1 || (!bl && u < v)) upd(root << 1, cl, mid, u);
  if (br == 1 || (!br && u < v)) upd(root << 1 | 1, mid + 1, cr, u);
}

void update(int root, int cl, int cr, int l, int r,
            int u) {  // 定位插入线段完全覆盖到的区间
  if (l <= cl && cr <= r) {
    upd(root, cl, cr, u);
    return;
  }
  int mid = (cl + cr) >> 1;
  if (l <= mid) update(root << 1, cl, mid, l, r, u);
  if (mid < r) update(root << 1 | 1, mid + 1, cr, l, r, u);
}

pdi pmax(pdi x, pdi y) {  // pair max函数
  if (cmp(x.first, y.first) == -1)
    return y;
  else if (cmp(x.first, y.first) == 1)
    return x;
  else
    return x.second < y.second ? x : y;
}

pdi query(int root, int l, int r, int d) {  // 查询
  if (r < d || d < l) return {1e18, 0};
  int mid = (l + r) >> 1;
	// cout<<root<<" "<<l<<" "<<r<<" "<<d<<"\n";
  double res = calc(s[root], d);
  if (l == r) return {res, s[root]};
  return pmax({res, s[root]}, pmax(query(root << 1, l, mid, d),
                                   query(root << 1 | 1, mid + 1, r, d)));
}

// int main() {
//   ios::sync_with_stdio(false);
//   int n, lastans = 0;
//   cin >> n;
//   while (n--) {
//     int op;
//     cin >> op;
//     if (op == 1) {
//       int x0, y0, x1, y1;
//       cin >> x0 >> y0 >> x1 >> y1;
//       x0 = (x0 + lastans - 1 + MOD1) % MOD1 + 1,
//       x1 = (x1 + lastans - 1 + MOD1) % MOD1 + 1;
//       y0 = (y0 + lastans - 1 + MOD2) % MOD2 + 1,
//       y1 = (y1 + lastans - 1 + MOD2) % MOD2 + 1;
//       if (x0 > x1) swap(x0, x1), swap(y0, y1);
//       add(x0, y0, x1, y1);
//       update(1, 1, MOD1, x0, x1, cnt);
//     } else {
//       int x;
//       cin >> x;
//       x = (x + lastans - 1 + MOD1) % MOD1 + 1;
//       cout << (lastans = query(1, 1, MOD1, x).second) << endl;
//     }
//   }
//   return 0;
// }

bool cmpp(node A,node B){
	return A.x<B.x;
}
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
inline void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
int QQ;
int dp[1000005],f[1000005],g[1000005];
int solve(){
	int n,k; n=read(),k=read();
	for(int i=1;i<=n;++i) a[i].x=read();
	for(int i=1;i<=n;++i) a[i].s=read(),a[i].tag=0;
	a[1].tag=1;
	a[n].tag=2;
	sort(a+1,a+n+1,cmpp);
	for(int i=1;i<=n;++i) dp[i]=1e18;
	for(int i=1;i<=n;++i) if(a[i].tag==1) dp[i]=0;
	for(int i=1;i<=4*n;++i) s[i]=0;
	p[0].b=1e18;
	int lim=min(k,13ll);
	if(n<=10000&&QQ)lim=min(k,50ll);
	for(int i=0;i<=lim;++i){
		for(int j=1;j<=n;++j) f[j]=g[j]=1e18;
		for(int j=1;j<=cnt;++j) p[j].k=p[j].b=0;
			for(int j=1;j<=4*n;++j)s[j]=0;
		cnt=0;
		for(int j=1;j<=n;++j){
			f[j]=min(dp[j],(int)query(1,1,n,j).first);
			// cout<<f[j]<<" ";
			add(a[1].x,f[j]-(a[j].x-a[1].x)*a[j].s,a[n].x,f[j]+(a[n].x-a[j].x)*a[j].s);
			update(1,1,n,1,n,cnt);
		}
		for(int j=1;j<=cnt;++j) p[j].k=p[j].b=0;
			for(int j=1;j<=4*n;++j)s[j]=0;
		cnt=0;
		for(int j=n;j>=1;j--){
			g[j]=min(dp[j],(int)query(1,1,n,j).first);
			// cout<<g[j]<<" ";
			add(a[1].x,f[j]+(a[j].x-a[1].x)*a[j].s,a[n].x,f[j]-(a[n].x-a[j].x)*a[j].s);
			update(1,1,n,1,n,cnt);
		}
		for(int j=1;j<=n;++j) dp[j]=min(f[j],g[j]);//cout<<dp[j]<<" ";
		// cout<<"\n";
	}
	for(int i=1;i<=n;++i) if(a[i].tag==2) return dp[i];
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t; t=read();
	if(t<=1000)QQ=1;
	while(t--) write(solve()),putchar('\n');
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13960kb

input:

2
4 2
3 2 1 6
3 1 1 3
2 0
1 2
1 2

output:

7
1

result:

ok 2 number(s): "7 1"

Test #2:

score: 0
Accepted
time: 1881ms
memory: 46076kb

input:

1
300000 204334
809492393 304618667 173130445 377106790 364888630 949045125 622060683 557772818 216607577 848817467 862855568 507840723 120816645 639713488 741781998 682531787 685261161 601686403 355792373 162819930 710057718 234560726 998604853 678957602 485413982 855985802 109303681 979706626 4822...

output:

31313390701066

result:

ok 1 number(s): "31313390701066"

Test #3:

score: 0
Accepted
time: 1682ms
memory: 24956kb

input:

3
100000 65460
217141764 710454586 789075415 24849107 685675008 839804815 638763480 327755609 43827967 390187172 301370841 622696676 598237196 232099091 211987715 416876077 572665966 73382836 520033984 808399404 752832432 341795744 434460344 535426588 136624537 997406768 297342165 558882675 26863877...

output:

70635841128944
47230361360721
59110547802683

result:

ok 3 number(s): "70635841128944 47230361360721 59110547802683"

Test #4:

score: 0
Accepted
time: 1899ms
memory: 42500kb

input:

1
300000 101975
207258305 525434317 528778163 645316642 562113679 143398489 9114413 669854123 106324041 841914487 21419012 308025536 689200225 263298218 39377353 860366080 24610184 43404209 529054797 902238799 422737070 484129934 967667618 953541323 338625285 115085955 363490839 998893783 877857789 ...

output:

40311829457542

result:

ok 1 number(s): "40311829457542"

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 13832kb

input:

18
17 0
500000000 499999997 500000010 499999965 500000118 499999609 500001291 499995739 500014064 499953589 500153157 499494579 501667889 494495965 518163316 440061055 697798520
197798520 59938945 18163316 5504035 1667889 505421 153157 46411 14064 4261 1291 391 118 35 10 3 1
17 1
500000000 499999997...

output:

15506866876
14901483521
14599311968
14327495899
14070135725
13814605121
13559139999
13303815695
13061698660
13061698660
13061698660
13061698660
13061698660
13061698660
13061698660
13061698660
13061698660
13061698660

result:

wrong answer 3rd numbers differ - expected: '14901483521', found: '14599311968'