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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 #include using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define ALL(G) (G).begin(),(G).end() //#define DEBUG #ifdef DEBUG #define DEB printf #else #define DEB(...) #endif typedef double D; typedef long long LL; typedef pair pli; typedef vector vi; const int mod = 1000000007; const int MAXN = 200006;//10^6 LL T[MAXN]; int Q[MAXN]; LL ANS[MAXN]; int n; LL brut(int d){ LL last=-(1LL<<50),ret=0; fru(i,n){ last=max(last,T[i]-d)+d; ret+=last-T[i]; } return ret; } LL kw(int a){return 1LL*a*(a+1)/2;} LL cost; int REP[MAXN],siz[MAXN],rig[MAXN],lef[MAXN]; bool self[MAXN]; int find(int x){ return REP[x]==x?x:(REP[x]=find(REP[x])); } void onion(int a, int b){ DEB("\tzlaczam %d i %d\n",a,b); a=find(a); b=find(b); assert(a && b); if(a==b) return; if(!self[a]) cost-=kw(siz[a]); if(!self[b]) cost-=kw(siz[b]); siz[a]+=siz[b]; cost+=kw(siz[a]); REP[b]=REP[a]; rig[a]=max(rig[a],rig[b]); lef[a]=min(lef[a],lef[b]); assert(siz[a]==rig[a]-lef[a]+1); self[b]=0; } set events; LL begg(int id,int d){ id=find(id); if(self[id]) return T[id]-d; return T[lef[id]-1]; } void make_event(int a,int b, int d){ a=find(a); b=find(b); DEB("make_event %d %d %d\n",a,b,d); assert(self[b] && siz[b]==1); assert(a=T[i]); ret+=last-T[i]; } cost=0; fru(i,n) if(find(i)==i && !self[i]) cost+=kw(siz[i]); DEB("ret = %lld\n",ret); assert(ret==brut(d)); fru(i,m) events.insert(pli(Q[i],MAXN+i)); assert((events.begin())->x==d); dprev=d; tr(it,events) DEB("%lld,%d\n",it->x,it->y); int answered=0; DEB("teraz c= %lld\n",cost); fru(i,n) if(self[i]) assert(siz[i]==1); while(answered%lld %d\n",e.x,e.y); events.erase(events.begin()); d=e.x; assert(d<=1000000); int id=e.y; assert(d>=dprev); DEB("cost = %lld\n",cost); ret+=cost*(d-dprev); dprev=d; if(id>=MAXN){ ANS[id-MAXN]=ret; /* fru(i,n) DEB("\tfind(%d) = %d, self = %d, siz = %d\n",i,find(i),self[find(i)],siz[find(i)]); DEB("odpowiadam na zapytanie o %lld: %lld ?? %lld\n",d,ret,brut(d)); assert(ret==brut(d)); //2 del!!! */ answered++; continue; } //przetworz event if(!self[id]) continue; assert(siz[id]==1); self[id]=0; cost++; LL roz=1; DEB("pierwsyz if\n"); LL poprz=begg(find(id-1),d); poprz+=1LL*siz[find(id-1)]*d; LL chcialem=T[id]-d; assert(poprz>chcialem); roz=poprz-chcialem; ret+=roz; DEB("teraz ret rosnie o %lld\n",roz); if(!self[find(id-1)]) onion(id-1,id); while(1){ DEB("while\n"); LL be=begg(find(id),d); LL en=be+1LL*d*siz[find(id)]; int r=find(rig[find(id)]+1); DEB("be = %lld, en = %lld, r = %d, begg(r): %lld\n",be,en,r,begg(r,d)); if(begg(r,d)<=en){ ret+=1LL*siz[r]*(en-begg(r,d)); DEB("teraz ret rosnie o %d*%lld\n",siz[r],en-begg(r,d)); onion(id,r); } else{ make_event(id,r,d); break; } } /* LL c=0; fru(i,n) if(find(i)==i && !self[i]) c+=kw(siz[i]); DEB("c = %lld, cost = %lld\n",c,cost); assert(c==cost);*/ } fru(i,m) printf("%lld\n",ANS[i]); return 0; }