Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define BAZA 10
  4. #define MAXBUF 4096
  5. #define MAXN 1000000
  6. #define MAXT 2097152
  7. char buf[MAXBUF];
  8. int a, b, val, ans[MAXN+1], v[MAXT], poz=MAXBUF;
  9. FILE *fin, *fout;
  10. inline char nextch(){
  11.     if(poz==MAXBUF){
  12.         fread(buf, 1, MAXBUF, fin);
  13.         poz=0;
  14.     }
  15.     return buf[poz++];
  16. }
  17. inline int numar(){
  18.     int x=0;
  19.     char ch=nextch();
  20.     while(!isdigit(ch)){
  21.         ch=nextch();
  22.     }
  23.     while(isdigit(ch)){
  24.         x=(x<<3)+(x<<1)+ch-'0';
  25.         ch=nextch();
  26.     }
  27.     return x;
  28. }
  29. inline void putch(char ch){
  30.     buf[poz++]=ch;
  31.     if(poz==MAXBUF){
  32.         fwrite(buf, 1, MAXBUF, fout);
  33.         poz=0;
  34.     }
  35. }
  36. inline void baga(int x){
  37.     int cif[BAZA], k=0, t;
  38.     do{
  39.         t=x/BAZA;
  40.         cif[k++]=x-(t<<3)-(t<<1);
  41.         x=t;
  42.     }while(x);
  43.     while(k){
  44.         putch(cif[--k]+'0');
  45.     }
  46. }
  47. void dfs(int t, int st, int dr){
  48.     if(st==dr){
  49.         ans[st]=v[t];
  50.         return ;
  51.     }
  52.     int m=st+(dr-st)/2, fiust=2*t+1, fiudr=2*t+2;
  53.     if(v[t]!=0){
  54.         if(v[t]>v[fiust]){
  55.             v[fiust]=v[t];
  56.         }
  57.         if(v[t]+m+1-st>v[fiudr]){
  58.             v[fiudr]=v[t]+m+1-st;
  59.         }
  60.     }
  61.     dfs(fiust, st, m);
  62.     dfs(fiudr, m+1, dr);
  63. }
  64. void update(int t, int st, int dr){
  65.     if((st>=a)&&(dr<=b)){
  66.         if(v[t]<val+st-a){
  67.             v[t]=val+st-a;
  68.         }
  69.         return ;
  70.     }
  71.     int m=st+(dr-st)/2, fiust=2*t+1, fiudr=2*t+2;
  72.     if(v[t]!=0){
  73.         if(v[t]>v[fiust]){
  74.             v[fiust]=v[t];
  75.         }
  76.         if(v[t]+m+1-st>v[fiudr]){
  77.             v[fiudr]=v[t]+m+1-st;
  78.         }
  79.     }
  80.     if(m>=a){
  81.         update(fiust, st, m);
  82.     }
  83.     if(m+1<=b){
  84.         update(fiudr, m+1, dr);
  85.     }
  86. }
  87. int main(){
  88.     int n, m, i;
  89.     fin=fopen("trade.in", "r");
  90.     fout=fopen("trade.out", "w");
  91.     fscanf(fin, "%d%d ", &n, &m);
  92.     for(i=0; i<m; i++){
  93.         a=numar();
  94.         b=numar();
  95.         val=numar();
  96.         update(0, 1, n);
  97.     }
  98.     dfs(0, 1, n);
  99.     poz=0;
  100.     for(i=1; i<n; i++){
  101.         baga(ans[i]);
  102.         putch(' ');
  103.     }
  104.     baga(ans[n]);
  105.     putch('\n');
  106.     fwrite(buf, 1, poz, fout);
  107.     fclose(fin);
  108.     fclose(fout);
  109.     return 0;
  110. }
  111.  
  112.  
  113.  
  114.  
  115. ////////////////////////////////////////////////
  116.  
  117.  
  118.  
  119. #include <fstream>
  120. #include <algorithm>
  121. using namespace std;
  122.  
  123. const int N = 1000005;
  124.  
  125. struct Trader{
  126.     int x, y, val;
  127.  
  128.     Trader(){
  129.         x = y = 0;
  130.     }
  131.  
  132.     Trader(int _x, int _y, int _val){
  133.         x = _x;
  134.         y = _y;
  135.         val = _val;
  136.     }
  137.  
  138.     inline bool operator<(const Trader& T) const{
  139.         return val < T.val;
  140.     }
  141. };
  142.  
  143. class Pmd{
  144.     int T[N];
  145.  
  146. public:
  147.     inline int tata(int x){
  148.         if (x == T[x])
  149.             return x;
  150.         return T[x] = tata(T[x]);
  151.     }
  152.  
  153.     void reset(int size){
  154.         for (int i = 1 ; i <= size ; i++)
  155.             T[i] = i;
  156.     }
  157.  
  158.     void merge(int x, int y){
  159.         x = T[x];
  160.         y = T[y];
  161.  
  162.         T[x] = y;
  163.     }
  164. };
  165.  
  166. int v[N], n, m;
  167. Trader a[N];
  168. Pmd A;
  169.  
  170. ifstream in("trade.in");
  171. ofstream out("trade.out");
  172.  
  173. int main(){
  174.     int x, y, z;
  175.  
  176.     in >> n >> m;
  177.  
  178.     for (int i = 1 ; i <= m ; i++){
  179.         in >> x >> y >> z;
  180.         a[i] = Trader(x, y, z - x);
  181.     }
  182.  
  183.     sort(a + 1, a + m + 1);
  184.  
  185.     A.reset(n + 1);
  186.  
  187.     for (int i = m ; i ; i--)
  188.         for (int j = A.tata(a[i].x) ; j <= a[i].y ; j = A.tata(j + 1)){
  189.             v[j] = j + a[i].val;
  190.  
  191.             A.merge(j, a[i].y + 1);
  192.         }
  193.  
  194.  
  195.     for (int i = 1 ; i <= n ; i++)
  196.         out << v[i] << " ";
  197.  
  198.     out << "\n";
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement