#include <iostream> #include <vector> #include <algorithm> using namespace std; #define int long long const int maxx = 5e5+7; const int base = 1 << 19; vector<pair<int,int>>last[maxx]; pair<int,int>tre[base << 1]; void add(int v, int hight, int color){ v+=base; tre[v] = {hight,color}; while(v){ v/=2; //tre[v] = tre[2*v] ^ tre[2*v+1]; if(tre[2*v].first > tre[2*v+1].first){ tre[v] = tre[2*v]; }else{ tre[v] = tre[2*v+1]; } } } pair<int,int> que(int a, int b){ pair<int,int>res = {0,0}; a +=base -1; b +=base +1; while(b>a+1){ if(!(a&1)){ //wyni ^= tre[a + 1]; if(res.first < tre[a+1].first){ res = tre[a+1]; } } if(b&1){ //wyni ^= tre[b - 1]; if(res.first < tre[b-1].first){ res = tre[b-1]; } } a/=2; b/=2; } return res; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; int c; cin>>c; vector<pair<int,int>>in(n+1,{0,0}); for(int i=1; n>=i; i++){ pair<int,int>tmp; cin>>tmp.first>>tmp.second; in[i] = tmp; } reverse(in.begin()+1,in.end()); // cout<<endl;for(auto x : in){ // cout<<x.first<<" "<<x.second<<endl; // }cout<<endl; pair<int,int>wsk = {in[1].first,0}; for(int i=1; n>=i; i++){ if(in[i].first < wsk.first){ wsk = {in[i].first,i-1}; } pair<int,int>tmp = que(0,wsk.second); // sprawdzam dwa poprzednie klocki o tym samym kolorze if(last[in[i].second].size() == 0){ last[in[i].second].push_back({in[i].first,i}); int maxi = max(in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); //last[in[i].second].push_back({maxi,i}); }else if(last[in[i].second].size() == 1){ int maxi = in[i].first; if(last[in[i].second][0].first > in[i].first){ pair<int,int>tmp2 = que(last[in[i].second][0].second,last[in[i].second][0].second); maxi = max(tmp2.first + in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); last[in[i].second].push_back({in[i].first,i}); }else{ maxi = max(in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); } }else{ int maxi = in[i].first; if(last[in[i].second][1].first > in[i].first){ pair<int,int>tmp2 = que(last[in[i].second][1].second,last[in[i].second][1].second); maxi = max(tmp2.first + in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); swap(last[in[i].second][1],last[in[i].second][0]); last[in[i].second][1] = {in[i].first, i}; }else{ pair<int,int>tmp2 = que(last[in[i].second][0].second,last[in[i].second][0].second); maxi = max(tmp2.first + in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); } } //wypisywanie maksymalnej wartosci na ten moment // cout<<"rozwzany klocek: "<<in[i].first<<" "<<in[i].second<<endl; // cout<<"maksymalny wstecz: "<<tmp.first<<" "<<tmp.second<<endl; // tmp = que(i,i); // cout<<"wynik na ten moment: "<<tmp.first<<" "<<tmp.second<<endl<<endl; } int maxi = -1; for(int i=1; n>=i; i++){ maxi = max(maxi, que(i,i).first); } cout<<maxi; } /* 4 1 1 1 3 2 4 3 4 1 4 5 1 1 3 2 4 4 4 4 4 5 1 1 3 2 4 3 4 1 */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <iostream> #include <vector> #include <algorithm> using namespace std; #define int long long const int maxx = 5e5+7; const int base = 1 << 19; vector<pair<int,int>>last[maxx]; pair<int,int>tre[base << 1]; void add(int v, int hight, int color){ v+=base; tre[v] = {hight,color}; while(v){ v/=2; //tre[v] = tre[2*v] ^ tre[2*v+1]; if(tre[2*v].first > tre[2*v+1].first){ tre[v] = tre[2*v]; }else{ tre[v] = tre[2*v+1]; } } } pair<int,int> que(int a, int b){ pair<int,int>res = {0,0}; a +=base -1; b +=base +1; while(b>a+1){ if(!(a&1)){ //wyni ^= tre[a + 1]; if(res.first < tre[a+1].first){ res = tre[a+1]; } } if(b&1){ //wyni ^= tre[b - 1]; if(res.first < tre[b-1].first){ res = tre[b-1]; } } a/=2; b/=2; } return res; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; int c; cin>>c; vector<pair<int,int>>in(n+1,{0,0}); for(int i=1; n>=i; i++){ pair<int,int>tmp; cin>>tmp.first>>tmp.second; in[i] = tmp; } reverse(in.begin()+1,in.end()); // cout<<endl;for(auto x : in){ // cout<<x.first<<" "<<x.second<<endl; // }cout<<endl; pair<int,int>wsk = {in[1].first,0}; for(int i=1; n>=i; i++){ if(in[i].first < wsk.first){ wsk = {in[i].first,i-1}; } pair<int,int>tmp = que(0,wsk.second); // sprawdzam dwa poprzednie klocki o tym samym kolorze if(last[in[i].second].size() == 0){ last[in[i].second].push_back({in[i].first,i}); int maxi = max(in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); //last[in[i].second].push_back({maxi,i}); }else if(last[in[i].second].size() == 1){ int maxi = in[i].first; if(last[in[i].second][0].first > in[i].first){ pair<int,int>tmp2 = que(last[in[i].second][0].second,last[in[i].second][0].second); maxi = max(tmp2.first + in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); last[in[i].second].push_back({in[i].first,i}); }else{ maxi = max(in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); } }else{ int maxi = in[i].first; if(last[in[i].second][1].first > in[i].first){ pair<int,int>tmp2 = que(last[in[i].second][1].second,last[in[i].second][1].second); maxi = max(tmp2.first + in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); swap(last[in[i].second][1],last[in[i].second][0]); last[in[i].second][1] = {in[i].first, i}; }else{ pair<int,int>tmp2 = que(last[in[i].second][0].second,last[in[i].second][0].second); maxi = max(tmp2.first + in[i].first, tmp.first + in[i].first - c); add(i,maxi,in[i].second); } } //wypisywanie maksymalnej wartosci na ten moment // cout<<"rozwzany klocek: "<<in[i].first<<" "<<in[i].second<<endl; // cout<<"maksymalny wstecz: "<<tmp.first<<" "<<tmp.second<<endl; // tmp = que(i,i); // cout<<"wynik na ten moment: "<<tmp.first<<" "<<tmp.second<<endl<<endl; } int maxi = -1; for(int i=1; n>=i; i++){ maxi = max(maxi, que(i,i).first); } cout<<maxi; } /* 4 1 1 1 3 2 4 3 4 1 4 5 1 1 3 2 4 4 4 4 4 5 1 1 3 2 4 3 4 1 */ |